234

Recently I installed WSL Ubuntu 18.04 on my Windows machine, but nothing seems to work properly, because I have no internet access.
I tried a few commands and sudo apt update says 'Connection failed' and ping google.com literally takes forever as you can see in this screenshot:


I also checked nano /etc/resolv.conf and made sure that the nameservers are right, completely disabled Kaspersky and its firewall, disabled Hyper-V, reinstalled WSL a few times and even disabled Windows Defender. Nothing helped.

So do you have any ideas what the issue could be here?

CC BY-SA 4.0
3
  • 15
    Can this question be migrated to the proper site instead of closed here?
    – leftjoin
    Feb 12, 2022 at 10:50
  • 2
    "sudo nano /etc/resolv.conf" add 1.1.1.1, then ping google.
    – scott
    May 6, 2022 at 8:16
  • 4
    TL;DR (also incomplete information below): sudo nano /etc/resolv.conf, put # before existing entries, add nameserver 8.8.8.8 on its own line, save (Ctrl+X; Y; Enter), ping google.com to make sure it worked, then to make sure it doesn't get overwritten: sudo nano /etc/wsl.conf, add [network] on its own line, then generateResolvConf = false on the next line, save, and you're done.
    – Andrew
    Jul 11, 2023 at 23:06

21 Answers 21

481

The reason this error occurs is because Windows automatically generates resolv.conf file with wrong nameserver.

To resolve this issue, follow the following steps.

  1. Locate the file by running the following command:

    sudo nano /etc/resolv.conf

You will see the following in the file:

# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/resolv.conf
# [network]
# generateResolvConf = false
nameserver xxx.xx.xx
  1. Change the nameserver value to 8.8.8.8 and save the file. You should now be able to connect to the internet.

  2. If you are able to connect to the internet now then you may also need to stop WSL from resettin this file when opening future terminals. You can do that by running these commands:

    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
    

    (Special thanks to comments from @kanekotic and @Echo9k for this final step.)

CC BY-SA 4.0
12
241

Open Command Prompt as an Administrator and type these commands:

netsh winsock reset 
netsh int ip reset all
netsh winhttp reset proxy
ipconfig /flushdns

Reboot your machine.

https://github.com/microsoft/WSL/issues/3438#issuecomment-410518578

CC BY-SA 4.0
16
62

Recipe which worked for me. (WSL2 + Cisco AnyConnect)

  1. Connect VPN and get DNS servers list, we will need it later (execute in elevated PowerShell)
Get-DnsClientServerAddress -AddressFamily IPv4 | Select-Object -ExpandProperty ServerAddresses
  1. Get search domain (execute in PowerShell)
Get-DnsClientGlobalSetting | Select-Object -ExpandProperty SuffixSearchList
  1. Open WSL and run:
sudo unlink /etc/resolv.conf # this will unlink the default wsl2 resolv.conf

# This config will prevent wsl2 from overwritting the resolve.conf file everytime
# you start wsl2
cat <<EOF | sudo tee -a /etc/wsl.conf
[network]
generateResolvConf = false
EOF

cat <<EOF | sudo tee -a /etc/resolv.conf
nameserver 10.50... # The company DNS/nameserver from the command in step 1
nameserver 10.50... # The company DNS/nameserver from the command in step 1
nameserver 10.50... # list all nameservers from step 1
nameserver 8.8.8.8
nameserver 8.8.4.4
search this.searchdomain.com # The search domain that we got from step 2
EOF

# Make the new /etc/resolve.conf immutable
sudo chattr +i /etc/resolv.conf 
  1. Change Cisco Anyconnect metric from default 1 to 6000 inside powershell
Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000

This needs to be done each time VPN connects. See here https://gist.github.com/machuu/7663aa653828d81efbc2aaad6e3b1431 how to automate this task.

  1. Restart wsl2 on the same elevated powershell, then you can open up wsl2 and it should connect to the internet.
Restart-Service LxssManager

Taken here: https://jamespotz.github.io/blog/how-to-fix-wsl2-and-cisco-vpn

