WSL2 Network Settings and Configuration Guide

wsl2 network configuration settings

To configure WSL2 networking, edit %UserProfile%\.wslconfig on Windows. On Windows 11 22H2 or later, set networkingMode=mirrored under [wsl2] to share the Windows network interfaces, get LAN access and use localhost in both directions. Run wsl --shutdown to apply it. On Windows 10, keep the default NAT mode and forward ports with netsh.

Network configuration in WSL2 makes or breaks your development workflow. Below we set up and fine-tune the network settings that matter.

Short version

iniini
# %UserProfile%\.wslconfig (Windows 11 22H2 or later)
[wsl2]
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true

Save the file, run wsl --shutdown in PowerShell, and start your distro again.

Table of Contents

Understanding WSL2’s Network Setup

WSL2 works differently from the original WSL. It runs a real Linux kernel in a lightweight VM and uses Hyper-V for networking. This means better compatibility but also some unique challenges.

By default WSL2 uses NAT mode: the VM gets its own private IP address behind a virtual NAT on your Windows host. The networkingMode key in .wslconfig accepts nat (the default), mirrored, none and consomme. The old bridged mode is deprecated since WSL 2.4.5. Mirrored mode copies your Windows network interfaces into Linux and adds IPv6 support, better VPN compatibility, multicast and direct LAN access.

Getting Started with Basic Settings

First, check your current network setup:

Terminalbash
ip addr show

You’ll see two main interfaces:

  • eth0: Your primary network connection
  • lo: The local loopback

In mirrored mode you see copies of your Windows adapters instead. From PowerShell, wsl hostname -I prints the IP address of your default distro.

Setting Up a Fixed IP

WSL2 has no setting for a static IP in NAT mode. WSL assigns the VM address itself, and it can change after wsl --shutdown or a reboot. Older guides, including an earlier version of this one, edited netplan files inside the distro, but that does not give you a stable address.

Use one of these options instead:

  • Connect from Windows to localhost. localhostForwarding=true is the default in NAT mode, so a server listening in WSL2 is reachable at localhost:port on Windows.
  • Switch to mirrored mode. WSL then uses the same IP addresses as Windows, so other devices reach WSL services at your Windows IP. If your router gives Windows a fixed DHCP lease, WSL gets it too.
  • Look up the current address when you need it with wsl hostname -I and script around it.

Opening Ports and Access

In NAT mode, you need port forwarding to reach WSL2 services from other machines. Open PowerShell as admin:

powershellpowershell
netsh interface portproxy add v4tov4 listenport=3000 listenaddress=0.0.0.0 connectport=3000 connectaddress=(wsl hostname -I)

And set up the firewall:

powershellpowershell
New-NetFirewallRule -DisplayName "WSL2 Port 3000" -Direction Inbound -Action Allow -Protocol TCP -LocalPort 3000

The portproxy rule points at the WSL IP from the moment you created it. Run the command again after the WSL IP changes.

In mirrored mode you don’t need portproxy. Bind your app to 0.0.0.0 and allow the port through the Hyper-V firewall, which filters WSL traffic by default on Windows 11 22H2 or later with WSL 2.0.9 or later:

powershellpowershell
New-NetFirewallHyperVRule -Name "WSL2-3000" -DisplayName "WSL2 Port 3000" -Direction Inbound -VMCreatorId '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}' -Protocol TCP -LocalPorts 3000

Fixing Common Network Issues

DNS Problems

On Windows 11 22H2 or later, dnsTunneling is on by default and answers DNS requests from WSL through Windows. That fixes most VPN-related DNS failures. If websites still won’t load, stop WSL from generating its own resolver file. Edit /etc/wsl.conf inside the distro:

Terminalbash
sudo nano /etc/wsl.conf

Add these lines:

iniini
[network]
generateResolvConf = false

Run wsl --shutdown in PowerShell, start the distro again, and replace the resolver file:

Terminalbash
sudo rm /etc/resolv.conf
sudo nano /etc/resolv.conf

Add these DNS servers:

Snippettext
nameserver 8.8.8.8
nameserver 8.8.4.4

Keep WSL2 from changing it:

Terminalbash
sudo chattr +i /etc/resolv.conf

Working Behind a Proxy

On Windows 11 22H2 or later, autoProxy=true under [wsl2] in .wslconfig makes WSL use the HTTP proxy configured in Windows. It is on by default. If you need a different proxy, or you are on Windows 10, add it to your shell config (~/.bashrc or ~/.zshrc):

