Skip to content

Instantly share code, notes, and snippets.

@ExFed
Forked from nfekete/wsl-fix-resolvconf.sh
Last active January 18, 2022 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ExFed/937a754f701d10bff081146e6dcd74c7 to your computer and use it in GitHub Desktop.
Save ExFed/937a754f701d10bff081146e6dcd74c7 to your computer and use it in GitHub Desktop.
Fix resolv.conf in Windows Subsystem for Linux, when WSL doesn't correctly generate it.
Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 4000
Get-NetIPInterface -InterfaceAlias "vEthernet (WSL)" | Set-NetIPInterface -InterfaceMetric 1
#!/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 "------------------------"
PSCMD='Get-DnsClientServerAddress -AddressFamily ipv4 | Where-Object { (Get-NetAdapter | Where-Object {$_.Status -Match "Up"} | Select-Object -ExpandProperty ifIndex) -contains $_.InterfaceIndex } | select -ExpandProperty ServerAddresses'
{
head -1 /etc/resolv.conf | grep '^#.*generated'
for i in `/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command "$PSCMD"`; do
echo nameserver $i
done
tail -n+2 /etc/resolv.conf | grep -v '^nameserver'
} | tr -d '\r' | tee $TMP
echo
(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