Skip to content

Instantly share code, notes, and snippets.

@coltenkrauter
Last active May 29, 2025 14:20
Show Gist options
  • Save coltenkrauter/608cfe02319ce60facd76373249b8ca6 to your computer and use it in GitHub Desktop.
Save coltenkrauter/608cfe02319ce60facd76373249b8ca6 to your computer and use it in GitHub Desktop.
Fix DNS resolution in WSL2

Permanent WSL DNS Fix (WSL 2.2.1+)

If you're encountering ping github.com failing inside WSL with a Temporary failure in name resolution, you're not alone — this has been a long-standing issue, especially when using VPNs or corporate networks.

This issue is now fixed robustly with DNS tunneling, which preserves dynamic DNS behavior and avoids limitations like WSL’s former hard cap of 3 DNS servers in /etc/resolv.conf.

DNS tunneling is enabled by default in WSL version 2.2.1 and later, meaning that if you're still seeing DNS resolution issues, the first and most effective fix is simply to upgrade WSL. Upgrading WSL updates the WSL platform itself, but does not affect your installed Linux distributions, apps, or files.

To upgrade WSL, follow these steps,

# Run all of the following in a Windows terminal (PowerShell, Command Prompt, etc.)

# 1. Check your current WSL version
wsl --version

# 2. Close all open *WSL windows* — any Linux terminals running via WSL (Ubuntu, Debian, etc.)

# 3. Shut down the WSL subsystem
wsl --shutdown

# 4. Upgrade WSL
wsl --upgrade

# 5. Verify the upgrade was successful (version should now be >= 2.2.1)
wsl --version

# 6. Open your WSL terminal and test
ping github.com

# 🎉 If it works, drop a comment on this Gist and tell us how happy you are.

If needed, explicitly enable tunneling by creating (source):

# C:\Users\<YourUsername>\.wslconfig
[wsl2]
dnsTunneling=true

That’s it. No more messing with /etc/resolv.conf. No more weird hacks!


🧟 Previous Workarounds (for WSL < 2.2.1 or locked-down systems)

Preserved for historical transparency and for users unable to upgrade WSL.

# In WSL
cd /etc
echo "[network]" | sudo tee wsl.conf
echo "generateResolvConf = false" | sudo tee -a wsl.conf

# Back in Windows
wsl --terminate <DistroName>  # or use wsl --shutdown

# Back in WSL
sudo rm -f /etc/resolv.conf
echo "nameserver 1.1.1.1" | sudo tee /etc/resolv.conf
echo "nameserver 8.8.8.8" | sudo tee -a /etc/resolv.conf
sudo chattr +i /etc/resolv.conf

If you're using a VPN like Cisco AnyConnect:

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

🙌 Credit & Sources

Big thanks to,


If you're still using manual resolv.conf hacks in 2024+, you're solving a problem that's already been solved.

@ten0s
Copy link

ten0s commented Feb 1, 2022

I found that one of my VPN blocks DNS queries to 8.8.8.8.
By default DNS uses UDP. My solution is to force DNS to use TCP instead.

The below configs work with and without VPN.

$ cat /etc/wsl.conf
[network]
generateResolvConf = false
$ cat /etc/resolv.conf
options use-vc
nameserver 8.8.8.8

@m2hd1
Copy link

m2hd1 commented Feb 1, 2022

$ cat /etc/resolv.conf
options use-vc
nameserver 8.8.8.8

@ten0s thank you !!! that fixed the issue for me
I've been looking for a simple solution like this one for a while

@tutods
Copy link

tutods commented Feb 1, 2022

@ten0s you are using WSL2 or WSL1?
For me only working change from WSL2 to WSL1

@ten0s
Copy link

ten0s commented Feb 1, 2022

@tutods WSL2

@tutods
Copy link

tutods commented Feb 1, 2022

So strange. Already try a lot of things and nothing works

@m2hd1
Copy link

m2hd1 commented Feb 1, 2022

@ten0s for some reasons the /etc/resolv.conf gets wiped out (or probably overwritten) after rebooting my machine

although I did check I still had in my /etc/wsl.conf file

[network]
generateResolvConf = false

is there a clean way to force it to always have your recommended resolv.conf settings ?

@mlemnian
Copy link

mlemnian commented Feb 1, 2022

@m2hd1
You need to modify the /etc/wsl.conf file
(within your WSL) like that:

echo "[network]" | sudo tee /etc/wsl.conf
echo "generateResolvConf = false" | sudo tee -a /etc/wsl.conf

@ten0s
Copy link

ten0s commented Feb 1, 2022

@m2hd1, you need to unsure that your Linux is really restarted.

In Windows terminal

Shutdown all

wsl --shutdown

Check

wsl --list -v

Your Linux should be Stopped

Start it again and then check /etc/resolv.conf

@m2hd1
Copy link

m2hd1 commented Feb 1, 2022

@ten0s I tried your steps but the /etc/resolv.conf keeps disappearing after shuting down wsl

I guess as a workaround I could add a small script to my bash profile to create the resolv.conf file

@camerondubas
Copy link

I'm experiencing the same behaviour as @m2hd1. My /etc/resolv.conf gets regenerated even though I have set a /etc/wsl.conf.

