Last active
April 26, 2021 23:22
-
-
Save thebiss/48e89b929f3fd45ce36e8c1ae8d68315 to your computer and use it in GitHub Desktop.
Fix Windows Subsystem for Linux DNS after connecting to a VPN
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to better handle IPV6, windows oddities like "None" and blanks.