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

15,244

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
Share:
15,244

Related videos on Youtube

Olivier Grégoire
Author by

Olivier Grégoire

Updated on September 18, 2022

Comments

  • Olivier Grégoire
    Olivier Grégoire over 1 year

    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?

    • Thomas Ward
      Thomas Ward almost 3 years
      /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
      Olivier Grégoire almost 3 years
      @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.
    • Thomas Ward
      Thomas Ward almost 3 years
      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
      Olivier Grégoire almost 3 years
      @ThomasWard sudo find /run -type f, returns only /run/resolvconf/resolv.conf and /run/sudo/ts/myusername. There is nothing else in there.
    • Thomas Ward
      Thomas Ward almost 3 years
      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
      Olivier Grégoire almost 3 years
      @ThomasWard I thought it was a typo because the /etc/resolvconf (or similar) doesn't exist.
    • NotTheDr01ds
      NotTheDr01ds almost 3 years
      @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.
    • Thomas Ward
      Thomas Ward almost 3 years
      @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
      NotTheDr01ds almost 3 years
      @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
      NotTheDr01ds almost 3 years
      @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
      Olivier Grégoire almost 3 years
      @NotTheDr01ds done! but /run/resolvconf/resolv.conf is now deleted when I exit/restart wsl
    • NotTheDr01ds
      NotTheDr01ds almost 3 years
      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
      Olivier Grégoire almost 3 years
      @NotTheDr01ds I can create it indeed, but when I shut WSL down, the file is deleted.
    • NotTheDr01ds
      NotTheDr01ds almost 3 years
      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
      Olivier Grégoire almost 3 years
      @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.
    • Rivered
      Rivered over 2 years
      Bumping, as I am running into the same problem and there is no solution yet
  • Thomas Ward
    Thomas Ward over 2 years
    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.
  • negstek
    negstek about 2 years
    does not work, fill stay reset
  • Olivier Grégoire
    Olivier Grégoire about 2 years
    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.