@jonshipmannwmg
Copy link

Add this to the /etc/wsl.conf file:

[boot]
command = "printf 'nameserver 8.8.8.8\nnameserver 4.4.4.4' > /etc/resolve.conf"

@ten0s
Copy link

ten0s commented Feb 8, 2022

The Boot setting is only available on Windows 11.

https://docs.microsoft.com/en-us/windows/wsl/wsl-config#boot-settings

@ten0s
Copy link

ten0s commented Feb 8, 2022

If your config keeps getting overwritten review
https://docs.microsoft.com/en-us/windows/wsl/wsl-config#the-8-second-rule

@jdgregson
Copy link

Add this to the /etc/wsl.conf file:

    [boot]
    command = "printf 'nameserver 8.8.8.8\nnameserver 4.4.4.4' > /etc/resolve.conf"

This, but /etc/resolv.conf not /etc/resolve.conf (no 'e').

@ghenadiibatalski
Copy link

what the 8.8.8.8 4.4.4.4? does anybody use a corporate dns or a local router? this is not a solution at all

@ten0s
Copy link

ten0s commented Feb 17, 2022

@ghenadiibatalski 8.8.8.8 and 4.4.4.4 are Google DNS servers

Sure, it's not a complete solution, but I don't need access to corporate network inside WSL.
If such access is needed, read https://gist.github.com/coltenkrauter/608cfe02319ce60facd76373249b8ca6?permalink_comment_id=4040950#gistcomment-4040950

@troy-mac
Copy link

troy-mac commented Feb 28, 2022

Thanks man.. Worked like a charm. We use Palo Alto Global Protect for VPN so I left out the final command of
Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000
This was not necessary for me and everything is working now. Thanks Microsoft updates for breaking this...

@Rodeoclash
Copy link

[boot]
command = "printf 'nameserver 8.8.8.8\nnameserver 4.4.4.4' > /etc/resolve.conf"

These are Google provided DNS servers. Just set the values to whatever DNS server you want to use.

@rkhaled0
Copy link

rkhaled0 commented Mar 8, 2022

+1 Thanks for sharing !

@justinholmes
Copy link

justinholmes commented Mar 15, 2022

@ten0s 4.4.4.4 is not Google DNS. Correct ones are 8.8.8.8 8.8.4.4

@j3N0
Copy link

j3N0 commented Mar 22, 2022

The Boot setting works for me.👍

@dungtv994
Copy link

The solution doesn't work for me.
But when I switch to WSL1, DNS is working well.
Does anyone have a solution?

@jonathanmorgan
Copy link

boot setting given by jonshipmannwmg above works for me in Windows 11. Also, works to manually recreate /etc/resolv.conf each time you boot if you don't do the [boot]. Not sure about windows 10.

@epomatti
Copy link

epomatti commented Apr 15, 2022

Based on this gist I created this simple code:

https://github.com/epomatti/wsl2-dns-fix-config

@Dwerg01
Copy link

Dwerg01 commented Jun 4, 2022

Thank you very much.
I love WSL2, but it still has so many annoying issues. Worked for Ubuntu-22.04

@gustavo-lara-molina
Copy link

Based on this gist I created this simple code:

thnks!!!, this works for me, I downloaded the code manually and after run this DNS works in WSL 2 Ubuntu-18.04 Win11

@epomatti
Copy link

@gustavo-lara-molina nice! happy to help

@heatlill
Copy link

heatlill commented Jun 23, 2022

Based on this gist I created this simple code:

https://github.com/epomatti/wsl2-dns-fix-config

My Environment:

  • Corporate environment (remote VPN).
  • Windows 10 host
  • WSL2
  • Ubuntu 22.04 LTS

Steps:

  1. Added the following to /etc/wsl.conf.
[network]
generateResolvConf = false
  1. Backed up then deleted /etc/resolv.conf.
  2. Created a new /etc/resolv.confusing the DNS servers from my VPN connection.
  3. Ran: chattr +i /etc/resolv.conf (Using the last line from run.sh script in the gist above.)

I've restarted WSL several times and my changes are persisting.

@jordanmiracle
Copy link

jordanmiracle commented Jul 1, 2022

This original solution worked perfectly. Before, the only solution seemed to be recreating .ssh keys on every startup, which doesn't make sense, but it had worked twice. Hopefully, this solution will be persistent across reboots. Thank you!

I should say, the main place I noticed the error was trying to push commits. I just finished the last step and went to push and it worked flawlessly.

@marwin1991
Copy link

  1. Find out nameserver with windows powershell (during VPN Session and without) using nslookup
  2. USe sudo touch /etc/wsl.conf and sudo vim /etc/wsl.conf to add:
[network]                                                                        
generateResolvConf = false
  1. Restart wsl (Windows powershell) using wsl --shutdown
  2. Open WSL and remove using rm -f /etc/resolv.conf
  3. Add new file sudo touch /etc/resolv.conf and sudo vim /etc/resolv.conf with:
nameserver X.X.X.X

nameserver Y.Y.Y.Y
  1. Restart wsl (Windows powershell) using wsl --shutdown
  2. Open WSL and remove using wget google.com and test some you corporate domain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment