WSL: Connect to external network
Following post describes how to connect your WSL system to another network. This is useful for cases like developing web applications and testing them on other devices on your local network. By default WSL is only connected to the host via an internal virtual network.
Following steps were tested on WSL2 using the HyperV backend running Debian.
1. Configure HyperV Virtual Switch
Open Hyper-V Manager
as admin and click on Virtual Switch Manager...
to the right. Now select the WSL virtual switch and change it’s connection type to “External Network” and select the network interface to connect to.
2. Setup WSL Network
Now WSL should be connected to the network of your choice but the IP addresses are likely not valid. I needed an IP address in the 192.168.0.x
network while WSL assigned an address in the 192.168.177.x
network. To fix this issue run following commands to remove assinged ip addresses, set a new address and finally assign a default gateway:
sudo ip a flush dev eth0
sudo ip a add 192.168.0.116/24 dev eth0
sudo ip r add default via 192.168.0.1
sudo service networking restart
Now WSL can access/be accesed by other devices on the local network and access the internet just like other devices on the network.
Be aware that this configuration is not persistent and lost after a reboot. I just added the commands to my .bashrc
which isn’t perfect but good enough for my WSL development environment.
Troubleshooting
Temporary failure resolving …
Check /etc/resolv.conf
for valid nameservers.
No network connection
Resetting wsock32.dll can help:
wsl --shutdown
netsh winsock reset
Resetting registry, proxy and dns settings can also fix the issue:
netsh int ip reset all
netsh winhttp reset proxy
ipconfig /flushdns
If all that doesn’t help and you double checked your network setup try rebooting the host system or disabling/enabling network interfaces.