CC BY-SA 4.0
6
  • 3
    This is the only thing which worked for me. Especially step 4 seems necessary, I wonder what it does. Thanks. Nov 4, 2021 at 9:55
  • 1
    What a life saver, I was able to get it to work on my work laptop finally.
    – Daniel
    Feb 20, 2022 at 11:13
  • 1
    This was the only hint that worked for me even without using Cisco VPN. So I left the Step 4. It seems that it is necessary to add the company and the external ns to get it working.
    – macbert
    Feb 23, 2022 at 10:24
  • 2
    This was very useful, It helped me fix the WSL access to internet, even when I dont have a VPN set up (anymore)
    – unJordi
    May 27, 2022 at 20:33
  • With these changes internet stopped working on WSL completely, nice
    – Gino Pane
    Dec 14, 2023 at 12:27
40

Copying the recipe that worked for me.
Using WSL 2 on Windows 10.

Solution found at https://github.com/microsoft/WSL/issues/5336#issuecomment-653881695


Steps

  1. Open Powershell or Cmd as Administrator
    and run each of these commands:

    wsl --shutdown
    netsh winsock reset
    netsh int ip reset all
    netsh winhttp reset proxy
    ipconfig /flushdns
    

  2. Hit the Windows Key,
    type Network Reset,
    hit enter.

    You should see this window.
    Click "Reset now".

    Network Reset magic WSL fix

  3. Restart Windows

If you're lucky, WSL 2 should now be able to sudo apt-get update && sudo apt-get upgrade.


Weird edge case

For me I am still unable to ping sites, but internet seems to work everywhere else in WSL 2.

For instance, ping stackoverflow.com (or pinging any site)
results in "100% packet loss".

However curl --location stackoverflow.com -i
results in a successful response.

And sudo apt-get update && sudo apt-get upgrade now works.


Troubleshooting steps before finding the above recipe:

Hadn't used WSL 2 in months, was trying to sudo apt update from a Debian distro's terminal when I discovered my internet wasn't working.

Spent an hour configuring my WSL's /etc/resolv.conf and /etc/wsl.conf and troubleshooting the auto-config bug (https://github.com/microsoft/WSL/issues/3928). Still didn't work.

Then I found this page. Tried the posted directions. Didn't work.

Finally found this posted solution, and WSL 2 suddenly works perfectly.


Took me hours trying to troubleshoot this, hope this is useful to someone someday.

CC BY-SA 4.0
9
  • 1
    I can confirm that my linux subsystem have internet connection but DNS is broken. Your solution about changin /etc/resolve worked for me. This WLS2 is behind a third party firewall. Jan 15, 2021 at 18:50
  • 2
    I followed this and ended up bricking my WiFi for what it's worth.
    – WillB3
    Mar 10, 2021 at 21:59
  • 1
    @WillB3: Oh snap. Really sorry to hear that. In case I (or anyone else) runs into your issue in the future, could you please elaborate? How severe, and where? Mar 10, 2021 at 22:51
  • 2
    Ended up being that I needed to turn WLAN AutoConfig back to "Automatic" Startup Type in Services. Not sure how many people would run into the same issue, but if you do, give that a shot.
    – WillB3
    Mar 11, 2021 at 14:44
  • 1
    This approach worked for me, but only in Command Prompt, not in Powershell.
    – rer
    Nov 8, 2021 at 23:09
27

If you fixed it changing the DNS but WSL2 keeps overwriting keep reading.

  1. Replace the nameserver Similarly to @Jeffrey Kilelo's
# Replace the nameserver
sudo rm /etc/resolv.conf
sudo bash -c 'echo "nameserver 1.1.1.1" > /etc/resolv.conf'

If you're getting Operation not permitted. Change it manually making use of sudo nano /etc/resolv.conf

  1. To prevent WSL 2 from overwriting this value run the following commands:
# Turn off generateResolvConf
sudo bash -c 'echo "[network]" > /etc/wsl.conf'
sudo bash -c 'echo "generateResolvConf = false" >> /etc/wsl.conf'
sudo chattr +i /etc/resolv.conf
CC BY-SA 4.0
1
  • 5
    First, create /etc/wsl.conf, then reboot WSL WSL --shutdown - WSL will delete resolve.conf after this manipulation, and after that create a new resolve.conf. No chattr is required.
    – t7e
    May 28, 2022 at 23:54
19

