WSL: WSL DNS resolution is really slow

Environment

Windows build number: Microsoft Windows [Version 10.0.19042.685]
Distribution version: Ubuntu 20.04 and Kali 2020.2
Whether the issue is on WSL 2 and/or WSL 1: WSL 2 in Ubuntu and WSL 1 in Kali

Steps to reproduce

Just ping google.com in Ubuntu or sudo ping google.com in Kali

Expected behavior

A normal ping operation.

Actual behavior

I’m actually getting the ping after 20 seconds or so, however, if I ping using an ip (8.8.8.8 for example), results are instantaneous. I have to say I tried to reboot WSL and my host, downgrade to WSL 1, setting custom DNS updating /etc/resolv.conf (previously updating /etc/wsl.conf too), and setting manually DNS in the virtual WSL switch. None of them worked and it is kind of desperating because easy tasks like installing packages with apt or similar become really time consuming.

I’m having this behaviour in both, Kali 2020.2 WSL 1 and Ubuntu 20.04 WSL 2.

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 6
  • Comments: 18 (1 by maintainers)

Most upvoted comments

Troubleshooting steps suggested by John in my Superuser question helped me to fix DNS resolver:

I’m citing here Johns words:

Run TCP/IP Reset, DNS Flush

Open cmd.exe with Run as Administrator

(1) netsh int ip reset c:\resetlog.txt

(2) ipconfig /flushdns

(3) restart the computer

Test after restarting. This fixes many DNS issues.

If the issue remains, try DISM and SFC.

Open cmd.exe with Run as Administrator.

DISM.exe /Online /Cleanup-image /StartComponentCleanup

DISM.exe /Online /Cleanup-Image /Restorehealth

SFC /SCANNOW

Again restart and test.

I had to do the first step two times, and eventually check and install latest Windows Updates. Not sure that was the final fix, but DNS resolver works for me now in both Windows and WSL2 environment. May be this information can also help you.

For me, the issue was caused by OpenVPN. After I closed it, I removed the custom resolve.conf and let WSL generate it.

  1. Close VPN program
  2. Remove custom /etc/resolv.conf and /etc/wsl.conf
  3. wsl --shutdown

Hi folks, we have put out a new update that aims to address networking issues in WSL. In your .wslconfig file you can set experimental.networkingMode=mirrored, as well as some other key settings that should improve your network compatibility! Please try them out and let us know what you think.

More info on this release and the changes can be found here in the blog post.

Please note: You need to be on a Windows Insiders version to use the new networking settings (Any channel of Windows Insiders will do, including release preview). If you see the “These are not supported” messages it means that your current Windows version doesn’t have support, and you will need to upgrade. These features will eventually be coming to Windows 11 22H2.

FYI, you do not need to reboot Microsoft Windows OS for any change in WSL2 VM. Just use wsl.exe --terminate <distro> to shutdown it.

For me, it looked like the generated windows dns server in /etc/resolv.conf was acting crazy. Strace showed lookups were timing out in linux while wireshark on the host showed they were being returned in millisconds. Might have something to do with IPv6 lookups vs IPv4. I removed the symlink to /etc/resolv.conf and made my own that uses my LANs DNS. It works wonderfully now but I’ll have to reconfigure it any time I connect to a new network. Could use 8.8.8.8 but there is stuff on the LAN that I need resolved by name. This workaround corrected the behavior in ubuntu but not docker-desktop.

Here are the steps for the workaround:

From linux root

rm /etc/resolv.conf
echo nameserver 8.8.8.8 > /etc/resolv.conf
echo "[network]" >> /etc/wsl.conf
echo generateResolvConf = false >> /etc/wsl.conf

Reboot windows

The problem mainly manifested for me in Docker. So because this is the top result for slow dns resolution wsl2 docker on Google, here is the fix for me:

Add the following into your Docker engine settings (Settings→Docker Engine in Docker Desktop)

"dns": [
    "8.8.8.8"
  ],

Pick whatever DNS you want (alternatives are 1.1.1.1 or your local router or ISP DNS). This apparently makes docker skip the internal DNS resolution. Credits to @ericslandry

@0anton this did not work for me, it is still really slow. Anyway, thanks for your answer.