-
-
Save a-stankevich/321d8134be5a69280901555ea9ade855 to your computer and use it in GitHub Desktop.
Fix resolv.conf in Windows Subsystem for Linux, when WSL doesn't correctly generate it.
This file contains 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
#!/bin/bash | |
TMP=`mktemp` | |
trap ctrlC INT | |
removeTempFiles() { | |
rm -f $TMP | |
} | |
ctrlC() { | |
echo | |
echo "Trapped Ctrl-C, removing temporary files" | |
removeTempFiles | |
stty sane | |
} | |
echo "Current resolv.conf" | |
echo "-------------------" | |
cat /etc/resolv.conf | |
echo | |
echo "Creating new resolv.conf" | |
echo "------------------------" | |
{ | |
head -1 /etc/resolv.conf | grep '^#.*generated' | |
for i in `/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command "Get-DnsClientServerAddress -AddressFamily ipv4 | Select-Object -ExpandProperty ServerAddresses"`; do | |
echo nameserver $i | |
done | |
tail -n+2 /etc/resolv.conf | grep -v '^nameserver' | |
} | tr -d '\r' | tee $TMP | |
(set -x; sudo cp -i $TMP /etc/resolv.conf) | |
removeTempFiles | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment