No internet connection on WSL Ubuntu (Windows Subsystem for Linux)

419.2k Views Asked by At

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?

21

There are 21 best solutions below

16
On BEST ANSWER

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

0
On

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

0
On

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.

2
On

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

2
On

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.

1
On

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:

15
On

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

To resolve this issue, use 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 resetting 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.)

2
On

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

so in PowerShell as an administrator:

wsl --set-default-version 1

then reinstall Ubuntu

1
On

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
0
On

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.

0
On

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

0
On

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.

0
On

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.

2
On

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
3
On

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.

0
On

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.

3
On

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.

1
On

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

-> sudo systemctl restart NetworkManager

7
On

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

9
On

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.

1
On

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