DNS not working on WSL
I'm currently using an Insider build of Windows 11. (22458)
However, I had this problem a few times on other builds. (and wsl worked before)
When I start wsl, I'm not able to do a DNS request. (Pinging an IP address directly works.)
I tracked this problem down to the file: /etc/resolv.conf
It includes the DNS server address.
The default is: 172.22.192.1
However, this one doesn't work.
If I change it to 1.1.1.1 (Cloud Flare DNS) it works again.
However, after every restart of wsl it changes it back to the default.
There is a file called wsl.conf in which I can disable generating this file, but if always just deletes the file entirely after every reboot.
So how can I fix this?
My first Idea was to completely reset wsl, but neither resetting the app, nor reinstalling WSL via features fixed it.
Does anyone have another idea what to do?
95 Answers
This is a problem with WSL, not Ubuntu or Windows. There are lots of solutions out there, the best one I found
2It also includes VPN info, but the steps work without the VPN also.
Run these commands in the Ubuntu terminal.
cd ~/../../etc # Go to etc folder in WSL. echo "[network]" | sudo tee wsl.conf # Create wsl.conf file and add the first line. echo "generateResolvConf = false" | sudo tee -a wsl.conf # Append wsl.conf the next line. wsl --terminate Debian # Terminate WSL in Windows cmd, in case the OS is Ubuntu not Debian. cd ~/../../etc # Go to etc folder in WSL. sudo rm -Rf resolv.conf # Delete the resolv.conf file.In Windows cmd, PowerShell or terminal with the VPN connected do:
Get-NetIPInterfaceoripconfig /allfor getting the DNS primary and secondary.With the DNS primary and secondary gotten from step 2. replace the numbers in the next step in the X.X.X.X
echo "nameserver X.X.X.X" | sudo tee resolv.conf(Create resolv.conf and append the line.)
echo "nameserver X.X.X.X" | sudo tee -a resolv.conf(Append the line in resolv.conf)
wsl --terminate Debian(Terminate WSL in Windows cmd, in case is Ubuntu not Debian).
sudo chattr +i resolv.confFinally in Windows cmd, PowerShell or terminal:
Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000Credit: @MartinCaccia, @yukosgiti, @machuu and @AlbesK:
WSL is getting its IP address (and it's DNS server, netmask and other stuff) from Windows' DHCP Server. Either fix it on Windows, or fix the DNS server on 172.22.192.1.
This is a Windows problem, not a Ubuntu problem.
And No, I don't know how Windows does DHCP.
1windows wsl dns issue fixdns
Inside WSL2, create or append file: /etc/wsl.conf
Put the following lines in the file in order to ensure the your DNS changes do not get blown away
echo "[network]" | sudo tee /etc/wsl.conf echo "generateResolvConf = false" | sudo tee -a /etc/wsl.confIn a cmd window, run
wsl --shutdownStart WSL2
Run the following inside WSL2
sudo rm -rf /etc/resolv.conf sudo cat << EOF > /etc/resolv.conf search [yourbase.domain.local] nameserver 8.8.8.8 nameserver 1.1.1.1 EOF
As stated by most answers out there, creating the following configuration files and then restarting WSL should be enough:
/etc/wsl.conf
[network]
generateResolvConf = false/etc/resolv.conf
nameserver 8.8.8.8However, for some reason my resolv.conf keep getting recreated on every boot.
So I had to make it immutable, like so:
sudo chattr +i /etc/resolv.confMany times this issue has bothered me, I ended up making this simple helper code:
2The problem is the VPN Ethernet Adaptor's DNS server settings are not taken by the WSL. These steps worked for me to add these settings manually:
I have forked and updated the steps by to make them easier.