Skip to content

Instantly share code, notes, and snippets.

@aledpardo
Last active December 31, 2021 05:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aledpardo/2ec174325d6ceb2f34ba39bc160df8fc to your computer and use it in GitHub Desktop.
Save aledpardo/2ec174325d6ceb2f34ba39bc160df8fc to your computer and use it in GitHub Desktop.
Fix internet connection in WSL2 when using Cisco AnyConnect VPN

Solving the internet connection in WSL2 when using Cisco AnyConnect VPN

Disclaimer:

The steps described here where copied from this web site: https://jamespotz.github.io/blog/how-to-fix-wsl2-and-cisco-vpn

Steps

All step should be done in the same terminal window.

Step 1

In Windows, open a powershell terminal as Administrator

Run the following commands:

Get-DnsClientServerAddress -AddressFamily IPv4 | Select-Object -ExpandProperty ServerAddresses
Get-DnsClientGlobalSetting | Select-Object -ExpandProperty SuffixSearchList

Step 2

Execute wsl -d <distro>, then run the following commands:

sudo unlink /etc/resolv.conf # this will unlink the default wsl2 resolv.conf
sudo chattr -i /etc/resolv.conf # this will ensure the file is not in read-only mode

# This config will prevent wsl2 from overwritting the resolve.conf file everytime
# you start wsl2
cat <<EOF | sudo tee -a /etc/wsl.conf
[network]
genearteResolvConf = false
EOF

cat <<EOF | sudo tee -a /etc/resolv.conf
nameserver 10.50... # The company DNS/nameserver from the 1st command in step 1
nameserver 10.50... # The company DNS/nameserver from the 2nd command in step 1
nameserver 8.8.8.8
nameserver 8.8.4.4
search this.searchdomain.com # The search domain that we got from 2nd command
EOF
exit

Step 3

Back in powershell terminal, run the following commands:

Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000
wsl --shutdown
Restart-Service LxssManager

Step 4

To confirm you've got internet connectivity in WSL, run:

wsl -d <distro>
curl www.duckduckgo.com

If above doesn't work, check you have connection in your Windows. If the error persists, I have sad news for you...

@c0nsaw
Copy link

c0nsaw commented Jul 15, 2021

Doesnt work for me, corporate network that uses anyconnect, so frustrating. +12 months no fix for the majority of folks, bar wsl --set-version xxx 1

@aledpardo
Copy link
Author

Doesnt work for me, corporate network that uses anyconnect, so frustrating. +12 months no fix for the majority of folks, bar wsl --set-version xxx 1

Oh snap, so in your case the VPN is on corporate network?

yeah, I just figured out that I need to execute the last 2 commands every day, which causes WSL dependent services to crash, like Docker and my WSL distro at use… but yeah trying to make it work… otherwise will stick with GIT Bash

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