42

On WSL 2, when I try to connect to a website, I get an issue:

$ ping www.google.com
ping: www.google.com: Temporary failure in name resolution

Now, to fix this, I had to add /etc/wsl.conf with this content:

[network]
generateResolvConf = true

And I need to change my /etc/resolv.conf to:

nameserver 8.8.8.8

This works fine.

However when I restart my system, when I shutdown wsl or do anything similar, /etc/resolv.conf is overwritten with the previous value.

Of course, if I change /etc/wsl.conf to the following and restart again:

[network]
generateResolvConf = false

The fix mentioned above is gone again.

So how do I make my changes to /etc/resolv.conf permanent on WSL 2?

3 Answers3

71

I found this solution here

sudo rm /etc/resolv.conf
sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'
sudo bash -c 'echo "[network]" > /etc/wsl.conf'
sudo bash -c 'echo "generateResolvConf = false" >> /etc/wsl.conf'
sudo chattr +i /etc/resolv.conf
snuczek
  • 826
10

I tried to comment on snuczek's answer but don't have enough reputation.

From the linked solution in their answer, adding:

[network]
generateResolvConf=false

to /etc/wsl.conf does stop wsl from automatically generating the file, but only once it has been shutdown once. Once done, the config will be respected and it's not required to make the /etc/resolv.conf file immutable using the chattr +i command.

To shutdown wsl, run wsl --shutdown in a windows command prompt (this shuts down all WSL distributions).

Of course you'll still need to add your dns servers into the /etc/resolv.conf file (for example):

nameserver 1.1.1.1
nameserver 1.0.0.1
0

These steps will ensure that /etc/resolv.conf is properly configured with the nameserver 8.8.8.8 each and every time the wsl starts

  1. Adding bashrc entry for initial user
echo "sudo su -" >> ~/.bashrc
  1. Creating sudoers entry for initial user
sudo su -

echo "username ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/username

  1. Adding bashrc entry for root user
echo "nameserver 8.8.8.8" > /etc/resolv.conf

Note: This will override the contents of the /etc/resolv.conf file. You can use sed command to do any specific modification as required