Windows Subsystem for Linux (WSL) fails to use DNS from a VPN tunnel
12/17/2020 18 People found this article helpful 469,593 Views
Description
Windows Subsystem for Linux (WSL) has a known problem with VPN based DNS. See:
https://github.com/Microsoft/WSL/issues/1350
It appears the WSL BASH shell does not autogenerate the /etc/resolv.conf file correctly, or regenerate it when needed.
The result is that DNS updates provided to the Windows OS, by the establishment of a VPN tunnel, are not recognized in the WSL environment.
This is seen whether the WSL environment is brought up before or after the VPN tunnel is established.
Cause
https://github.com/Microsoft/WSL/issues/1350
Resolution
Use the following shell script to clear this issue. This script should only have to be run once on a particular instance of WSL.
Note: If the 'ed' command is not found, you will need to install it manually (Ex: sudo apt-get install ed).
#!/bin/bash
echo "Modifying namesever for VPN"
vpn() {
[ -s /run/resolvconf/resolv.conf -a -L /etc/resolv.conf ] && sudo cp --remove-destination /run/resolvconf/resolv.conf /etc/resolv.conf
local sudo="sudo"
if [ -w /etc/resolv.conf ]; then sudo=""; fi
$sudo ed -s /etc/resolv.conf <<'EOF'
H
a
#EOF
.
kx
g/^nameserver /d
.-1r !/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command '$x = Get-NetAdapter | Group-Object -AsHashtable -Property ifIndex; Get-DnsClientServerAddress -AddressFamily ipv4 | where {$x[$_.InterfaceIndex].Status -eq "Up"} | Select-Object -ExpandProperty ServerAddresses | foreach {"nameserver " + $_}' | sed 's/\r//g'
'xd
wq
EOF
}
vpn
echo "Done!"
sleep 5
The script may not work properly when pasted into a text editor. If you experience problems with the script, please try the base64-encoded string below.
1. Copy the entire Base64 block and paste it into a blank text editor.
2. Save the text file locally as vpn_script.txt.
3. On the WSL prompt, change directory to where vpn_script.txt was saved. Enter the command: 'base64 -d vpn_script.txt >> vpn_script.sh'.
4. The new file (vpn_script.sh) should contain the script text shown above and should avoid any issues with broken syntax due to new lines, etc.
IyEvYmluL2Jhc2gKCiMgVGhpcyBzY3JpcHQgZml4ZXMgL2V0Yy9yZXNvbHYuY29uZiB0byBhZGRyZX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