Score:3

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

in flag

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?

ru flag
`/etc/resolv.conf` is typically a symlink, and gets overwritten by NetPlan / SystemD. When it 'overwrites' the data what do you see in `ls -al /etc/resolv.conf`?
Olivier Grégoire avatar
in flag
@ThomasWard this is the result of that command: `lrwxrwxrwx 1 root root 29 Jan 25 17:43 /etc/resolv.conf -> ../run/resolvconf/resolv.conf`. I thought that `..` was relative to my own directory, so I tried in other directories and it's still `../run/resolvconf/resolv.conf`. And I see that that file currently has `8.8.8.8` as value anyways.
ru flag
OK that's normal. When you are altering things, make sure you look at `/etc/resolvconf` - somewhere in there is a 'head' file you can configure to have your primary DNS servers yourself ahead of anything set by the system - this is useful because that's how resolvconf works with dynamic network connections (Network Manager) in a base install of Ubuntu. Just for the future so they stay. Edit the head file for resolvconf, those changes are persistent. There may be OTHER items that get created underneath that but you'ill set primary DNS with that way. If that works I'll convert this to an answer
Olivier Grégoire avatar
in flag
@ThomasWard `sudo find /run -type f`, returns only `/run/resolvconf/resolv.conf` and `/run/sudo/ts/myusername`. There is nothing else in there.
ru flag
I didn't say it's in `/run` - note the directory i said in my comment is **`/etc/resolvconf`**, and is *not* in `/run`
Olivier Grégoire avatar
in flag
@ThomasWard I thought it was a typo because the /etc/resolvconf (or similar) doesn't exist.
NotTheDr01ds avatar
vn flag
@ThomasWard Please note that the OP said this is WSL. Systemd / NetPlan should not be in play here, right? WSL normally configures the `/etc/resolv.conf` from the Windows host, unless disabled with the `/etc/wsl.conf` mentioned.
ru flag
@NotTheDr01ds WSL2 is a fully virtualized environment, so it *could* be involved. We don't know if they're WSL1. As OP indicated they had to enable it and then disable it. If they disable it, nothing's going to manage the DNS, so OP has to handle it themselves.
NotTheDr01ds avatar
vn flag
@ThomasWard I'm not sure that I would call it "fully virtualized". Even WSL2 uses it's own `init` (PID 1) process to hook the interop between WSL and Windows. Most importantly for this question, at least, it's that `init` on WSL (both 1 and 2) where the networking is set up. Really, there's very little "Ubuntu" involved in that side of things, *especially* not Systemd.
NotTheDr01ds avatar
vn flag
@OlivierGrégoire Can you elaborate (with an edit to the question) on the "that doesn't work again"? Does the `resolv.conf` get overwritten, even with the `wsl.conf`? If so, try removing the spaces around the equals sign (`generateResolvConf=false`). It's a long shot, but an easy change to try.
Olivier Grégoire avatar
in flag
@NotTheDr01ds done! but `/run/resolvconf/resolv.conf` is now deleted when I exit/restart wsl
NotTheDr01ds avatar
vn flag
Sure, but I thought that's what you wanted? With it no longer using `/run/resolvconf/resolv.conf`, can you manually create your `/etc/resov.conf` successfully?
Olivier Grégoire avatar
in flag
@NotTheDr01ds I can create it indeed, but when I shut WSL down, the file is deleted.
NotTheDr01ds avatar
vn flag
Sorry, in the previous comment you said that it was `/run/resolvconf/resolv.conf` that was being deleted when you restarted. Was that supposed to be `/etc/resolv.conf`, or is it really both that are deleted?
Olivier Grégoire avatar
in flag
@NotTheDr01ds In my installation of WSL (I won't speak in general because I don't know), `/etc/resolv.conf` is a symlink to `/run/resolvconf/resolv.conf`. It's the latter that's deleted while the first stays a symlink even when the latter is deleted.
kn flag
Bumping, as I am running into the same problem and there is no solution yet
Goran Mitrovic avatar
mx flag
After upgrading to Ubuntu 23.04, none of solutions seem to work for me - resolv.conf seems to be generated every time after I do wsl --shutdown.
sy flag
While Ubuntu WSL still running in a window I ran a `wsl --shutdown` in another CMD window and waited a few seconds. Eventually the WSL session was killed. I then restarted WSL Ubuntu and the `resolv.conf` was still there. Have not yet tested whether it is retained through laptop reboots.
Goran Mitrovic avatar
mx flag
@NeilG I have two Ubuntu WSL2 instances at my place - 22.04 and 23.04. 22.04 is totally fine after applying suggested workarounds, but 23.04 seems consistent - it loses resolv.conf every single time. No idea why...
sy flag
I'm still on 22.04, @GoranMitrovic. I guess I'm happy staying here then. Maybe they "fixed" the bug for WSL on 23.04, and that's why the workarounds no longer work? ;-)
sy flag
That's funny @GoranMitrovic, I have just received a SO notification about 1 hour ago, notifying me of your comment from May 9, which I even already replied to! Monkey business! I'm being hunted by revenge voters. Could be them. Unless you have a clue?
Score:0
mv flag

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 https://github.com/Microsoft/WSL/issues/1908

ru flag
This answer seems incomplete - removing resolv.conf and then recreating it *without* detailing how it should be formatted, etc. is a good way to break things, and simply saying "The source is this other answer" without providing *complete* steps can break WSL.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.