34

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?

Olivier Grégoire
  • 453
  • 1
  • 4
  • 10
  • 1
    /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? – Thomas Ward Jun 23 '21 at 13:34
  • @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. – Olivier Grégoire Jun 23 '21 at 13:38
  • 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 – Thomas Ward Jun 23 '21 at 13:56
  • @ThomasWard sudo find /run -type f, returns only /run/resolvconf/resolv.conf and /run/sudo/ts/myusername. There is nothing else in there. – Olivier Grégoire Jun 23 '21 at 14:24
  • I didn't say it's in /run - note the directory i said in my comment is /etc/resolvconf, and is not in /run – Thomas Ward Jun 23 '21 at 14:26
  • @ThomasWard I thought it was a typo because the /etc/resolvconf (or similar) doesn't exist. – Olivier Grégoire Jun 23 '21 at 15:13
  • @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. – NotTheDr01ds Jun 23 '21 at 16:49
  • @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. – Thomas Ward Jun 23 '21 at 17:42
  • @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 Jun 23 '21 at 18:30
  • @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. – NotTheDr01ds Jun 23 '21 at 18:45
  • @NotTheDr01ds done! but /run/resolvconf/resolv.conf is now deleted when I exit/restart wsl – Olivier Grégoire Jun 24 '21 at 07:19
  • 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? – NotTheDr01ds Jun 27 '21 at 15:31
  • @NotTheDr01ds I can create it indeed, but when I shut WSL down, the file is deleted. – Olivier Grégoire Jun 27 '21 at 15:59
  • 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? – NotTheDr01ds Jun 27 '21 at 16:12
  • @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. – Olivier Grégoire Jun 27 '21 at 16:24
  • Bumping, as I am running into the same problem and there is no solution yet – Rivered Aug 04 '21 at 22:07
  • 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. – Goran Mitrovic Apr 22 '23 at 10:11
  • 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. – NeilG May 08 '23 at 00:58
  • @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... – Goran Mitrovic May 09 '23 at 07:52
  • 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? ;-) – NeilG May 10 '23 at 01:05
  • 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? – NeilG Aug 29 '23 at 05:57

3 Answers3

57

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
  • 686
  • This seems promising. Unfortunately I recently got rid of Windows so I can't try. If your answer gets sufficiently upvoted, I'll accept it. – Olivier Grégoire Mar 17 '22 at 20:22
  • i can confirm this worked for me, thanks ! – overflowed Nov 16 '22 at 08:40
  • 1
    Yes it works. The step where it overrides /etc/wsl.conf is dangerous though, in case there is something there. The first step is really necessary, removing the symlink. – Henrique Bucher Nov 17 '22 at 22:02
  • 2
    In my case I had to reload WSL afterwards, with the wsl --shutdown command in PowerShell. – dbernard Jan 20 '23 at 21:43
  • 2
    This issue refers to the same thing and emphasises that /etc/resolv.conf may start out as a symlink so it needs to be deleted first to create a regular file; there's a bug in WSL so even with generateResolvConf=false you need sudo chattr +i /etc/resolv.conf which apparently suppresses that. And note /etc/wsl.conf can also have a [boot] section line systemd=true but I have no advice on what to do about that. – NeilG May 08 '23 at 00:12
  • Confirmed working on my case – JCm Sep 14 '23 at 07:55
  • This solution isn't permanent - at least not in my experience. I need to run it every 2-3 hours. – Slbox Jan 03 '24 at 21:43
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

  • there is a problem here. /etc/resolv.conf is a symlink and you will be overwriting the file pointed by the symbolic link. You need to remove the symlink first and only them populate. – Henrique Bucher Nov 17 '22 at 22:06