- Prevent WSL from overwriting DNS settings by setting
generateResolvConf = falsein/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
nameserverentries and saving it to/etc/resolv.conf(with CRLF normalization viatr -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