DNS not working on WSL

10,789

Solution 1

This is a problem with WSL, not Ubuntu or Windows. There are lots of solutions out there, the best one I found https://gist.github.com/coltenkrauter/608cfe02319ce60facd76373249b8ca6

It also includes VPN info, but the steps work without the VPN also.

  1. Run these commands in the Ubuntu terminal.

    cd ~/../../etc # Go to etc folder in WSL.
    echo "[network]" | sudo tee wsl.conf # Create wsl.conf file and add the first line.
    echo "generateResolvConf = false" | sudo tee -a wsl.conf # Append wsl.conf the next line.
    wsl --terminate Debian # Terminate WSL in Windows cmd, in case the OS is Ubuntu not Debian.
    cd ~/../../etc # Go to etc folder in WSL.
    sudo rm -Rf resolv.conf # Delete the resolv.conf file.
    
  2. In Windows cmd, PowerShell or terminal with the VPN connected do: Get-NetIPInterface or ipconfig /all for getting the DNS primary and secondary.

  3. With the DNS primary and secondary gotten from step 2. replace the numbers in the next step in the X.X.X.X

  4. echo "nameserver X.X.X.X" | sudo tee resolv.conf (Create resolv.conf and append the line.)

  5. echo "nameserver X.X.X.X" | sudo tee -a resolv.conf (Append the line in resolv.conf)

  6. wsl --terminate Debian (Terminate WSL in Windows cmd, in case is Ubuntu not Debian).

  7. sudo chattr +i resolv.conf

  8. Finally in Windows cmd, PowerShell or terminal:

    Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000
    

Credit: @MartinCaccia, @yukosgiti, @machuu and @AlbesK:
https://github.com/microsoft/WSL/issues/4277
https://github.com/microsoft/WSL/issues/4246

Solution 2

WSL is getting its IP address (and it's DNS server, netmask and other stuff) from Windows' DHCP Server. Either fix it on Windows, or fix the DNS server on 172.22.192.1.

This is a Windows problem, not a Ubuntu problem.

And No, I don't know how Windows does DHCP.

Share:
10,789

Related videos on Youtube

LightJack05
Author by

LightJack05

Updated on September 18, 2022

Comments

  • LightJack05
    LightJack05 over 1 year

    I'm currently using an Insider build of Windows 11. (22458)

    However, I had this problem a few times on other builds. (and wsl worked before)

    When I start wsl, I'm not able to do a DNS request. (Pinging an IP address directly works.)

    I tracked this problem down to the file: /etc/resolv.conf

    It includes the DNS server address.

    The default is: 172.22.192.1

    However, this one doesn't work.

    If I change it to 1.1.1.1 (Cloud Flare DNS) it works again.

    However, after every restart of wsl it changes it back to the default.

    There is a file called wsl.conf in which I can disable generating this file, but if always just deletes the file entirely after every reboot.

    So how can I fix this?

    My first Idea was to completely reset wsl, but neither resetting the app, nor reinstalling WSL via features fixed it.

    Does anyone have another idea what to do?

  • LightJack05
    LightJack05 over 2 years
    Hi, the problem is: I dont have a DNS on this IP. I dont even know what this IP is. Windows is getting its DNS from my router at 192.168.178.1.
  • NotTheDr01ds
    NotTheDr01ds over 2 years
    Welcome to Ask Ubuntu and thanks for your answer! I noticed you already have a downvote, and that's likely (although you can never be sure) because your answer boils down to what we call a "link-only answer", which isn't typically allowed here and may be deleted. While it's okay (and encouraged) to provide a link as attribution, we ask that you include the relevant details of how to solve the problem directly in your answer. Thanks!
  • David
    David about 2 years
    Hello. Link only answers are not considered good answers. What about later if this link is gone? If there is info in the link that is the answer include the info in the body of the question. You can have the link as a reference.
  • Evandro Pomatti
    Evandro Pomatti about 2 years
    Thanks @David I've edited it.