Make /etc/resolv.conf changes permanent in WSL 2
Olivia Zamora
On WSL 2, when I try to connect to a website, I get an issue:
$ ping
ping: Temporary failure in name resolutionNow, to fix this, I had to add /etc/wsl.conf with this content:
[network]
generateResolvConf = trueAnd I need to change my /etc/resolv.conf to:
nameserver 8.8.8.8This 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 = falseThe fix mentioned above is gone again.
So how do I make my changes to /etc/resolv.conf permanent on WSL 2?
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:
- Delete /etc/resolv.conf
- Make /etc/resolv.conf by vim/nano
- Insert appropriate nameservers
- Start a new session and verify if resolv.conf is not reset again
Source: bradley101's answer here
2These steps will ensure that /etc/resolv.conf is properly configured with the nameserver 8.8.8.8 each and every time the wsl starts
- Adding bashrc entry for initial user
echo "sudo su -" >> ~/.bashrc- Creating sudoers entry for initial user
sudo su -
echo "username ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/username- Adding bashrc entry for root user
echo "nameserver 8.8.8.8" > /etc/resolv.confNote: 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=falseto /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