-
Notifications
You must be signed in to change notification settings - Fork 826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WSL 2 keeps overwriting resolv.conf #5420
Comments
/etc/resolv.conf is symlink to /run/resolvconf/resolv.conf, and /run is tmpfs. |
@0xbadfca11 That did not help. I ran:
But after restarting WSL, I see the following: |
Ok, so I finally found solution in an old ticket: #1908 (comment) So the full set of commands that have to be run is:
|
|
There's no point mucking about with |
I can confirm that the wsl.conf does nothing and that using the solution mention above with the +i attribute does work. |
wsl --shutdown It seems that changes to wsl.conf are only read when the subsystem completely restarts. I kept trying to get /etc/resolv.conf to stick around and it was overwritten every time I started a new wsl instance even with the +i attribute on /etc/resolv.conf. The only way to get it to stay for me was to do a wsl --shutdown once. |
Yeah, sudo chattr -f +i /etc/resolv.conf set the file to immutable, and prevents the os from changing this file. |
Save the content in /etc/resolve.conf run wsl --shutdown in powershell this should solve your problem. |
If you need to make changes to the file after running # make file mutable again
sudo chattr -i /etc/resolv.conf |
that worked for me like charm. thanks a bunch! |
This just results in resolv.conf being deleted every time wsl starts for me now. Only workaround is to chattr +i resolv.conf which shouldn't be necessary - and I'm kind of astonished by how long this issue has persisted. Is it really so hard to just leave the dang file alone if |
On current Windows 11 build the Also, trying to set +i attribute on them results in error for me:
|
I had to delete /etc/resolv.conf first as it was a symlink to /run/resolvconf/resolv.conf you can verify by running |
I simply use this every time I set up a new WSL instance..
works even on windows 11. |
@kevin-he-01 I'm on Windows 10. This issue was about WSL2 on Windows 10, it then gets confusing because no one is saying if they are on W10 or W11. Anyway, Windows being a mess, the solution from
@anomaly256 This issue was for Windows 10, and it was closed because the solution was provided. See what I wrote above. I think that the appropriate issue for you both is #6977. |
Of course Microsoft won't fix it, they want to create the false impression that Linux is unstable and crap, and that everyone should just use Windows instead. |
@jnawk @kevin-he-01 @anomaly256 @Aziks0 Hello! Thanks for bringing this to my attention. This issue #8030 may provide some context to this, granted this change was made for WSL on Windows 11. For the time being, I would make a backup copy of your custom |
Thank you! I think #8030 is exactly describing what many of us are facing. It's good to see that it is already fixed in the preview Store version, so I expect some later Windows releases will include it. |
Hi, |
Setting DNS in /etc/resolv.confBy default /etc/resolv.conf is overwritten in WSL,
In your
Configuration
Final / Most Important StepRun the following command in your terminal, Note: Do not close your terminal while doing this entire process or it may overwrite your
Then, start
This Should Fix It! |
nothing worked for me but what worked for me was to edit ~/.bashrc file. Just add the following line to the end:
|
I kind of get the opposite impression. The 100+ instances of Debian I've used before have all worked quite well, and resolv.conf has never been a problem ... until I tried to run Debian on Windows. Leave it to M$ to f *** up a stable aspect of a stable OS. M$ could screw up a one-car funeral. |
I don't know whether to laugh, cry, or give you kudos and just move on. A few years from now, I can see: "Hey guys, I finally figured out how to make it work, each time you want to use Debian, cat the source directories of debian--src, pipe it to gcc, wait a few minutes, and PRESTO! It's so intuitive, anyone could do it!" |
you rather make my point, but think if this was your only experience with Linux. |
Try this, it works for me:
It also works after reboot/shutdown the WSL. However, it only works with one line, when I try:
After restart WSL, |
@qwang07 This line liudonghua@DESKTOP-DELL:~$ echo "nameserver 1.1.1.1\nnameserver 8.8.8.8" > /tmp/resolv.conf
liudonghua@DESKTOP-DELL:~$ cat /tmp/resolv.conf
nameserver 1.1.1.1\nnameserver 8.8.8.8
liudonghua@DESKTOP-DELL:~$ #
liudonghua@DESKTOP-DELL:~$ # use cat with Ctrl+D
liudonghua@DESKTOP-DELL:~$ cat > /tmp/resolv-1.conf
nameserver 1.1.1.1
nameserver 8.8.8.8
# press Ctrl+D to terminate
liudonghua@DESKTOP-DELL:~$ cat /tmp/resolv-1.conf
nameserver 1.1.1.1
nameserver 8.8.8.8
liudonghua@DESKTOP-DELL:~$ # use cat with EOL delimiter
liudonghua@DESKTOP-DELL:~$ cat > /tmp/resolv-2.conf << EOL
> nameserver 1.1.1.1
> nameserver 8.8.8.8
> EOL
liudonghua@DESKTOP-DELL:~$ cat /tmp/resolv-2.conf
nameserver 1.1.1.1
nameserver 8.8.8.8
liudonghua@DESKTOP-DELL:~$ # use echo with unfinished quotes
liudonghua@DESKTOP-DELL:~$ echo "nameserver 1.1.1.1
> nameserver 8.8.8.8" > /tmp/resolv-3.conf
liudonghua@DESKTOP-DELL:~$ cat /tmp/resolv-3.conf
nameserver 1.1.1.1
nameserver 8.8.8.8
liudonghua@DESKTOP-DELL:~$ # use echo -e
liudonghua@DESKTOP-DELL:~$ echo -e "nameserver 1.1.1.1\nnameserver 8.8.8.8" > /tmp/resolv-4.conf
liudonghua@DESKTOP-DELL:~$ cat /tmp/resolv-4.conf
nameserver 1.1.1.1
nameserver 8.8.8.8
liudonghua@DESKTOP-DELL:~$
liudonghua@DESKTOP-DELL:~$ cat << EOL > /tmp/resolv-5.conf
> nameserver 1.1.1.1
> nameserver 8.8.8.8
> EOL
liudonghua@DESKTOP-DELL:~$ cat /tmp/resolv-5.conf
nameserver 1.1.1.1
nameserver 8.8.8.8
liudonghua@DESKTOP-DELL:~$ ll /tmp/resolv*
-rw-r--r-- 1 liudonghua liudonghua 38 Sep 16 11:38 /tmp/resolv-1.conf
-rw-r--r-- 1 liudonghua liudonghua 38 Sep 16 11:42 /tmp/resolv-2.conf
-rw-r--r-- 1 liudonghua liudonghua 38 Sep 16 12:16 /tmp/resolv-3.conf
-rw-r--r-- 1 liudonghua liudonghua 38 Sep 16 12:20 /tmp/resolv-4.conf
-rw-r--r-- 1 liudonghua liudonghua 38 Sep 16 12:48 /tmp/resolv-5.conf
-rw-r--r-- 1 liudonghua liudonghua 39 Sep 16 11:37 /tmp/resolv.conf
liudonghua@DESKTOP-DELL:~$ |
This is the only workaround that work for me, the only comment is that it requires admin access to modify resolv.conf, I found a work around, Adding a script when login.
|
Try restarting windows after changing wsl.conf |
thank you |
And for portability sake, it might be better to use printf instead. If your script uses /bin/sh, the -e option for echo likely will cause it to print the '-e' In dash:
|
I like this approach. Makes /root/resolv.conf.sh executable using wsl.conf boot command
|
This or a similar issue is recurring. With Ubuntu 24.04,
The solution to this was setting values in /etc/systemd/resolved.conf, e.g. |
Environment
Steps to reproduce
Previously, I had my WSL in version 1 and everything worked fine. Then I decided to upgrade to WSL 2 and - similar to many others - I lost internet connection. Thankfully, I could easily bring it back by running:
sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'
Apparently, it was just a DNS issue. I could live with that. However, then I restarted my WSL and the nameserver was set back to
172.31.208.1
. So I decided to do exactly what was written in the comments in/etc/resolv.conf
:And ran the following lines:
I was proud that I resolved the issue so elegantly until I restarted my WSL again. Now,
resolv.conf
was in red and not accessible from Ubuntu. When I tried to access it from Windows, I saw just an empty file.Expected behavior
After restarting WSL,
resolv.conf
keeps the user-defined values.Actual behavior
After restarting WSL,
resolv.conf
is empty or not accessible at all.The text was updated successfully, but these errors were encountered: