Getting your WSL2 distro to behave like any other client on your network
Building on Roelofs suggestion no.2, here's what made everything tick in my case. I'm too fresh to just leave a comment unfortunately.
My starting point:
Win 10 Pro
Ubuntu under WSL2
(Docker with Linux containers)
My goal:
Getting an rtmp stream from a client on the network into and back out of an nginx server running on the Ubuntu machine.
Building the bridge
In my case, I could not get Hyper-V to set the bridge up properly. After selecting External network for the WSL switch in the Virtual switch section of Hyper-V Manager and hitting apply, it eventually failed with error 0x80070490. Don't know why and didn't have the time to investigate. WSL was not running and neither was the Docker service.
Instead, I just left the setting on Internal network and bridged the interfaces the manual way, under Network Connections (run->ncpa.cpl). In my case, the WiFi connection and vEthernet (WSL).
Immediately after doing this, I lost internet connectivity and it took me an embarrassingly long time to find out that a reboot was needed. (Windows for once did not ask me to!)
Getting the VM in shape
After the reboot, I now had internet access from the host, the bridge was set to DHCP and had inherited the IP of the WiFi interface (192.168.1.246). Great.
The VM however was still getting the IP of the virtual switch (or however you want to view it, the random 172.x.x.x address that windows seems to assign to the switch as well as the VM).
Firing up Ubuntu, I decided to do a:
sudo ip addr flush dev eth0
Continuing with:
sudo dhclient eth0
threw a handful of errors at me since I was using the vanilla Ubuntu distro from Windows store, no systemd, no fun. Despite that, it did manage to add the IP of the bridge to eth0. As this was not very handy, I got rid of that with:
sudo ip addr del 192.168.1.248/24 dev eth0
but not before taking a sneak peek at the routing table:
user@vm:~$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
After deleting the old IP, I added a unique one from outside of my DHCP range:
sudo ip addr add 192.168.1.50/24 dev eth0
I checked the routing table again and the first entry was gone. At this stage I could ping LAN but not WAN.
Added the entry back with:
sudo ip route add default via 192.168.1.1 dev eth0
Pinging WAN IPs was now possible, but no DNS resolution.
LMGTFM: Adding permanent DNS
In case the solution goes missing, here it is, credit to non-static:
Create a file: /etc/wsl.conf.
Put the following lines in the file
[network]
generateResolvConf = false
In a cmd window, run wsl --shutdown
Restart WSL2
Create a file: /etc/resolv.conf. If it exists, replace existing one with this new file.
Put the following lines in the file
nameserver 8.8.8.8
Or the IP of whatever DNS server you want to use, repeat step 3 and 4.
So, to conclude, check your routing and setup your DNS-conf properly. Unfortunately, the IP settings are reverted every time you restart WSL. If you are not ok with doing this manually every time there are discussions on how to automate it here and here. I haven't had the time to find my favorite.
Best regards
Alexander
dhcp=true
to.wslconf
as wsl eth0 kept the previous IP (for the previous Hyper-v adapter)