Re-enable auto generation of resolv.conf (if disabled)
by commented the disable with
#
sudo nano /etc/wsl.conf
#[network] #generateResolvConf = false
Create the script
sudo nano /bin/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' > /etc/resolv.conf clear echo "VPN networking fix applied per: https://superuser.com/questions/1630487/no-internet-connection-ubuntu-wsl-while-vpn"
Make it executable/run as sudo
sudo chmod +x /bin/vpn-dns.sh echo "$(whoami) ALL=(ALL) NOPASSWD: /bin/vpn-dns.sh" | sudo tee /etc/sudoers.d/010-$(whoami)-vpn-dns
Make it run on wsl startup
echo "sudo /bin/vpn-dns.sh" | sudo tee /etc/profile.d/vpn-dns.sh
Per JHixson's comment, the startup script should include `sudo`. Also added a comment to show this script's been applied (to get over the 6 char edit limit, and so this doesn't get forgotten)
Giacomo1968
- 56.1k
- 23
- 167
- 214
Per JHixson's comment, the startup script should include `sudo`. Also added a comment to show this script's been applied (to get over the 6 char edit limit, and so this doesn't get forgotten)
JohnLBevan
- 860
- 7
- 15
- 34