Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aont/e5f33e588abe8180a3e2aed1a8d4b4e6 to your computer and use it in GitHub Desktop.

Select an option

Save aont/e5f33e588abe8180a3e2aed1a8d4b4e6 to your computer and use it in GitHub Desktop.

Workaround for WSL1 DNS issue

  • Prevent WSL from overwriting DNS settings by setting generateResolvConf = false in /etc/wsl.conf.
  • Pull active DNS servers from Windows (based on default IPv4/IPv6 routes and interface metrics) using PowerShell, ensuring the most relevant adapters are used.
  • Write a stable Linux resolver configuration by converting the Windows DNS list into nameserver entries and saving it to /etc/resolv.conf (with CRLF normalization via tr -d '\r').
echo -e '[network]\ngenerateResolvConf = false' >> /etc/wsl.conf

/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -NoProfile -Command '$ifs=@(Get-NetRoute -DestinationPrefix "0.0.0.0/0","::/0" -ErrorAction SilentlyContinue | Sort-Object RouteMetric,InterfaceMetric | Select-Object -ExpandProperty InterfaceIndex -Unique); $dns=foreach($i in $ifs){ (Get-DnsClientServerAddress -InterfaceIndex $i).ServerAddresses }; $dns | Where-Object { $_ } | Select-Object -Unique | ForEach-Object { "nameserver $_" }' | tr -d '\r' > /etc/resolv.conf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment