Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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:

@fanismichalakis

This comment has been minimized.

Copy link

@fanismichalakis fanismichalakis commented Feb 13, 2021

Thanks a lot! I was scratching my head over this, your last "still not working" instructions finally made it work.

@sivinnguyen

This comment has been minimized.

Copy link
Owner Author

@sivinnguyen sivinnguyen commented Feb 22, 2021

Thanks a lot! I was scratching my head over this, your last "still not working" instructions finally made it work.

That's great. I'm glad it worked for you.

@dingobar

This comment has been minimized.

Copy link

@dingobar dingobar commented Mar 24, 2021

Didn't work.

@mbzbugsy

This comment has been minimized.

Copy link

@mbzbugsy mbzbugsy commented Mar 31, 2021

Didn't work...

@sivinnguyen

This comment has been minimized.

Copy link
Owner Author

@sivinnguyen sivinnguyen commented Mar 31, 2021

Didn't work.

@dingobar @mbzbugsy
Sorry, I can't find any new solution. Please try to update your windows or WSL.

@cbare

This comment has been minimized.

Copy link

@cbare cbare commented Apr 1, 2021

This worked for me. Thanks!

But, still a little worried about why I had to do that...

@sivinnguyen

This comment has been minimized.

Copy link
Owner Author

@sivinnguyen sivinnguyen commented Apr 1, 2021

@cbare
I think wsl generates wrong nameserver. But I don't have to do this right now with Insider Preview Build 21296.rs_prerelease.210115-1423

@mbzbugsy

This comment has been minimized.

Copy link

@mbzbugsy mbzbugsy commented Apr 1, 2021

Alright, here is what did work for me at the end. I tried some other poor bastards solution — who also had been plagued by this hell issue with WSL2. His solution was to disable the Windows WSL feature and re-enabling it (restart required), and voila network is up again. But disabling the auto-generation of the resolv.conf and overriding the nameserver is still a must. I just wish I had tried this earlier,, so many hours of trial and error — mostly error — finally came to an end. Hope this will work for others as well.

May the force be with you, always!

@dingobar

This comment has been minimized.

Copy link

@dingobar dingobar commented Apr 1, 2021

Didn't work.

@dingobar @mbzbugsy
Sorry, I can't find any new solution. Please try to update your windows or WSL.

Please excuse my outburst. At the time I think I was getting overly frustrated that I can't get it working. I think my problem may be related to enterprise firewall settings and as such a different problem than what you solved here.

@mayorvlf

This comment has been minimized.

Copy link

@mayorvlf mayorvlf commented May 6, 2021

I was able to resolve by configuring a DNS in .yaml file for Netplan located /etc/netplan I created a static IP as well in the process but it is not necessary:
network:
version: 2
renderer: networkd
ethernets:
ens3:
dhcp4: no
addresses:
- 192.168.121.221/24
gateway4: 192.168.121.1
nameservers:
addresses: [8.8.8.8, 1.1.1.1]

I followed instruction out of the following link: https://linuxize.com/post/how-to-configure-static-ip-address-on-ubuntu-20-04/

@sivinnguyen

This comment has been minimized.

Copy link
Owner Author

@sivinnguyen sivinnguyen commented May 12, 2021

I was able to resolve by configuring a DNS in .yaml file for Netplan located /etc/netplan I created a static IP as well in the process but it is not necessary:
network:
version: 2
renderer: networkd
ethernets:
ens3:
dhcp4: no
addresses:

  • 192.168.121.221/24
    gateway4: 192.168.121.1
    nameservers:
    addresses: [8.8.8.8, 1.1.1.1]

I followed instruction out of the following link: https://linuxize.com/post/how-to-configure-static-ip-address-on-ubuntu-20-04/

Thanks, @mayorvlf

@PavelSosin-320

This comment has been minimized.

Copy link

@PavelSosin-320 PavelSosin-320 commented May 16, 2021

Maybe, I'm missing something but I don't see how it can work at all. I suppose that VPN providers offer not only HTTP traffic but also DNS traffic using one of the secured DNS protocols (DOH). When I'm looking at the standard Linux DNS service config file I see parameters selecting the type of the DNS protocol per network link. VPN client works as a proxy for both DNS and HTTP traffic. Google Chrome and Microsoft Edge have such configurations too and select the protocol during DNS negotiation.
The VPN client installed on the routers serves name resolution requests from LAN insecure but delegates them to the DNS services secured or unsecured.
If VPN doesn't encode name resolution requests everybody can intercept them and resolve them to some hacker's machine IP. It is called DNS leak.
I think that everybody can look at the VPN portal to see what is written about DNS service. I found it by 1st hit for NotdVPN DNS.

@DanielViglione

This comment has been minimized.

Copy link

@DanielViglione DanielViglione commented Nov 13, 2021

This kind of works. Adding

 [network]
generateResolvConf = false

to /etc/wsl.conf certainly doesn't generate /etc/resolv.conf during next wsl launch. However, I couldn't save the google DNS to resolv.conf. So instead I removed the generateResolvConf line and in next wsl reboot, it automatically generated a new DNS, which did work.

@jnettels

This comment has been minimized.

Copy link

@jnettels jnettels commented Nov 25, 2021

The solutions found online helped me fix the issue in my home network, but DNS resolution would still not work when using my laptop at the office. This is what seemed to work:

  • In WSL, set up two distributions, e.g. "Ubuntu" and "Debian"
  • Set one to WSL version 1, and the other to WSL version 2
  • wsl --set-version Ubuntu 1
  • wsl --set-version Debian 2
  • Copy the contents of /etc/resolv.conf in Ubuntu (WSL 1) to Debian (WSL 2)
  • Done

It turned out that /etc/resolv.conf in WSL 1 had a special entry "search our-company-domain.de" (and some ipv4 and ipv6 addresses). One of the ips is the address of our central server. I guess I will also have to turn of the automatic generation of resolv.conf.

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