Skip to content

Instantly share code, notes, and snippets.

@alex-ber
Last active February 3, 2024 22:42
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 alex-ber/9dcf398586395cb2b5caf1c9de91c234 to your computer and use it in GitHub Desktop.
Save alex-ber/9dcf398586395cb2b5caf1c9de91c234 to your computer and use it in GitHub Desktop.
Fix DNS resolution in WSL2
  1. Open PowerShell as Administator. Type Get-DnsClientServerAddress (similar to ipconfig /all) Look upon Ethernet. Ignore 0.0.0.0. In my case the only IP is 192.168.1.1

  2. In the bash terminal of WSL2: Append following text:

[network]

generateResolvConf = false

to /etc/wsl.conf

  1. In a cmd window, run wsl --shutdown

  2. Click on restart WSL2

  3. Go to /etc/

  4. if /etc/resolf.conf file doesn't exist, create one and go to p.9.

  5. Run:

sudo lsattr resolv.conf

  1. If you see -i- in the result, run

sudo chattr -i resolv.conf

(removes immutable attribute)

  1. Put the following line in the file:

nameserver 192.168.1.1 # Change to match IP from p.1

  1. sudo chattr +i resolv.conf (restoring immutable flag)

  2. In a cmd window, run wsl --shutdown

  3. Click on restart WSL2

  4. Open WSL2 bash and type

Try something like

nslookup github.com

It should work now.

Credit: microsoft/WSL#4285 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment