ubuntu under windows subsystem for linux 2 (wsl2) has no internet access [closed]
Asked Answered
L

8

29

I had an Ubuntu under WSL 1 working fine and connecting to the internet.

Just by upgrading to wsl2, I can't ping google.com from Bash. But I can ping if I downgrade to wsl1.

Here is the issue:[WSL2] No network · Issue #4731 · microsoft/WSL


I tried this but it's not working.

Step 1: open Hyper-V manager as admin

enter image description here

Step 2: select Virtual Switch Manager From Actions

enter image description here

Step 3: select WSL

enter image description here

Step 4: select External Network; choose an adapter and connect to the internet

enter image description here


My windows ipconfig Ethernet adapter vEthernet (WSL):

Ethernet adapter vEthernet (WSL):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::30a4:2612:6ee3:2390%142
   IPv4 Address. . . . . . . . . . . : 192.168.0.103
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.0.1

ping 192.168.0.103 from Ubuntu:

xxxxxxxxx@xxxxxxxxxxx:~$ ping 192.168.0.103
PING 192.168.0.103 (192.168.0.103) 56(84) bytes of data.
From 172.22.131.55 icmp_seq=1 Destination Host Unreachable
From 172.22.131.55 icmp_seq=2 Destination Host Unreachable

ip route from Ubuntu:

xxxxxxxxx@xxxxxxxxxxx:~$ ip route
default via 172.22.128.1 dev eth0 
172.22.128.0/20 dev eth0  proto kernel  scope link  src 172.22.131.55

From Ubuntu, tried sudo ip route add default via 192.168.0.103:

xxxxxxxxx@xxxxxxxxxx:~$ sudo ip route add default via 192.168.0.103
RTNETLINK answers: Network is unreachable
Lomasi answered 16/6, 2020 at 10:28 Comment(3)
"Internal network" should be selectedTricuspid
@user1602 thank ,tried all of them , thankfully i have background in hyper-v so i understand what the options do in details, my observation is that it is not a hyper-v or Ubuntu problem but automating the integration between them it's almost a year I rolled back to v1 next major windows update I will try it againLomasi
Possible duplicate of #62315289Aphrodite
T
17

I was having similar issue. I already tried most of the fixes mentioned in the issues section of its github repo. But, none worked for me permanently.

To temporarily gain access, I created a function which seemed to work for me.

net_activate() {
sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'
}

I saved it in .bashrc file. Every time I needed to connect, I ran this command.

Yesterday, I was a bit frustrated with this issue and did many things, and now everything is running fine without calling that function.

So, here's the list of thing I did in sequence:

  • Uninstalled all the VPNs I had, and removed their adapters manually.
  • Removed all the Network Adapters (not only VPN, but all listed, including hidden ones) from Device Manager
  • Ran CCleaner and did a custom clean with every check-boxes selected.
  • Did Network Reset. (To do this, simply press "Win + S" and type "network reset")

After restarting my PC, internet in WSL2 was working completely fine.

Trickle answered 21/8, 2020 at 17:10 Comment(5)
Thanks for this! My problem was that sometimes WSL2 would lost the connectivity and I had to reboot to work again. This worked like a charm :)Resht
This is a great bandaid solution until Microsoft gets this issue fixedInvestment
You can edit the file /etc/resolv.conf ( sudo vi /etc/resolv.conf and change nameserver value to a fixed DNS e.g, nameserver 8.8.8.8 No need to add it to bashrcServer
@Ritesh, You need to do this again and again, hence the function.Trickle
echo -e "[network]\ngenerateResolvConf = false" | sudo tee -a /etc/wsl.conf and sudo unlink /etc/resolv.conf might help to make the fix a bit more permanent (see Jonathan Bowman Network issues? sectionEnduring
N
17

I tried many of the other suggestions here, but only the following worked for me (doing a networking reset, and rebooting Windows 11):

github.com/microsoft/WSL/issues/4275#issuecomment-846553905

wsl --shutdown
netsh winsock reset
netsh int ip reset all
netsh winhttp reset proxy
ipconfig /flushdns
netsh winsock reset
shutdown /r 
Nuptials answered 5/4, 2022 at 17:45 Comment(4)
worked for me. (WSL2, Windows 10)Embroideress
worked for me too, thanks ser :*Jaggy
Great! Worked for me on Win11 and WSL2Rosauraroscius
Thank you so much! My WSL2 didn't work all of a sudden. This works for me on Windows 10.Gusty
L
5

##This a a hack (hot fix) not a permanent solution

Inside ubuntu i had to :

  • add manual ip assignment to network adapter
  • My router Dynamic ip rang 100-200
  • My ip address is 192.168.0.103/255.255.255.0
  • My ip address is 192.168.0.103/24

note you have to enable external network in the wsl hyper-v network virtual switch (see step 1 to 4 in the question )

bash commands :


sudo ip a flush dev eth0
sudo ip a add 192.168.0.5/24 dev eth0
sudo ip r add default via 192.168.0.1

you have to repeat theses command every restart of the vm (or you can script it)

##Ref. :

enter image description here

Note : there is about 14 issues open on the subject of wsl networking so i don't suggest enabling it (on 2020-06-17 maybe it will be better after the issues close)

Lomasi answered 16/6, 2020 at 20:6 Comment(2)
Sharing the internet connection in host OS also works. I have a virtual adapter that cannot be set to external mode in hyper v. Using ICS got me around this issue.Sumptuous
This one worked for me (doing a networking reset, and rebooting Windows 11): github.com/microsoft/WSL/issues/4275#issuecomment-846553905Nuptials
R
1

I had the same issue of losing internet access in WSL (running Ubuntu 20.04) when converting it to version 2. Reverting to version 1 promptly restored internet. Converting back and forth switched internet on and off consistently. Internet access outside the WSL was always fine.

Trying to repair the Ubuntu installation didn't solve the issue. Neither did meddling with network interface settings or rebooting my PC.

I then followed @sgalpha01's suggestion to run CCleaner and that did the trick:

  • A Custom Clean with all boxes (like a 100) checked except for (the rather impacting / time consuming):
    • Windows Explorer/Network passwords
    • System/Start Menu Shortcuts
    • System/Desktop Shortcuts
    • Advanced/Wipe Free Space
    • Google Chrome/Saved Passwords
  • A Registry Clean-up
  • Removal of a couple of obsolete and unrelated (I think) programs.

After that internet access in the WSL was restored without rebooting the PC.

Many thanks to @sgalpha01! For anyone trying this fix: since no reboot seems to be required for the internet to reappear, you can try out Custom Clean with more selective settings until internet (hopefully) returns.

Russophobe answered 18/12, 2020 at 23:39 Comment(0)
C
1

Well referring to the post by @shortski on Dec 19 '20 higher up on this screen...CC Cleaner worked on first attempt for me ! I only had a few checkboxes ticked. The only one I found logically could have an impact would be:

  • System: DNS Cache

Anyway here is the single attempt CC cleanup list I used:

System: All except:

  • Windows Error Reporting
  • Start Menu Shortcuts
  • Desktop Shortcuts

Advanced:

  • Environment Path
  • Custom Files and Folders

That was it. Happy sudo apt update:ing !

Cephalopod answered 19/3, 2021 at 17:55 Comment(0)
C
0

I found a permanent solution:

1 - Under Hyper-V, Set the "wsl" virtual switch to "bridge"

2 - Under "Control Panel\Network and Internet\Network Connections", set the "wsl" network interface to "DHCP" and automatic DNS

This link provide general guidance but it is not specific to WSL : following something like : https://www.veeam.com/blog/how-to-configure-hyper-v-virtual-switch.html

Contracted answered 28/11, 2020 at 5:50 Comment(1)
What do you mean by bridge? I have 3 options, External network, Internal Network or private network.Waggoner
G
0

The simple procedure that works is to modify the c:\users\%USERPROFILE%\.wslconfig file to be:

[wsl2]
memory=8GB
**swapFile=0**
swap=0

For some reason someone at microsoft changed the name of the swap config parameter. Stragely, when swapfile is defined. network works like a whistle.

Microsoft: Please decouple swalfile from network. And consider supporting both names for the swap.

Granthem answered 2/9, 2021 at 7:21 Comment(2)
swap and swapFile are both still valid config settings: github.com/MicrosoftDocs/wsl/blob/master/WSL/…Anni
I've had swap=0 for a long time now, and still no internetNuptials
S
0

If you just need quick internet access,

sudo nano /etc/resolv.conf

comment out any existing nameservers with '#', and add

nameserver 8.8.8.8

and save it. Now you should have internet, for a while. No reboot needed.

From https://github.com/microsoft/WSL/issues/4275#issuecomment-896117669

Swats answered 31/8, 2022 at 16:43 Comment(1)
Still not workingInnocency

© 2022 - 2025 — McMap. All rights reserved.