Fix WSL DNS issue on VPN

1 minute read

Re-enble auto-generation of resolv.conf (if disabled)

by commented the disable with #

sudo vi /etc/wsl.conf
#[network]
#generateResolvConf = false

Create the script

sudo vi vpn-dns.sh

#!/bin/bash

echo "Getting current DNS servers, this takes a couple of seconds"

/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command '
$ErrorActionPreference="SilentlyContinue"
Get-NetAdapter -InterfaceDescription "Cisco AnyConnect*" | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
Get-NetAdapter | ?{-not ($_.InterfaceDescription -like "Cisco AnyConnect*") } | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
' | \
        awk 'BEGIN { print "# Generated by vpn fix func on", strftime("%c"); print } { print "nameserver", $1 }' | \
        tr -d '\r' | sudo tee /etc/resolv.conf

Make it executable/run as sudo

sudo chmod +x vpn-dns.sh

Run as superuser

sudo vpn-dns.sh

Set interface metric in admin PowerShell

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

It can be restored:

Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 1

Sources: https://www.frakkingsweet.com/automatic-dns-configuration-with-wsl-and-anyconnect-client/ https://stackoverflow.com/questions/66444822/wsl-2-dns-is-not-working-with-vpn-connection-on-win-10