Previously, WSL2 allocated a dynamic IP address on each startup using NAT networking, making it difficult to run services predictably or maintain consistent network configurations. With recent updates to WSL, Microsoft introduced reliable support for bridged networking and static IP assignment—eliminating the need for complex workarounds or scripts.
Prerequisites
Ensure your WSL version supports bridged networking:
wsl --version
# Requires WSL version >= 1.0
If the version is outdated, update WSL using:
wsl --update
bước 1: Create an External Virtual Switch (Bridged Network)
Use Hyper-V Manager or PowerShell to create an external virtual switch that bridges WSL2 to your physical network:
Get-VMSwitch -SwitchType External
Record theswitch name (e.g., "Ethernet" or "Wi-Fi") for use in later configuration steps.
Step 2: Configure Global WSL Settings in .wslconfig
Place the file at %UserProfile%.wslconfig (e.g., C:\Users\.wslconfig):
[wsl2]
networkingMode=bridged
vmSwitch=Ethernet # Replace with your switch name
dhcp=false # Disable WSL’s DHCP for IP control
ipv6=true
Then shut down and relaunch WSL:
wsl --shutdown
wsl
ip addr show eth0
Verify that eth0 now shows a bridged network interface with the expected IPv4 and IPv6 addresses (assigned by your router’s DHCP).
Step 3: Assign a Static IP Address (Systemd-based Distro)
For distributions using systemd (e.g., Ubuntu 22.04+), define a systemd-networkd configuration file:
# /etc/systemd/network/10-eth0.network
[Match]
Name=eth0
[Network]
Address=192.168.1.10/24
Gateway=192.168.1.1
DNS=8.8.8.8
DNS=1.1.1.1
LinkLocalAddressing=ipv6
IPv6AcceptRA=true
Create the directory if needed:
sudo mkdir -p /etc/systemd/network
sudo tee /etc/systemd/network/10-eth0.network << 'EOF'
[Match]
Name=eth0
[Network]
Address=192.168.1.10/24
Gateway=192.168.1.1
DNS=8.8.8.8
DNS=1.1.1.1
LinkLocalAddressing=ipv6
IPv6AcceptRA=true
EOF
Reboot WSL and ensure network services are active:
wsl --shutdown
wsl
sudo systemctl enable systemd-networkd systemd-resolved
sudo systemctl restart systemd-networkd systemd-resolved
ip addr show eth0
You should now see your assigned static IP (e.g., 192.168.1.10/24) on eth0.
Step 4: Disable WSL’s Auto-Generated /etc/resolv.conf
Prevent WSL from regenerating DNS settings by editing /etc/wsl.conf (inside the Linux distro):
[boot]
systemd=true
[network]
generateResolvConf=false
Then replace the existing symlink and write your custom DNS:
sudo rm -f /etc/resolv.conf
echo "nameserver 223.5.5.5" | sudo tee /etc/resolv.conf
Optional Enhancements
Enable systemd support: Required for systemctl and modern service management. Disable interop: Prevent Windows binary execution and PATH merging by setting [interop] options to false. Custom hostname: Set a stable hostname via [network] hostname = customname.
Example .wslconfig (Full-Featured)
[wsl2]
networkingMode=bridged
vmSwitch=Ethernet
dhcp=false
ipv6=true
memory=4GB
processors=4
localhostforwarding=true
guiApplications=true
debugConsole=false
Example wsl.conf (Customized)
[boot]
systemd=true
[network]
hostname=MyWSLHost
generateHosts=false
generateResolvConf=false
[interop]
enabled=false
appendWindowsPath=false
[user]
default=developer
After applying changes, restart WSL to verify:
wsl --shutdown; wsl