Windows Subsystem of Linux 2: DNS lost

Windows Subsystem for Linux 2 (WSL2) keeps losing domain name resolution for me.

Why is this?

Two reasons I think, first reason is as it doesn't have a init setup that works with cronjobs.  Example of what I am talking about is here, there are workarounds explored other places.

sudo systemctl status redis-server

System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
  • Create or open the file /etc/wsl.conf If it already exists it will look like this
# 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.22.32.1

The nameserver listed here is my internal network address of my machine confirmed by running ipconfig /all from the command line

  • Powershell ipconfig /all | Select-String "WSL" -Context 0,20
  • Bash ipconfig.exe /all | grep "WSL" -A 20
> Ethernet adapter vEthernet (WSL):

     Connection-specific DNS Suffix  . :
     Description . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter #2
     Physical Address. . . . . . . . . : 00-15-5D-AD-CE-BC
     DHCP Enabled. . . . . . . . . . . : No
     Autoconfiguration Enabled . . . . : Yes
     Link-local IPv6 Address . . . . . : fe80::5c9e:5e67:a621:22a1%56(Preferred)
     IPv4 Address. . . . . . . . . . . : 172.22.32.1(Preferred)
     Subnet Mask . . . . . . . . . . . : 255.255.240.0
     Default Gateway . . . . . . . . . :
     DHCPv6 IAID . . . . . . . . . . . : 939529565
     DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-21-4D-E1-CC-80-FA-5B-4A-8D-96
     DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                         fec0:0:0:ffff::2%1
                                         fec0:0:0:ffff::3%1
     NetBIOS over Tcpip. . . . . . . . : Enabled

Now change the contents of the wsl.conf file to I am going to set the nameserver to an external known good internet IP of 8.8.8.8.  Now you should remember that name resolution will happen externally so internal ips may need to be mapped going forward.

[network]
generateResolvConf = false
nameserver 8.8.8.8
  • Save your changes
  • Restart WSL2 Service wsl --shutdown and then restart the windows service

Name resolution has been fixed now.