Connecting to services running in WSL 2 from external sources can be challenging due to the instances being on a different network. This guide offers a solution to replace the internal virtual switch of WSL 2 with an external version in Windows 20H2 (WSL 2.0) and configure it for better networking control.
This recipe uses a Hyper-V virtual switch to bridge the WSL 2 network, providing improved control and visibility of Windows' network adapters within Ubuntu. The configuration supports both dynamic and static IP addressing, eliminating the need for port forwarding and simplifying network setup.
-
Enable Hyper-V and Management PowerShell Features:
- Open PowerShell as administrator and run:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-Management-PowerShell
- Open PowerShell as administrator and run:
-
Create an External Virtual Switch in Hyper-V:
- Open PowerShell as administrator and run:
New-VMSwitch -Name "External Switch" -NetAdapterName eth0
- Open PowerShell as administrator and run:
-
Modify the WSL Configuration:
- Create or modify the
.wslconfig
file in your user profile directory ($env:USERPROFILE/.wslconfig
) with the following content:[wsl2] networkingMode=bridged vmSwitch="External Switch" dhcp=true ipv6=true
- Create or modify the
-
Enable systemd in the WSL Distribution:
- Edit the
/etc/wsl.conf
file in your WSL distribution and add the following lines:[boot] systemd=true [network] hostname = HOSTAGE generateResolvConf = false
- Edit the
-
Configure Network Addressing:
- For dynamic address configuration, ensure the following is present in
/etc/systemd/network/10-eth0.network
:[Match] Name=eth0 [Network] DHCP=yes
- For static address configuration, use:
[Match] Name=eth0 [Network] Address=192.168.x.xx/24 Gateway=192.168.x.x DNS=192.168.x.x
- For dynamic address configuration, ensure the following is present in
-
Link systemd Resolv.conf:
- Create a symbolic link to link resolv.conf from systemd:
ln -sfv /run/systemd/resolve/resolv.conf /etc/resolv.conf
- Create a symbolic link to link resolv.conf from systemd:
-
Verification:
- Restart the WSL 2 instance and verify the network configuration with:
ip addr show eth0
- Restart the WSL 2 instance and verify the network configuration with: