WSL: Windows Defender Firewall blocks access from WSL2
-
Your Windows build number: 10.0.18917.1000
-
What you’re doing and what’s happening: Trying to run X11 apps from my Ubuntu installation, which I just upgraded from WSL1 to WSL2. I’ve configured the X server (VcXsrv) to accept TCP connections, and I’ve put the IP address of the Windows host into the DISPLAY variable, but the connection times out. Digging into it, I’ve discovered that the vEthernet adapter is treated as an “Unidentified Network”, and so it gets the Public firewall access rules (which, unsurprisingly, includes blocking port 6000). If I disable the firewall entirely, I can connect to the X server just fine. This worked under WSL1, of course.
-
What’s wrong / what should be happening instead: Connections from a WSL2 VM should be treated as privileged and not subject to firewall rules, probably?
-
Strace of the failing command, if applicable:
$ strace xev
execve("/usr/bin/xev", ["xev"], 0x7ffeee6e64a0 /* 21 vars */) = 0
brk(NULL) = 0x55feb7684000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
[cut for brevity]
close(3) = 0
socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC, IPPROTO_TCP) = 3
setsockopt(3, SOL_TCP, TCP_NODELAY, [1], 4) = 0
setsockopt(3, SOL_SOCKET, SO_KEEPALIVE, [1], 4) = 0
connect(3, {sa_family=AF_INET, sin_port=htons(6000), sin_addr=inet_addr("172.17.252.209")}, 16 [hangs here...]
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 88
- Comments: 71
I found the following to work for me: Open windows firewall settings, and remove the vEthernet (WSL) connection for each of the Domain, Private, and Public profiles:
Instead of talking about all kinds of workarounds, why couldn’t WSL2 just fix this (by providing some way to mark the wsl-host network as private)?
Just chiming in that it was very unintuitive to figure out that the network being created was marked public and that’s why I couldn’t actually access host services due to the firewall. I’d expect it to be marked private.
Inlining here since this issue ended up being the landing zone. Props go to @dansanduleac
I have the same issue. It would also be nice if it were possible to change the network profile of the vEthernet adapter that WSL2 creates to Private. This way, we could at least disable the firewall for private networks. I tried doing this in PowerShell, but PS claims the adapter doesn’t exist (I’m using the correct alias):
upon reboot, I disable the firewall on the WSL2 interface with an Administrator Powershell:
this only works after I’ve launched the X client that is trying to talk to my VcXsrv
I launch my X client (xterm) with a desktop shortcut whose “Target” is set to:
Where wslrun.sh is:
Tedious parts of this:
I want to connect from WSL2 to X410. And this works currently: Check
Settings->Firewall->Advance Settings. In the opened window, check in-site rules, you can find TCP & UDP rules of X410. You can double click each rule, switch to advance tab in the dialog, check on all three checkboxes: domain, private, universal. This changes the profileprivatetoall.For xeyes demo:
I’ve struggling to understand why this is still an issue after 3 years of being open. What the hell is going on at Microsoft? My company is now telling me that I can’t use WSL2 precisely because they want to enforce a policy of not allowing Public domain firewalls to be disabled. I’m sure I’m not the only person in that boat.
Can we have some kind of update please. @craigloewen-msft - Is there anybody else that could be added to this issue to get some traction?
I also have a version of this problem with some insteresting symptoms.
I have 2 users on the same machine, each with their own WSL2 instance. User 1 can make TCP from linux to windows via the 127.0.0.1 address without disabling the firewall, and user 2 (me!) cannot, even if I use the machinename.local DNS.
One option was disabling the firewall on the Public profile (BAD!).
My GPOs stop me changing adapter specific settings on the default firewall profile so the solution described above was not working for me.
However I was allowed to change the protected network connections on just the Public profile for user 2, and that made it work without opening everything up. Cannot explain why 2 users on the same machine have different behaviour but at least now both can work.
In summary - you only need to modify the public profile, not all/default profiles for WSL to be unblocked
This is insanely dumb. Why on earth would they choose to set a PURELY INTERNAL NETWORK as “Public” on the firewall settings?? This network has the same level of trust as starbucks wifi. It should be a “Private” network BY DEFAULT. ughh
I am also using this as the working solution but this will result in Windows yelling at you that you no longer use the recommended firewall configuration. Anyone with a solution for that? Also this does not save across reboots.
Note that if you have a machine (like my work-managed laptop) that has local firewall rules turned off by GPO…
…then the following won’t work for you:
New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action AllowYou can (I think) infer this by the following line from the result from Net-NetFirewallRule: “EnforcementStatus: NotApplicable”
You should get the equivalent rule added by your domain administrators… maybe create an OU for power-users or developers etc.
But when you can’t (yet) get it done via appropriate firewall policy, the following will work, with the priviso that Windows Defender will raise this as an insecurity.
Set-NetFirewallProfile -DisabledInterfaceAliases "vEthernet (WSL)"Regarding this option:
@avzero07 asked:
You can check that the rule only applies to the named interface using this command:
Output should be:
(Just to be sure I tested to connect to an open port on my PC from my phone, and indeed the created rule does not open the ports for my main network interface, it only opens the ports for the WSL interface.)
@kppullin mentioned:
Yes, sadly after a reboot the rule doesn’t work anymore:
printed
after I rebooted.
The rule can be modified to work again after reboot, no need to recreate:
Automate adjusting of firewall rule (only works if you have admin rights):
Create a task in the windows taskscheduler (taskschd.msc), that runs as your useraccount, "Run only when user is logged on", "Run with highest privileges":The task doesn't need any triggers, and the action should be: The command uses
cmd /c start "" /minto avoid a flashing window. Note the carret (^) before the pipe to ensure that cmd doesn't interpret the pipe for itself and instead sends it to powershell.After this preparation add to your
~/.bashrcto run the task at each execution of WSL.If even after creating the rule a connection can’t be established, ensure that no blocking rules accidentally exist.
If an application tries to open a port, you typically get a message like this (sorry for the german localization of Windows 🤷♂️):
After accepting that dialog, Windows will not only create allow rules for the selected profiles, but also block rules for the not selected profiles:
You need to delete those created block rules (in
wf.msc), otherwise the connection is refused because these block rules are more important than our created “WSL” allow rule.Note: even if you pressed “Cancel” in the firewall question dialog, the block rules will be created…
Confirmed adding the firewall rule and limiting the source IP scope range works. Functional and secure!
@rmja excellent. I think what you propose here is the best solution! You can just uncheck vEthernet instead of configuring a risky inbound firewall rule.
@therealkenc This has had the status “investigation required” for 2.5 years. How is… the investigation going? Any chance this is fixable? Is WSL2 fundamentally incompatible with Windows networking? There are dozens of cross-referencing issues, this is a real problem for so many people.
I believe that indicating the network as Private could resolve the issue, at least partially, if that’s a possibility at all. The VM switch adapter connection profile is not exposed through WMI, so changing its profile the “normal” way, with
Set-NetConnectionProfile, is impossible. Other Internal VM switches are visible, only not the WSL’s one.The second row is a Hyper-V internal switch, visible in Control Panel/Networking, but the WSL one isn’t. It’s interesting that both are visible in the long output of
Get-VMSwitch | fl *, also returned through WMI(!); I compared them, no differences. The network interface is not visible in Device Manager either, although the Hyper-V switch is, as “Hyper-V Virtual Ethernet Adapter #2”. I don’t understand what is really going on.Since I just got bumped on email about this issue, I’d like to point everyone to the fact that vSwitch can now be used to directly connect WSL2 distro, officially, no hacks, using “networkingMode=bridged”. You do need W11 Pro, which shouldn’t be an issue. I’ve compiled a nice big tutorial couple of days ago as a comment in main networking thread: https://github.com/microsoft/WSL/issues/4150#issuecomment-1288152162
Likewise added it to my GitHub: https://github.com/luxzg/WSL2-fixes
I went through W10 to W11 upgrade (linked instructions for those with “unsupported” hardware), WSL2 Preview, creation of vSwitch, configuration of a bridge, and as a bonus systemd configuration on Ubuntu with complete systemd based networking setup. It should be doable for both newbies and experienced users. Btw, I can now tear up and setup new WSL2 instance with proper networking and full apt upgrades and all in less than 10 minutes, easy-peasy.
My missing piece was the firewall Public Profile Firewall State Inbound Connections was set to “Block all connections”. After changing this to “Block (default)” adding the rules below using PowerShell as Administrator allowed WSL connections in and out.
New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action AllowNew-NetFirewallRule -DisplayName "WSL" -Direction Outbound -InterfaceAlias "vEthernet (WSL)" -Action AllowEDIT: Note that this basically turns off the firewall for WSL - use with caution! For more granular access you can define the specific ports you want to allow in and out with the New-NetFirewallRule PowerShell cmdlet.
Agree though that ability to set
vEthernet (WSL)to Private net connection profile would be the ideal workaround/real fix…If the profile was set to Private by default, none of his hokey firewall business would be required. And considering its a NAT Interface behind the main host networkk(s), it’s certainly a private network.
In the interim, it would be nice to set it manually as a Private Interface, but on Windows 10 Home, it doesn’t show up in the list of adapters in the Control Panel and does not show up in a Get-NetConnectionProfile cmdlet call in PowerShell. The only thing it shows up in is an
ipconfig /allcall.@rmja and @avzero07, is it not the same as
Set-NetFirewallProfile -DisabledInterfaceAliases "vEthernet (WSL)"from above in this thread?Between this and
New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action Allow(also from this thread) I’d pick the latter because it doesn’t result in the alerted Windows Firewall icon in the systray.Ideally, it’d be great to have a separate Network Profile (under
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles) forvEthernet (WSL), which we could then set toPublicorPrivatesimilar to what we can do forWiFi.I’ve been looking at this, but I haven’t figured out yet how to create a custom Windows Network Profile and assign an interface to it.
Updated: following this similar Docker issue, I’ve tried adding
*NdisDeviceTypeto thevEthernet (WSL)registry key underHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class. The goal was make it appear under a separate Windows network profile, so I could useSet-NetConnectionProfile -InterfaceAlias 'vEthernet (WSL)' -NetworkCategory Privateto make it private.Sadly, that did not work and
*NdisDeviceTypedisappeared after a reboot.Then I tried to create a new profile manually:
New-CimInstance -Namespace root/StandardCimv2 -ClassName MSFT_NetConnectionProfile -Property @{Name="WSL"}, so I could try addingvEthernet (WSL)to it somehow. This produced an error:The requested operation is not supported.WSL may pick the ip range
192.168.0.0/16too:This firewall setting should cover all case
Allowing traffic for Public networks via firewall rules works as described by @faymek, But it’s kind-of working on my nerves security-wise.
So I tried to make the network private using the tricks mentioned here and here, but to no avail. Even after disabling and re-enabling the device (vEthernet (WSL) / Hyper-V Virtual Ethernet Adapter) the firewall still blocks it. And rebooting the host recreates the keys in the registry. I even tried protecting the keys with access permissions, but Windows can still overwrite them on reboot.
I’ve found that this configuration does not persist across Windows reboots. Actually I think the rule does persist, it just stops working and I have to create another copy of the rule. Is this a me problem or more general? Running the regular, non-insider 2004 build.
Same issue here. Have to not use the block all incoming firewall setting on public networks, otherwise WSL2 is broken when accessing the internet.
Microsoft: please let us designate WSL2 as private.
I created automated solution to this it seems… creating a run-as-admin shortcut that disables the FW for WSL interface and this shortcut is run when opening WSL console.
I think (but can’t prove) that is because the WSL interface is transient as opposed to a permanent fixture of a WSL install. The hangout for that is open issues #4467 #4210 et al.
Because Windows randomly turns on the firewall without your acknowledge, enforce a cron job to disable it might be the only compromised solution.
(I know basically nothing about windows powershell, so please let me know if I’m wrong.) Also don’t forget to enable inbound rules on the “Windows” host side.
WSL2 already supports systemd and can start the sshd service, so we can use the port forwarding function of ssh to let WSL2 access the host port. like this:
Just found a workaround that seems to allow this.
First, install Hyper-V Manager if necessary. This is purely to get access to the PowerShell tools it provides to manage VM switches.
Run
Set-VmSwitch "WSL" -SwitchType Internal. This converts the switch from a private VM switch to an internal one. It also makes it appear in Network Connections in Control Panel, as well as appearing when you runGet-NetConnectionProfilein PowerShell. You’ll see that for vEthernet (WSL) NetworkCategory is listed as Public.Manually assign an IP to the adapter via Control Panel, something in the 172.XX.XX.1 range, and make sure that your WSL install is on the same subnet.
Run
Set-NetConnectionProfile -InterfaceAlias "vEthernet (WSL)" -NetworkCategory Private. You should now be able to ping the host machine from inside the VM without needing to modify any firewall rules.Slight disclaimer: I’ve literally only just discovered that this works, so haven’t tested it extensively, but it seems to be able to reliably access the Internet and the host machine.
EDIT: Unfortunately I’ve just found that it doesn’t survive a reboot. Ah well.
FWIW I’m on a Windows Hello for Business machine (non-domain-joined - Azure AD joined only w/ Intune etc. – so it’s not using Group Policy) and I was able to adjust the Group Policy setting for merging local firewall policies alone to get things working. I’ve rebooted the machine and it’s persisted and still working.
I realize this may not apply for everyone if they’re domain-joined and a scheduled Group Policy enforcement swings around and explicitly resets this to “No,” but wanted to throw it out there. You may be surprised and find out that it’s not explicitly set to “No” by Group Policy and that it’s actually set to “Not Configured,” so you may be able to set it explicitly, if not through the Group Policy UI then perhaps through the registry directly.
This is the registry key and property:
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\WindowsFirewall\PublicProfileAllowLocalPolicyMerge: 1To set via PowerShell:
At the very least if that doesn’t fix it entirely, it should at least open up the ability to add a local firewall rule to do so. (As a side note: I wonder if fully opening up the firewall is necessary or if you just need select ports – really port 53 UDP was the issue for me for DNS resolution which is mentioned in other issues.)
This needs to be bumped because I spent way too long spelunking for a fix for this and this was it.
https://github.com/microsoft/WSL/issues/4585#issuecomment-610061194 Might be helpful for solving firewall problem.
For those of you that cannot for whatever reason install Windows 11, its now possible to run WSLg (https://github.com/microsoft/wslg) in WSL2 on Windows 10 builds that have a UBR>= 2311. So, if you are reading this issue because you are trying to run Linux GUIs in Windows using an X-11 server like VcxSrv installed in Windows, then one alternative is to use WSLg instead.
This is solved all the problems I was personally having as I now do not have to disable the Defender firewall for the WSL2 connection in order to get the GUI processes in WSL2 to be able to connect to the X-11 port running in Windows. That in turn allows me to connect to my corporate VPN (which otherwise blocked dues to security policies related to firewall settings).
If you don’t have the .2311 build of Windows 10, then install all the latest updates from Windows Update. You need to be updated to Version 22H2. I took the advice from here - https://superuser.com/questions/1754138/running-wsl-that-was-installed-from-the-microsoft-store-results-in-windows-vers. Note if that doesn’t get you to UBR 2311, then you may need the preview update https://support.microsoft.com/en-gb/topic/november-15-2022-kb5020030-os-builds-19042-2311-19043-2311-19044-2311-and-19045-2311-preview-237a9048-f853-4e29-a3a2-62efdbea95e2.
I actually couldn’t get that via Windows Update - probably because it’s a preview release, but I found I could install it manually by downloading from https://www.catalog.update.microsoft.com/Search.aspx?q=KB5020030. Do this at your own risk though - it’s a preview release. People seem to think it will be out of preview in a few months, so you might want to wait and use Windows Update.
After that - I rebooted and ensured my DISPLAY env variable setting was set to :0, and WAYLAND_DISPLAY was set to wayland-0 (the required values for WSLg) and WSLg started serving my Linux GUIs!!
Hope you find this helpful!
Wakaka, you are the ONE. Thx.
This works, even persists after a reboot. Basically what was already suggested, but using the interface name instead of IP (the IP changes, the interface name does not).
Run this in an elevated PowerShell:
New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action AllowCredit to @dansanduleac in this comment.
@QingGo You just need to edit the TCP rule for “VcXsrv windows xserver” from Block to Allow, no need to disable and create a new rule. I have successfully used this approach to allow WSL 2 connect to a Postgres SQL installed on Windows.
P/S: can someone enlighten me why we limit the IP range to 172.16.0.0/12 ?
Just a note, the settings from the above comment work for me, but I had trouble finding them: control panel > system and security > windows defender firewall > advanced settings > inbound rules > edit the vcxvsrv rule for public networks.
Also, since the hostname is in the DNS forwarder, you can do
to set the display
Hi ! Please try the latest networking features that we’ve added in WSL. Those should greatly improve connectivity.
If the issue still remains, please reopen this issue.
My solution:
run_in_background.vbs:disable-wsl-public-profile.bat:finally something what works also for me. yes it was need of rules for firewall… i will borrow it… thx
Windows firewall ultimately makes me less secure. I cannot apply a rule that applies only to the WSL network adapter, which means the only way to make this work is either manually check and add the specific ipv4 /32 (why no IPv6 support?), or create a general rule for 172.19.240.0/20 applied to networks of type public (and hope that when I am roaming that the foreign LAN does not use those addresses, otherwise I am left vulnerable), and interface type local area network. It would be much more useful if I could apply a rule that applied to the specific WSL network adapter.
Everything is OK if I disable the firewall for the public network.
However, I tried both
New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action Allowand @rmja 's solution, none of them work.Get-NetFirewallInterfaceFilter -AssociatedNetFirewallRule (Get-NetFirewallRule -DisplayName "WSL")gives meInterfaceAlias : vEthernet (WSL)Do you have any suggestions?
I inspected the “WSL” rule using the Windows Defender Firewall application and it seems like an allow-everything type of inbound rule. Even if you try creating a new rule via the wizard in the Widows Defender Firewall application, it doesn’t give you any option to choose the adapter.
I could be wrong but it seems like the -InterfaceAlias is not doing anything in that PS command and all we’re setting is up is a very generic inbound rule that allows all traffic inbound (seems very unsafe).
Could someone please explain how we can confirm that the rule which is set up using that command is specifically tied to inbound connections coming in through the vEthernet adapter?
The following workaround works for me:
Set-NetFirewallProfile -Name $(Get-NetConnectionProfile).NetworkCategory -DisabledInterfaceAliases $(Get-NetAdapter | Where-Object Name -like ‘WSL’).Name
Cross WSL 2 distro access via ports published to Windows host should work bypassing all firewalls. Am I right? In the case of Docker Desktop Kubernetes integration using HTTP port 6443 works well. Is it a firewall issue or something else, like of difference between Kubernetes and Docker proxy implementation?
Hmm if you add that range, you get most home networks, and there’s some chance of an attack via a compromised printer or whatever. Granted, very unlikely but not impossible.
The rule should only allow connections from the local pc.
For the record, I added some more surgical options and documented here:
https://github.com/cascadium/wsl-windows-toolbar-launcher#firewall-rules
For me though the ideal solution would be for this to work:
https://github.com/microsoft/WSL/issues/4619
@sandric: I wrote this PS script to deal with the WSL2 IP address changing on reboot:
https://github.com/paul-reilly/WSL2-Xming-Init
It works with firewall active on my machine at least.