For me this issue arise when I switch from one network to another. Lets say for example I was working at office wifi and then I went home and start using home wifi. The above https://stackoverflow.com/a/63578387/1409707 answer worked for me. It seems like this is an actual isse, so till Windows comes up with a solution I had to find an easier way to do it every time. I copied the commands and created a bat file, kept it on desktop. So, everytime I switch the wifi connection/network I run the bat file as administrator and restart the system. I hope this helps to someone who is trying to figure out why it is happening and how to know an easy way to fix it,(not a permanent) one though.

create a new .bat file with following contents.

I named my file as networkreset.bat

wsl --shutdown
netsh winsock reset
netsh int ip reset all
netsh winhttp reset proxy
ipconfig /flushdns
netsh winsock reset
shutdown /r 

everytime you switch the wifi/network run the script as adminstrator and restart.

After restarting internet works on WSL on the connected network.

CC BY-SA 4.0
2
  • 2
    Interesting.... you have netsh winsock reset listed twice in the script. Is that on purpose? We should run that at the beginning and at the end?
    – TLS
    Feb 23, 2021 at 21:58
  • 1
    Have you found any permanent solution? I tried everything but still, nothing worked. Sep 28, 2021 at 9:09
16

In my case if not on VPN, the internet on my WSL works. When I connect to VPN it suddenly stops working. There is a relevant discussion (still open the day I'm posting) on internet loss on WSL while on VPN here.

This solution presented before for this question works but requires reboot:

netsh winsock reset 
netsh int ip reset all
netsh winhttp reset proxy
ipconfig /flushdns

The other solution presented before:

sudo nano /etc/resolv.conf

with the change of the nameserver to 8.8.8.8 or 1.1.1.1 or any other random address it does not work for me while on VPN.

What solved it though is a hybrid of this second solution and the discussion presented above:

Step 1:

While on VPN in Powershell:

ipconfig /all

Search for your adapter that is linked to your VPN connection. Usually you can find in the description something like

"CISCO Anyconnect ..."

Search in that block the addresses of your DNS servers (for me first DNS server didn't work so I took the second)

Step 2: Taken from that same discussion here, in PowerShell:

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

Step 3:

While on VPN on my WSL :

$ sudo echo "nameserver <the DNS server address from point 1>" > /etc/resolv.conf

The drawback of this solution (as for the previous ones) is that you need to do it at every new launch of your WSL. To make it a bit easier one can create a short .sh script to automate it. On your WSL:

$touch restore_internet_connection.sh
$nano restore_internet_connection.sh

place the below text inside:

echo "nameserver <the output from point 1>" > /etc/resolv.conf

Next time you have the issue you just repeat step 2 and:

$sudo restore_internet_connection.sh
CC BY-SA 4.0
2
16

For me the nameserver was no more pingable. So I did the below and it worked for me.

  • type "Turn Windows features on or off" in windows prompt, open the app enter image description here
  • disable Linux subsystem in Windows features enter image description here
  • disable hyper-v (all subcomponents) enter image description here
  • Click "Ok" and follow to restart
  • enable Linux subsystem in Windows features enter image description here
  • enable hyper-v enter image description here
  • Click "Ok" and follow to restart

Unfortunately I could not remove Ubuntu as that contained my work. I had tried everything in this and other stackoverflow posts, with no success.

But the above worked for me and without any loss of work.

CC BY-SA 4.0
3
6

So I don't know if this helps anyone, but I installed Docker on Windows and followed the Microsoft instructions to install the WSL2 engine as recommended by Docker.

Then installed Ubuntu 18.04 LTS with WSL2 and ran into exactly the same problem - no internet. All I really want to be able to do is build some docker images to test, on my work laptop, so WSL2 with no internet was a total no go for me.

After a lot of poking around various forums and guides including this thread, here is what finally worked for me:

  • I reinstalled Docker with WSL2
  • I installed Ubuntu 20.04 LTS from the Microsoft Store, but set the WSL version to 1 using the command prompt as follows

This first command shows the list of distros:
C:\users\xyz> wsl -l -v

NAME STATE VERSION
Ubuntu-20.04 Running 1
docker-desktop-data Running 2
docker-desktop Running 2

So now use the following command to set the WSL version - substitute your distro name and use 1 to switch to WSL v1

C:\users\xyz> wsl --set-version

Results

  1. At this point I had a fully working Ubuntu distribution in WSL, with full access to the internet and none of the annoyances of not being able to access update archives etc. But, no connectivity between the Ubuntu distro and my Docker desktop.
  2. To solve the last piece of the puzzle - I used the guidance provided on this link . Just follow the instructions directly from there - it is rather too long to reproduce here.

Bottom line: I have a WSL1 Ubuntu distro, it connects to the internet and I can build docker images using the CLI in Ubuntu using Dockerfiles without any issues whatsoever.

CC BY-SA 4.0
6

NOTE - This fix addresses DNS resolution issues in WSL. Absurd settings might cause the WSL distro to not have any internet connection at all.

The fix is to set the DNS of your Ethernet/WiFi adapter to your preferred choice. WSL uses the DNS of your host machine. commands like wget were working for me, however the commands like apt update didn't seem to work - basically it couldn't resolve the ubuntu archive URL.

As seen in this image, the DNS value is empty. It's inferred from the DNS of the host machine.

wsl_adapter_settings

Here is the image for adapter properties of the host machine where the DNS is provided manually. If you choose the automatic DNS option for the host, some things might not work on the DNS provided by your ISP.

host_adapter_settings

It is recommended to use OpenDNS, Google DNS or CloudFlare DNS since these are quite fast and reliable. Use DNSBenchmark to find the fastest one for your connection. I would like to highlight that it is strictly your choice.

CC BY-SA 4.0
0
4

Non from above worked. But switching to WSL 1 worked!

so in PowerShell as an administrator:

wsl --set-default-version 1

then reinstall Ubuntu

CC BY-SA 4.0
2
4

I used this answer to network restart several times, but the last time it didn't work.

It only returned to work after uncheck compression to 'Temp' folder (%TEMP%)

compact /u /s:"%TEMP%" /i /Q

https://github.com/microsoft/WSL/issues/5336#issuecomment-770494713

And so, network restart steps.

Command line refer:

https://www.tenforums.com/tutorials/26340-compress-uncompress-files-folders-windows-10-a.html

CC BY-SA 4.0
2
  • If you want to keep %TEMP% compressed, just decompress the WSL swap file: %TEMP%\ApppData\Local\Temp\swap.vhdx Jun 1, 2021 at 20:39
  • According to my experience on Win10.21H2, One-time compact /u %TEMP%\swap.vhdx is not enough. I have to compact /u %TEMP%\ , so that the %TEMP% dir itself is NOT marked compact. Otherwise, everytime we wsl --shutdown then start a WSL instance, the %TEMP%\swap.vhdx is re-created and inherit the compact flag from %TEMP% and the problem re-arise.
    – Jimm Chen
    Mar 4, 2022 at 16:05
4

Make sure NTFS compression is disabled for the following file:

%TEMP%\swap.vhdx

(i.e., normally {2,3,4}: C:\Users\%USERNAME%\AppData\Local\Temp\swap.vhdx)

File properties window for %TEMP%\AppData\Local\Temp

This could also happen if you have moved your entire %TEMP% folder to another location.

References:

CC BY-SA 4.0
1
3

Okay, I know this thread hasn't had much activity in a while. I spent DAYS trying to fix this on a work laptop, because the corporate setup doesn't allow an 8.8.8.8 (Google DNS) address, not only did I have to edit the WSL2 /etc/wsl.conf

#/etc/wsl.conf
[network]
generateResolvConf = False

I also had to write a PowerShell script to write from Windows into the WSL2 folder the actual DNS server address. I don't know if it's because of how locked down my work PC is or what, but I can only use the DNS server my Windows machine uses, and WSL2 always pulls its own IP address to populate the resolv.conf file, which has no DNS server.

Anyone else suffering through this issue, here's how I fixed it. Save this off as whatever you want (in my case, debian.ps1, run from a Admin PowerShell prompt ./debian.ps1), I am using an imported customized Debian Buster WSL2 distro set as default; if you're using something else not set as default, you'll have to change the last line (wsl.exe) to launch the correct distribution:

# Get the DNS server of the Windows machine, save into variable nameserver
$nameserver = Get-WmiObject -Namespace root\cimv2 -Query "Select dnsserversearchorder from win32_networkadapterconfiguration" | where {$_.DNSServerSEarchOrder -ne $null} | select -ExpandProperty DNSServerSearchOrder
# Convert nameserver object into a string
$nameserver = Out-String -InputObject $nameserver
# Run Set-Contents (sc) to write the resolv.conf file in a public location as it has DOS formatted line endings written by PowerShell, not readable by Linux
sc -Path 'c:\Users\Public\Documents\resolv.conf' -Value ('nameserver ' + $nameserver) -Encoding utf8
# Convert the DOS formatted file into UNIX format for WSL2 and write it in the proper place (\etc\resolv.conf, its primary location is \\wsl$\[distro_name] from Windows)
[string]::Join( "`n", (gc 'c:\Users\Public\Documents\resolv.conf')) | sc '\\wsl$\debian\etc\resolv.conf'
wsl.exe

Note if your distribution isn't under \wsl$\debian\ you'll need to change it to where it actually resides. I know this is not pretty, and pulls from many different solutions posted all over the internet, but it's the only one that works with my corporate administered PC and group policies. Hope this saves someone a lot of hassle on a company administered PC. Note if you're connecting via VPN I don't think this will work - I also haven't tested in the office with Coronavirus, it's running on home WiFi. There may be some adjustments needed if you're on a company network directly.

CC BY-SA 4.0
3
  • This actually worked for me; however, your sc abbreviation is not a valid alias for the Set-Content cmdlet. SC (C:\Windows\System32\sc.exe) is a command line program used for communicating with the Service Control Manager and services. It is on the env path and Get-Alias sc does not return a positive response. Dec 19, 2022 at 19:37
  • Also, the WSL path is \\wsl.localhost for example \\wsl.localhost\Ubuntu-22.04\etc. However, I was not able to use Set-Content to write to the resolv.conf, but you can edit it with sudo vi /etc/resolv.conf and just paste the content. Dec 19, 2022 at 19:42
  • Interesting, it works for me no problem just as I pasted it here. I just run it from PowerShell with a: .\debian.ps1 - likely your setup is different as the Get-Alias sc returns a valid handle to Set-Content
    – Matt
    Dec 24, 2022 at 16:48
2

Unfortunately the only solution for me was:

  • uninstall any Ubuntu (18.10 and 20.04)
  • remove Linux subsystem in Windows features
  • restart
  • enable Linux subsystem in Windows features
  • restart
  • Reinstall Ubuntu 20.04

This is with Win10 V1909 (OS Build 18363.1379). I had tried everything in this and other stackoverflow posts, with no success.

I'm only suggest the above steps as an absolute last resort.

CC BY-SA 4.0
2

In my case, WSL network starts not working when I installed WSL2 requirements and upgraded my wsl to version 2.

It can resolve domain to IP but can't reach out to IP over internet.

I updated OS and wsl2 modules to latest and tried all workaround solutions but it didn't work.

Finally, I found the solution.

  1. Go to Device Manager and check View > Hidden Device
  2. Uninstall all Hyper-V Virtual Switch Extensions
  3. Remove "Windows Sub-system for Linux" from Features
  4. Restart
  5. Add "Windows Sub-system for Linux" from Features

For me, distro remained as version 2 after that, but it's working! It's worth to try.

CC BY-SA 4.0
2

I installed wsl and Ubuntu 20.04.

After troubleshooting for days, this is what worked for me.

I want to believe that those experiencing this issue like me are running wsl version 2.

This issue was resolved as soon as I switched back to wsl version 1 without the need for further configuration.

To see how to effect this change, kindly follow follow this link Change wsl version

CC BY-SA 4.0
1

I had ufw installed on WSL2 with debian 10, after removing it and restarting Windows it worked.

CC BY-SA 4.0
1

This happenned to me when I was trying to install MySQL-Server on WSL2 and messed up with ssh@local host trying to access root on Ubuntu.
The system itself got really weird (Sometimes I wish there were alternatives for Ubuntu) so I tried restarting the vEthernet(WSL) adapter and rebooting the computer, and it was back working again.

CC BY-SA 4.0
-3

You can solve this problem by just type this command in your terminal

-> sudo systemctl restart NetworkManager

CC BY-SA 4.0
1
-4

For me, it was an issue with VPN service (NordVPN service) which cause many problems. Just stop its service from services.msc.

CC BY-SA 4.0
1
  • 2
    This helps only if you don't really need that VPN connection. But what if you need exactly that VPN to get to a certain page, from WSL?
    – userfuser
    Dec 17, 2021 at 16:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.