Why is there no network connectivity in Ubuntu using WSL 2 behind VPN?

I had similar issues with Cisco AnyConnect. I think WSL1 does it's networking through Windows (via Hyper-V?) and WSL2 is effectively a Linux Kernel running virtualised alongside Windows.

My fix was

  1. In Windows ipconfig /all get the DNS values for the VPN adaptor
  2. In WSL sudo vi /etc/resolv.conf and add the DNS values as lines nameserver <DNS IP>

You can optionally add the block below to /etc/wsl.conf to stop resolv.conf being "refreshed" (wiped) on restart. But you need to remember it's not being automatically updated in future.

   [network]  
   generateResolvConf = false   

As for why you had no network connection when VPN was connected, I think it was trying to connect to the "off VPN" DNS which your VPN was blocking. There seem to be some GitHub issues around WSL2 and VPNs so I'd expect a fix to come out at some point.


I used ubuntu_20 and WSL2 and I did the following steps to fix the issue:

  • Run Powershell as administrator

    Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 4000  
    Get-NetIPInterface -InterfaceAlias "vEthernet (WSL)" | Set-NetIPInterface -InterfaceMetric 1
    
  • Find nameservers (command at windows powershell)

    ipconfig /all
    

    in my case I found values : 10.150.54.19 and 10.158.55.13

  • Make permanent changes to dns by edditing /etc/wsl.conf (inside WSL instance)

    [network]
    generateResolvConf = false
    
  • shutdown WSL2 (command at PowerShell)

    wsl --shutdown
    

    afterwards open new terminal window with wsl

  • delete and create new /etc/resolv.conf file

    sudo rm /etc/resolv.conf
    sudo nano /etc/resolv.conf
    
  • add the following at the resolv.conf

    nameserver 8.8.8.8
    nameserver 10.150.54.19
    nameserver 10.158.55.13 
    
  • the first step (two powershell commands) need to be executed every time we are connected to the vpn


There's an easy workaround that worked for me on Windows 10 with WSL2+Ubuntu 20.04 and Cisco AnyConnect.:

Launch WSL before connecting to the VPN:

wsl --shutdown
# disconnect VPN
wsl
# connect VPN again