Terminalbash
export HTTP_PROXY="http://proxy.example.com:8080"
export HTTPS_PROXY="http://proxy.example.com:8080"
export NO_PROXY="localhost,127.0.0.1"

Making It Faster

Memory Settings

Edit %UserProfile%/.wslconfig in Windows:

iniini
[wsl2]
memory=8GB
processors=4
swap=2GB
localhostForwarding=true

localhostForwarding is ignored when networkingMode=mirrored, because mirrored mode shares localhost with Windows anyway. Run wsl --shutdown after every change to .wslconfig.

Network Speed

Update /etc/sysctl.conf with:

Snippettext
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_probes = 5
net.ipv4.tcp_keepalive_intvl = 15

Load the new values with sudo sysctl -p. These settings tune TCP timeouts and keepalives, which helps with long-lived connections more than with raw throughput.

Fixing Network Problems

When things go wrong:

  1. Restart WSL2 from PowerShell, wait about 8 seconds, then start it again:
powershellpowershell
wsl --shutdown
wsl
  1. Update WSL, since many networking fixes ship in WSL releases:
powershellpowershell
wsl --update
  1. Check host networking (NAT mode, PowerShell as admin). The adapter is called vEthernet (WSL) or vEthernet (WSL (Hyper-V firewall)) depending on your version, so the filter matches both:
powershellpowershell
Get-NetIPInterface | Where-Object { $_.InterfaceAlias -like 'vEthernet (WSL*' } | Set-NetIPInterface -Forwarding Enabled

Working with Development Tools

For VS Code, update your settings:

jsonjson
{
    "remote.WSL.fileWatcher.polling": true
}

File watcher polling mainly fixes rename errors on WSL 1. It costs performance on large workspaces, so leave it off on WSL2 unless file changes go unnoticed.

Keeping Things Secure

Set up firewalls in both systems:

In WSL2:

Terminalbash
sudo ufw enable
sudo ufw allow 3000

In Windows:

powershellpowershell
New-NetFirewallRule -DisplayName "WSL2 Inbound" -Direction Inbound -Action Allow -Protocol TCP -LocalPort 3000

On Windows 11 22H2 or later, keep firewall=true in .wslconfig so Windows Firewall and Hyper-V firewall rules also filter WSL traffic. In mirrored mode, open ports with New-NetFirewallHyperVRule as shown above.

FAQ

How do I enable mirrored networking in WSL2?

Add networkingMode=mirrored under [wsl2] in %UserProfile%\.wslconfig, then run wsl --shutdown and start your distro again. Mirrored mode needs Windows 11 22H2 or later. You can also change WSL settings in the WSL Settings app from the Start menu.

Can WSL2 get an IP address from my LAN’s DHCP server?

Not in the default NAT mode, where WSL2 sits behind a virtual NAT with its own private address. The old bridged mode did this, but Microsoft marks it as deprecated since WSL 2.4.5. Use mirrored mode instead: WSL shares the Windows network interfaces, so other devices reach WSL services at the Windows IP.

How do I access a WSL2 service from another computer on my network?

In mirrored mode, bind the app to 0.0.0.0 and allow the port with New-NetFirewallHyperVRule. In NAT mode, add a netsh interface portproxy rule on Windows plus a Windows firewall rule for the port. Run the portproxy command again whenever the WSL IP changes.

Why don’t my .wslconfig changes take effect?

WSL reads .wslconfig only when the WSL 2 VM starts. Run wsl --shutdown, check with wsl --list --running that nothing is running, then start your distro again. Also check the section: networkingMode, dnsTunneling, firewall and autoProxy go under [wsl2], while hostAddressLoopback and ignoredPorts go under [experimental].

Key Tips

  1. Keep your system updated
  2. Write down your network settings
  3. Use environment variables for sensitive info
  4. Watch your network performance
  5. Keep security in mind

With these settings, your WSL2 network will run smoothly and securely. Remember to test everything thoroughly and adjust settings based on your needs.

whoami

I'm Stefan, a senior software engineer. I build with AI agents every day, and I show how: my workflows, the new tools I test and what breaks along the way. I build apps like XPmetric and ProofSwap, and my own tooling too, like the AI Coding Orchestrator and the pipeline that edits my videos. I started in 2016 with Linux, cybersecurity and Python tutorials. Then AI changed how I work, so the videos changed too. Still a Linux nerd at heart.

More about me

One thought on “WSL2 Network Settings and Configuration Guide”

  1. Why couldn't they have set it up so the virtual network adapter used by wsl got an address from the LAN DHCP server, just like every other device on the LAN?

Comments are closed. I keep the discussion here because the answers still help.