WSL2 Ubuntu: ping: hostname: Temporary failure in name resolution

( It would mean the world if you would consider following @colhountech on twitter. It won’t cost you anything but it gives me great encouragement to continue writing these posts)

If you run your own AD / DNS servers for internal network resolution, an annoyance with WSL2 / Ubuntu is that you can’t ping or connect to local network hosts. This is because the nameserver is set to an internal wsl proxy nameserver used to resolve the Hyper-V private internal network. If you open Hyper-V and select the Virtuaal Switch Manager from the right side panel, you will be able to see this network configured as an internal network.

When I ping or try to connect to a local machine (which I’ll call hostname) from the WSL ubuntu shell, I get the following error:

username@WSL:~$ ping hostname
ping: hostname: Temporary failure in name resolution

username@WSL:~$ ssh -l username hostname
ssh: Could not resolve hostname hostname: Temporary failure in name resolution

username@WSL:~$ ssh -l username hostname.localnet.local
ssh: Could not resolve hostname hostname.localnet.local: Temporary failure in name resolution

The issue has to do with the /etc/resolv.conf pointing to the wrong name server, not so much wrong, but the proxy nameserver that does not seem to resolv local network hostnames. I’m not sure why, as I would assume it would resolve based on the DNS settings of the Windows 10 host computer. Maybe someone can let me know in the comments.

The contents of the /etc/resolv.conf were:

# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.21.67.1

If you try to replace the nameserver, this file will be replaced when you restart a WSL Ubuntu session.

To fix this , you have to stop WSL from generating the resolv.conf which firstly involves creating the file etc/wsl.conf and adding the following contents.

/etc/wsl.conf

[network]
generateResolvConf = false

Caveat: You also have to shutdown WSL for the change to take effect. From a Cmd Prompt, do:

C:\Users\Username> wsl.exe --shutdown

When I restart the ubuntu wsl shell, I had a broken symbolic link:

ls -l /etc/resolv.conf 
lrwxrwxrwx 1 root root         23 Aug  4  2020 resolv.conf ->  ../run/systemd/resolve/resolv.conf

I just deleted this symlink and recreated the file as root.

rm /etc/resolv.conf
cat > /etc/resolv.conf <<EOF

nameserver 192.168.2.1
hameserver 192.168.2.2
domain localnet.local
EOF

Now it does what I want it to do. Maybe not what the WSL guys intended, but there you go!

Did you learn something or have an opinion? Let me know on twitter @colhountech

References:

WSL 2 keeps overwriting resolv.conf · Issue #5420 · microsoft/WSL · GitHub