Skip to content

Instantly share code, notes, and snippets.

@sivinnguyen
Last active April 12, 2024 07:30
Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save sivinnguyen/8bc0125b274250683a97e149cf270040 to your computer and use it in GitHub Desktop.
Save sivinnguyen/8bc0125b274250683a97e149cf270040 to your computer and use it in GitHub Desktop.
Fix DNS resolution in WSL2

Error

$ sudo apt-get update
Err:1 http://archive.ubuntu.com/ubuntu focal InRelease
Temporary failure in name rerolution

$ host google.com
;; connection timed out; no servers could be reached

Solution

The /etc/resolv.conf is the main configuration file for the DNS name resolver library. It was automatically generated by WSL. Some time there was a problem with that DNS.

  1. To stop automatic generation of resolv.conf, add the following entry to /etc/wsl.conf:
$ sudo cat << EOF > /etc/wsl.conf
[network]
generateResolvConf = false
EOF
  1. In a cmd/powershell window, run:
> wsl --shutdown

or:

> wsl --terminate <Distro>
  1. Restart WSL
  2. Create a file: /etc/resolv.conf. If it exists (even a link), replace existing one with new file.
sudo cat << EOF > /etc/resolv.conf
# Use one or many DNS servers you like
# nameserver 192.168.1.1
nameserver 8.8.8.8
nameserver 1.1.1.1
EOF
  1. Shutdown and restart WSL again.

Still not working

cmd/powershell as admin:

> wsl --shutdown  
> netsh winsock reset  
> netsh int ip reset all  
> netsh winhttp reset proxy  
> ipconfig /flushdns  

Restart Windows.

Ref:

@baartch
Copy link

baartch commented Apr 12, 2024

I figured out, I can leave the defaults inside WSL2 (let /etc/wsl.conf default and automatically create resolv.conf).
I simply have to reset the Windows settings like you described here, and then it works again....

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