Skip to content

Instantly share code, notes, and snippets.

@mikegerber
Last active April 28, 2025 12:35
Show Gist options
  • Save mikegerber/91fcea262028e09b2fd0969193c6c260 to your computer and use it in GitHub Desktop.
Save mikegerber/91fcea262028e09b2fd0969193c6c260 to your computer and use it in GitHub Desktop.
Fix WSL2 vs VPN networking

The problem

WSL2 uses a random network from the 172.16.0.0/12 RFC1918 private IP address block. And our VPN uses that address block, too, with a route metric of 1 (= most preferred.)

This breaks networking for WSL2. Meh!

The solution

While messing around with the interface/route metric of the VPN network may work around the problem, it also reduces the priority of the VPN. We do not really want this. Additionally, changing the interface metric does not seem to be permanent, so it requires more work when it breaks again.

A better solution is configuring WSL2 to not use a network in the VPN network space at all. However, in our case, the VPN routed all the available RFC1918 address space... (Isn't IPv4 great!)

But we can use the link-local address space from 169.254.0.0/16 and so have at least a semi-elegant and permanent solution!

  1. These PowerShell commands set the NAT network used by WSL2 to a subnet of 169.254.0.0/16 - I chose 169.254.214.0/24 here - and need to be run as a Windows administrator:
Set-ItemProperty `
  -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss `
  -Name NatNetwork `
  -Value "169.254.214.0/24"
Set-ItemProperty `
  -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss `
  -Name NatGatewayIpAddress `
  -Value "169.254.214.1"
  1. Reboot (I couldn't be bothered to check if restarting some service suffices.)

  2. After the reboot, you a. should get an error message the first time you start your WSL2 (because it can't use the IP it used before the change) and b. networking should work, now with shiny new 169.254.x.y addresses.

Notes

  • The only thing that makes this "semi-elegant" is that I would prefer using a network from RFC1918.
  • To check the current values, run Get-Item -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss.
  • I've also seen DNS break a lot and would recommend checking IPv4 connectivity through the WSL2 NAT without DNS first (e.g. ping -n 8.8.8.8 or similar), then fixing DNS, if needed. My WSL just auto-configured 169.254.x.1 in /etc/resolv.conf, and that worked here. So WSL2 seems to have a built-in DNS proxy, but I couldn't find any documentation on it.
  • Our VPN set up does not route all traffic through it, so this might be not be a complete solution in that case. It would be interesting to see how a Cisco AnyConnect VPN with default route to the VPN sets this default route - what metric does the route have?
@ddamerjian
Copy link

I really need help here

@ddamerjian
Copy link

Can someone step up to the plate and help me please

@mikegerber
Copy link
Author

@ddamerjian Check any wsl.conf (in the WSL distribution) or .wslconfig files, if you have anything network-related configured there. (I'm on Windows 10, you are on Windows 11, which might make a difference, can't say.)

@ddamerjian
Copy link

thanks for the response.

the only wsl.conf i found is the one i have been modifying in testing different solutions for this issue, and currently it looks like the following where i was taking a suggestion from a post to use mirrored feature which doesnt work. But if i dont have that setting the problem still exists

[network]
generateResolvConf = True
[experimental]
networkingMode=mirrored
dnsTunneling=true
cisco@LAPTOP-L0MJCF72:~$

As far as the other file you mentioned, .wslconfig, no such file exists on my system

Thanks
dd

@mikegerber
Copy link
Author

networkingMode=mirrored

With mirrored mode the NAT config I propose here probably isn't relevant.

@ddamerjian
Copy link

right I know that, I already acknowledged that, what I am saying is that I tried your approach without that setting and it still doesnt work

@ddamerjian
Copy link

Come on guys I really really need your help and to step up to the plate and help me. I absolutely need access within Cisco, its at the point that well, I may have to stick with my hardware (Meraki) VPN which DOESNT WORK with T-Mobile Fixed Wireless Access which I am using and I might have to revert to FIOS which does allow me to use my hardware VPN, but FIOS is more expensive and TMO is the way to go. Please help me, I'll setup a Webex session if youre free, anyone, please.

@mark-os
Copy link

mark-os commented Jan 6, 2025

@ddamerjian did you figure it out?

@ddamerjian
Copy link

No I did not, if I had I would have mentioned it. Im high and dry.

@mark-os
Copy link

mark-os commented Jan 14, 2025

@ddamerjian I checked my config and it turns out I can only get this working with wsl-vpnkit

Have you tried that?

@ddamerjian
Copy link

ddamerjian commented Jan 14, 2025

wow, wow, wow, it actually worked, I dont believe it. I cannot thank you enough for finding this solution!!!!!

So the way it appears to work is that you have to keep the script running in the Powershell for it to continue to work, as soon as you stop the script the connectivity stops working. All good

PS C:\Users\ddamerji> wsl.exe -d wsl-vpnkit --cd /app wsl-vpnkit
+ VPNKIT_GATEWAY_IP=192.168.127.1
+ VPNKIT_HOST_IP=192.168.127.254
+ VPNKIT_LOCAL_IP=192.168.127.2
+ TAP_MAC_ADDR=5a:94:ef:e4:0c:ee
+ VMEXEC_PATH=/app/wsl-vm
+ GVPROXY_PATH=/app/wsl-gvproxy.exe
+ TAP_NAME=wsltap
+ CHECK_HOST=example.com
+ CHECK_DNS=1.1.1.1
+ DEBUG=0
+ set +x
+ WSL2_TAP_NAME=eth0
+ WSL2_GATEWAY_IP=172.29.96.1
+ '[' 0 -eq 0 ]
+ set +x
starting vm and gvproxy...
INFO[0000] waiting for packets...
time="2025-01-14T11:12:10-05:00" level=info msg="waiting for clients..."
time="2025-01-14T11:12:10-05:00" level=info msg="new connection from remote to 66840"
started vm and gvproxy
check: ✔️ ping success to IPv4 WSL 2 gateway / Windows host (172.29.96.1)
check: ✔️ ping success to IPv4 Windows host (192.168.127.254)
check: ✔️ ping success to IPv4 gateway (192.168.127.1)
check: ✔️ nslookup success for example.com A using 192.168.127.1
check: ✔️ nslookup success for example.com A using 172.29.96.1
check: ✔️ nslookup success for example.com A using 1.1.1.1
check: ✔️ ping success to IPv4 external host domain (example.com)
check: ✔️ ping success to IPv4 external host IP (1.1.1.1)
check: ✔️ nslookup success for example.com AAAA using 192.168.127.1
check: ✔️ nslookup success for example.com AAAA using 172.29.96.1
check: ✔️ nslookup success for example.com AAAA using 1.1.1.1
ping: bad address 'example.com'
check: ➖ ping fail to IPv6 external host (example.com)
check: ✔️ wget success for http://example.com
check: ✔️ wget success for https://example.com

There is also an option to Setup as a standalone script within Linux, I assume bypassing the need for the Powershell script, but it failed at the following step maybe bc the required file is not in that site I dont know, no big deal as most importantly I got it to work as documented.

cisco@LAPTOP-L0MJCF72:~$ wget https://github.com/sakai135/wsl-vpnkit/releases/download/$VERSION/wsl-vpnkit.tar.gz
--2025-01-14 10:52:24--  https://github.com/sakai135/wsl-vpnkit/releases/download/v0.4.x/wsl-vpnkit.tar.gz
Resolving github.com (github.com)... 140.82.113.4
Connecting to github.com (github.com)|140.82.113.4|:443... connected.
HTTP request sent, awaiting response... 404 Not Found
2025-01-14 10:52:24 ERROR 404: Not Found.
cisco@LAPTOP-L0MJCF72:~$

@jecsanb
Copy link

jecsanb commented Apr 9, 2025

wow, wow, wow, it actually worked, I dont believe it. I cannot thank you enough for finding this solution!!!!!

So the way it appears to work is that you have to keep the script running in the Powershell for it to continue to work, as soon as you stop the script the connectivity stops working. All good

PS C:\Users\ddamerji> wsl.exe -d wsl-vpnkit --cd /app wsl-vpnkit
+ VPNKIT_GATEWAY_IP=192.168.127.1
+ VPNKIT_HOST_IP=192.168.127.254
+ VPNKIT_LOCAL_IP=192.168.127.2
+ TAP_MAC_ADDR=5a:94:ef:e4:0c:ee
+ VMEXEC_PATH=/app/wsl-vm
+ GVPROXY_PATH=/app/wsl-gvproxy.exe
+ TAP_NAME=wsltap
+ CHECK_HOST=example.com
+ CHECK_DNS=1.1.1.1
+ DEBUG=0
+ set +x
+ WSL2_TAP_NAME=eth0
+ WSL2_GATEWAY_IP=172.29.96.1
+ '[' 0 -eq 0 ]
+ set +x
starting vm and gvproxy...
INFO[0000] waiting for packets...
time="2025-01-14T11:12:10-05:00" level=info msg="waiting for clients..."
time="2025-01-14T11:12:10-05:00" level=info msg="new connection from remote to 66840"
started vm and gvproxy
check: ✔️ ping success to IPv4 WSL 2 gateway / Windows host (172.29.96.1)
check: ✔️ ping success to IPv4 Windows host (192.168.127.254)
check: ✔️ ping success to IPv4 gateway (192.168.127.1)
check: ✔️ nslookup success for example.com A using 192.168.127.1
check: ✔️ nslookup success for example.com A using 172.29.96.1
check: ✔️ nslookup success for example.com A using 1.1.1.1
check: ✔️ ping success to IPv4 external host domain (example.com)
check: ✔️ ping success to IPv4 external host IP (1.1.1.1)
check: ✔️ nslookup success for example.com AAAA using 192.168.127.1
check: ✔️ nslookup success for example.com AAAA using 172.29.96.1
check: ✔️ nslookup success for example.com AAAA using 1.1.1.1
ping: bad address 'example.com'
check: ➖ ping fail to IPv6 external host (example.com)
check: ✔️ wget success for http://example.com
check: ✔️ wget success for https://example.com

There is also an option to Setup as a standalone script within Linux, I assume bypassing the need for the Powershell script, but it failed at the following step maybe bc the required file is not in that site I dont know, no big deal as most importantly I got it to work as documented.

cisco@LAPTOP-L0MJCF72:~$ wget https://github.com/sakai135/wsl-vpnkit/releases/download/$VERSION/wsl-vpnkit.tar.gz
--2025-01-14 10:52:24--  https://github.com/sakai135/wsl-vpnkit/releases/download/v0.4.x/wsl-vpnkit.tar.gz
Resolving github.com (github.com)... 140.82.113.4
Connecting to github.com (github.com)|140.82.113.4|:443... connected.
HTTP request sent, awaiting response... 404 Not Found
2025-01-14 10:52:24 ERROR 404: Not Found.
cisco@LAPTOP-L0MJCF72:~$

Mine gets stuck at the set +x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment