Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

Make /etc/resolv.conf changes permanent in WSL 2

Writer Olivia Zamora

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

$ ping
ping: 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?

16

4 Answers

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
1

Had the exact same problem and followed these steps I found on WSL's github repo as a workaround:

  1. Delete /etc/resolv.conf
  2. Make /etc/resolv.conf by vim/nano
  3. Insert appropriate nameservers
  4. Start a new session and verify if resolv.conf is not reset again

Source: bradley101's answer here

2

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

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy