Skip to content

Instantly share code, notes, and snippets.

@thebiss
Last active April 26, 2021 23:22
Show Gist options
  • Save thebiss/48e89b929f3fd45ce36e8c1ae8d68315 to your computer and use it in GitHub Desktop.
Save thebiss/48e89b929f3fd45ce36e8c1ae8d68315 to your computer and use it in GitHub Desktop.
Fix Windows Subsystem for Linux DNS after connecting to a VPN
#
# Refresh WSL's DNS
# for .bashrc
#
type dnsfix &> /dev/null
if [ $? -ne 0 ]; then
dnsfix () {
local search=$(grep -F "search" /etc/resolv.conf);
local ipv4dnsraw=$(/mnt/c/Windows/system32/netsh.exe interface ip show dns | tr -d '\r');
local ipv6dnsraw=$(/mnt/c/Windows/system32/netsh.exe interface ipv6 show dns | tr -d '\r');
local nameservers=$( printf '%s\n%s\n' "${ipv4dnsraw}" "${ipv6dnsraw}" | grep -iF "DNS Servers" | grep -v "None" | cut -c 43- | uniq | sed 's/^/nameserver /')
printf '%s on %s\n%s\n%s\n' '# generated by dnsfix' "`date`" "${nameservers}" "${search}" | sudo tee /etc/resolv.conf;
}
export -f dnsfix
fi
@thebiss
Copy link
Author

thebiss commented Apr 16, 2020

Updated to better handle IPV6, windows oddities like "None" and blanks.

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