Skip to content

Instantly share code, notes, and snippets.

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 dphoebus/a97a4a6c8c9a1c79e025e3e6998d0295 to your computer and use it in GitHub Desktop.
Save dphoebus/a97a4a6c8c9a1c79e025e3e6998d0295 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:

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