Network configuration in WSL2 makes or breaks your development workflow. Let’s look at how to set up and fine-tune your network settings for peak performance.
Table of Contents
- Understanding WSL2’s Network Setup
- Getting Started with Basic Settings
- Setting Up a Fixed IP
- Opening Ports and Access
- Fixing Common Network Issues
- Making It Faster
- Fixing Network Problems
- Working with Development Tools
- Keeping Things Secure
- Key Tips
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.
Getting Started with Basic Settings
First, check your current network setup:
ip addr show
You’ll see two main interfaces:
- eth0: Your primary network connection
- lo: The local loopback
Setting Up a Fixed IP
While WSL2 uses dynamic IPs by default, a static IP can help with development consistency. Edit your WSL config:
sudo nano /etc/wsl.conf
Add these lines:
[network]
generateResolvConf = false
Code language: JavaScript (javascript)
Now set up your network plan:
sudo nano /etc/netplan/01-netcfg.yaml
Add your IP settings:
network:
version: 2
ethernets:
eth0:
addresses:
- 192.168.50.2/24
gateway4: 192.168.50.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
Opening Ports and Access
To use WSL2 services from Windows or other machines, you’ll need port forwarding. Open PowerShell as admin:
netsh interface portproxy add v4tov4 listenport=3000 listenaddress=0.0.0.0 connectport=3000 connectaddress=(wsl hostname -I)
And set up the firewall:
New-NetFirewallRule -DisplayName "WSL2 Port 3000" -Direction Inbound -Action Allow -Protocol TCP -LocalPort 3000
Code language: PHP (php)
Fixing Common Network Issues
DNS Problems
If websites won’t load, try updating your DNS settings:
sudo rm /etc/resolv.conf
sudo nano /etc/resolv.conf
Add these DNS servers:
nameserver 8.8.8.8
nameserver 8.8.4.4
Code language: CSS (css)
Keep WSL2 from changing it:
sudo chattr +i /etc/resolv.conf
Working Behind a Proxy
Add to your shell config (~/.bashrc
or ~/.zshrc
):
export HTTP_PROXY="http://proxy.example.com:8080"
export HTTPS_PROXY="http://proxy.example.com:8080"
export NO_PROXY="localhost,127.0.0.1"
Code language: JavaScript (javascript)
Making It Faster
Memory Settings
Edit %UserProfile%/.wslconfig
in Windows:
[wsl2]
memory=8GB
processors=4
swap=2GB
localhostForwarding=true
Code language: JavaScript (javascript)
Network Speed
Update /etc/sysctl.conf
with:
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_probes = 5
net.ipv4.tcp_keepalive_intvl = 15
Fixing Network Problems
When things go wrong:
- Restart the network:
sudo service network-manager restart
- Restart WSL2:
wsl --shutdown
wsl
- Check host networking:
Get-NetIPInterface | Where-Object {$_.InterfaceAlias -eq 'vEthernet (WSL)' } | Set-NetIPInterface -Forwarding Enabled
Code language: JavaScript (javascript)
Working with Development Tools
For VS Code, update your settings:
{
"remote.WSL.fileWatcher.polling": true,
"remote.WSL.debug": true
}
Code language: JSON / JSON with Comments (json)
Keeping Things Secure
Set up firewalls in both systems:
In WSL2:
sudo ufw enable
sudo ufw allow 3000
In Windows:
New-NetFirewallRule -DisplayName "WSL2 Inbound" -Direction Inbound -Action Allow -Protocol TCP -LocalPort 3000
Code language: PHP (php)
Key Tips
- Keep your system updated
- Write down your network settings
- Use environment variables for sensitive info
- Watch your network performance
- 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.
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?