Skip to content

Instantly share code, notes, and snippets.

@girorme
Last active January 26, 2024 20:03
Show Gist options
  • Save girorme/65653a036f41cbb2e78eef10970857a0 to your computer and use it in GitHub Desktop.
Save girorme/65653a036f41cbb2e78eef10970857a0 to your computer and use it in GitHub Desktop.
wsl dns resolutions errors

DNS resolution

option 1

  • Execute the command below
$ sudo sed -i '/nameserver/d' /etc/resolv.conf
$ powershell.exe -Command '(Get-DnsClientServerAddress -AddressFamily IPv4).ServerAddresses | ForEach-Object { "nameserver $_" }' | tr -d '\r' | sudo tee -a /etc/resolv.conf > /dev/null

option 2

  • Create a script (put it in /bin/ to ease of use) and run it
#!/bin/bash

echo "Getting current DNS servers, this takes a couple of seconds"

/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command '
$ErrorActionPreference="SilentlyContinue"
Get-NetAdapter -InterfaceDescription "Cisco AnyConnect*" | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
Get-NetAdapter | ?{-not ($_.InterfaceDescription -like "Cisco AnyConnect*") } | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
' | \
        awk 'BEGIN { print "# Generated by vpn fix func on", strftime("%c"); print } { print "nameserver", $1 }' | \
        tr -d '\r' > /etc/resolv.conf
clear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment