[WSL 2] NIC Bridge mode 🖧 (Has TCP Workaround🔨)
Issue
WSL 2 seems to NAT it's virtual network, instead of making it bridged to the host NIC. My goal is for a service running in Ubuntu in WSL 2 to be accessible from anywhere on my local network.
Issue Details
-
Your Windows build number:
Microsoft Windows [Version 10.0.18917.1000] -
What you're doing and what's happening: Running
ipconfigon my Windows 10 host machine
IPv4 Address. . . . . . . . . . . : 192.168.1.41
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.1.1
Running ifconfig in Ubuntu WSL 2
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.18.72.60 netmask 255.255.0.0 broadcast 172.18.255.255
Accessing 172.18.72.60 from my Window host does work, however this IP is not accessible from another system on my network.
-
What's wrong / what should be happening instead: I would expect to
ifconfigin Ubuntu WSL 2 to have an IP address in the same network as my host machine. -
Searching the docs: The only detail I have found about WSL 2 networking is the following that notes that it will have it's own IP and that localhost is something that will be used for WSL 2 in the future. https://docs.microsoft.com/en-us/windows/wsl/wsl2-ux-changes This is fine, however I would like the IP WSL 2 is getting to be on my local LAN which means I need to configure the virtual NIC to be bridged.
Workaround
See @edwindijas https://github.com/microsoft/WSL/issues/4150#issuecomment-504209723
Same issue here.
IP address of Windows machine is 192.168.1.95 and the eth0 interface in Ubuntu on WSL2 is getting 172.18.47.17.
I originally had Ubuntu on WSL1 running and then upgraded the installation to WSL2 but same problem. Then I uninstalled Ubuntu altogether and reinstalled fresh but same issue after reinstall.
Prior to using WSL2 I had an instance of Ubuntu setup as a Hyper-V VM on the same machine (Hyper-V NIC bridge already existed).
Same issue. I have a rstudio server running in docker container in WSL2 and wanted to access it from other machine but failed. Really need some fix/idea on it.
I can confirm this issue. I can no longer access the service running in subsystem from my mobile device, which is within the same wifi network with my windows laptop.
It's not a bug with WSL 2, WSL 2 is running as a hyper-v virtual machine. The hyper-v adapter can be found in network adapters. You can use port forwarding to forward the port with netsh as below.
Example command below will forward tcp from port 3000 of the WSL 2 client to port 3000 of the host OS.
netsh interface portproxy add v4tov4 listenport=3000 listenaddress=0.0.0.0 connectport=3000 connectaddress=172.18.28.x
Next allow incoming and outgoing ports on port 3000 in firewall.
It's not a bug with WSL 2, WSL 2 is running as a hyper-v virtual machine. The hyper-v adapter can be found in network adapters. You can use port forwarding to forward the port with netsh as below. Example command below will forward tcp from port 3000 of the WSL 2 client to port 3000 of the host OS.
netsh interface portproxy add v4tov4 listenport=3000 listenaddress=0.0.0.0 connectport=3000 connectaddress=172.18.28.xNext allow incoming and outgoing ports on port 3000 in firewall.
Hi! Did you try it on your end? It didn't work for me when I was trying to access it from another machine in the same network. Although I did work when I use the windows host.
I tried it. It worked but the craziest thing is happening, the ip address is changing on reboot. Don't forget to add inbound and outbound rules. Microsoft will fix this issue in the future
WSL 2 TPC NETWORK FORWARDING
Introduction
With the introduction of WSL 2 Beta, Microsoft has made changes to the system architecture. The changes include changing from the default bridged network adapter to a hyper-v virtual network adapter. The implementation was not completed during the launch of the beta program. This makes accessing of network resources under WSL 2 complex. The work around is to forward the TCP ports of WSL 2 services to the host OS. The virtual adapter on WSL 2 machine changes it's ip address during reboot which makes it tough to implement a run once solution. Also a side note, windows firewall will block the redirected port.
The work around is to use a script that does :
- Get Ip Address of WSL 2 machine
- Remove previous port forwarding rules
- Add port Forwarding rules
- Remove previously added firewall rules
- Add new Firewall Rules
Configuration
The script must be run at login ,under highest privileges to work, and Powershell must be allowed to run external sources.
PowerShell Configuration
Enable power shell to run external scripts, run the command below in power shell with administrative privileges.
How To: Go to search, search for task scheduler. In the actions menu on the right, click on create task. Enter Name, go to triggers tab. Create a new trigger, with a begin task as you login, set delay to 10s. Go to the actions and add the script. If you are using Laptop, go to settings and enable run on power.
Finally: I am no expert at security nor scripting and technically new to the windows OS.
Update The update adds the feature to remove unwanted firewall rules. Here is the script.
$remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '"
$found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
if( $found ){
$remoteport = $matches[0];
} else{
echo "The Script Exited, the ip address of WSL 2 cannot be found";
exit;
}
#[Ports]
#All the ports you want to forward separated by coma
$ports=@(80,443,10000,3000,5000);
#[Static ip]
#You can change the addr to your ip config to listen to a specific address
$addr='0.0.0.0';
$ports_a = $ports -join ",";
#Remove Firewall Exception Rules
iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";
#adding Exception Rules for inbound and outbound Rules
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";
for( $i = 0; $i -lt $ports.length; $i++ ){
$port = $ports[$i];
iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
}
@edwindijas Awesome, that worked for me! 👍 🎉 I can access my running Linux service from any system on my network via my Windows host IP! Thanks for all the detail and Task Scheduler suggestion too.
In addition, this workaround means localhost works too.
I was trying to go down another route by forcing bridge mode of WSL virtual adapter, that didn't work. Just including it here for completeness.
PS C:\WINDOWS\system32> Set-VMSwitch WSL -NetAdapterName 'Name_of_your_phsyical_windows_NIC'
Set-VMSwitch : Failed while adding virtual Ethernet switch connections.
External Ethernet adapter 'Name_of_your_phsyical_windows_NIC' is already bound to the Microsoft Virtual Switch
protocol.
At line:1 char:1
+ Set-VMSwitch WSL -NetAdapterName 'Name_of_your_phsyical_windows_NIC'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-VMSwitch], VirtualizationException
+ FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.SetVMSwitch
@edwindijas Linked your comment as a workaround for this issue in my original post https://github.com/microsoft/WSL/issues/4150#issue-456591548.
The script only opens ports you desired. And the ports are redirected to WSL machine. That said, I am no security expert, if you have a better suggestion, I am open to suggestions.
my method to solve this problem: refresh ip in win10:hosts file
WSL 2 TPC NETWORK FORWARDING
Introduction .... Configuration
The script must be run at login ,under highest privileges to work, and Powershell must be allowed to run external sources.
PowerShell Configuration
Enable power shell to run external scripts, run the command below in power shell with administrative privileges.
I saved your script in a file called "wslbridge.ps1" and then in Windows Scheduler just set Powershell.exe as Action and as argument I wrote this (instead of setting the Unrestricted ExecutionPolicy):
-ExecutionPolicy Bypass c:\scripts\wslbridge.ps1
Thanks
I wrote a Windows Service that automatically writes the WSL2 VM's IP address to the Windows hosts file. You can then just always reference "wsl.local" from your host machine and it will resolve to the WSL2 VM.
I've been using this for a week now and just open-sourced it.
https://github.com/shayne/go-wsl2-host
I've collected a few WSL2 hacks into a repo: https://github.com/shayne/wsl2-hacks
One thing I wanted to point out, relevant to this thread, was "Access localhost ports from Windows", a way to access ports bound to 127.0.0.1 / localhost from the Windows host.
Thanks @edwindijas, it is a great workaround.
For people using Debian, which does not come with ifconfig out of the box, you can try:
$remoteport = bash.exe -c "ip addr | grep -Ee 'inet.*eth0'"
Also, the hint from @gstorelli of using -ExecutionPolicy Bypass inside the arguments of your Task Scheduler script action is awesome!
Thanks, guys!
If there are multi wsl2 in your system, remember use this command on cmd to change the wsl2 which you want to bind these port as the default one, because bash.exe will run command in the default wsl2 environment:
# list the wsl on you system
wslconfig /l
# set Debian as default for example
wslconfig /setdefault Debian
@edwindijas I was hoping to make my Google Chrome (inside my Kali Distro) recognize my Chromecast, but i couldn't find anywhere the actual ports chromecast uses to connect, however now i can access my Kali from anywhere trought SSH.
You can now use localhost to connect in recent WSL2 versions
You can now use localhost to connect in recent WSL2 versions
That's really sort of a different problem -- you can use "localhost" from the host itself, but there's no obvious way to get there from a different machine on the network:
WSL2 is running on Windows host A WSL2 on Windows host A is running server application B You can connect to the application from host A by just doing "localhost:port" You can't connect to the application from Windows Host C in any obvious way.
You can connect to the application from host A by just doing "localhost:port" You can't connect to the application from Windows Host C in any obvious way.
If you open the port, why would you not be able to connect to host A from host C by just doing "host-A-ip:port"?
This behaviour shouldn't be any different then it is for linux.
I don't know the technical reason for it, but it doesn't work by default, and is what brought me here.