Windows10 WSL2 Ubuntu / Debian # no network
Solution 1
Most probably, the Distribution gets its own virtual adapter, first there are some steps you might try:
-
Need to check if the packets really go through the Windows firewall Then check
%systemroot%\system32\LogFiles\Firewall\pfirewall.log
-
If packets are not going through firewall most likely the distribution gets it's own Virtual Adapter, check what IP gets distribution from inside Debian with:
ifconfig
or if you don't have ifconfig
:
perl -MSocket -le 'socket(S, PF_INET, SOCK_DGRAM, getprotobyname("udp"));
connect(S, sockaddr_in(1, inet_aton("8.8.8.8")));
print inet_ntoa((sockaddr_in(getsockname(S)))[1]);'
or ipconfig
on the Windows WSL2 host machine and look what IP takes the machine unde WSL adapter
- If you need to have access to the internet through the Windows IP check this issue: https://github.com/microsoft/WSL/issues/4150
The work around is to use a script that does :
a. Get Ip Address of WSL 2 machine
b. Remove previous port forwarding rules
c. Add port Forwarding rules
d. Remove previously added firewall rules
e. Add new Firewall Rules
$remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '"
$found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
if( $found ){
$remoteport = $matches[0];
} else{
echo "The Script Exited, the ip address of WSL 2 cannot be found";
exit;
}
#[Ports]
#All the ports you want to forward separated by coma
$ports=@(80,443,10000,3000,5000);
#[Static ip]
#You can change the addr to your ip config to listen to a specific address
$addr='0.0.0.0';
$ports_a = $ports -join ",";
#Remove Firewall Exception Rules
iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";
#adding Exception Rules for inbound and outbound Rules
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";
for( $i = 0; $i -lt $ports.length; $i++ ){
$port = $ports[$i];
iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectpor t=$port connectaddress=$remoteport";
}
An alternative solution is to go to Hyper-V Manager and change the Virtual Switch that is bound to the physical NIC
Solution 2
The generated file /etc/resolv.conf is:
nameserver 172.24.0.1
..had to change it to
nameserver 8.8.8.8
which resolves the problem
Solution 3
Original Answer:
In my case I was using a VPN in Windows, when disconnecting from the VPN the problem was resolved.
Edit:
However, this is becoming a wider issue, it's happened again to me and I've solved it by removing Hyper-V Virtual Switch Extension Adapter.
The two solutions most widely used at the moment for this issue are:
1 Prevent /etc/resolfv.conf to become "corrupted"
- Create a file:
/etc/wsl.conf
. - Put the following lines in the file
[network]
generateResolvConf = false
- In a
cmd
window, runwsl --shutdown
- Restart WSL2
- Create a file:
/etc/resolv.conf
. If it exists, replace existing one with this new file. - Put the following lines in the file
nameserver 8.8.8.8
- Repeat step 3 and 4. You will see
git
working fine now.
2 Remove corrupted Network Interface Drivers (could be permanent)
Credits to Jaysonsantos who shared it on GitHub
Solution 4
I was experiencing the same issue.
Do cat /etc/resolv.conf
, and see if the output has something like this:
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.X.X.X
You'll need to change the nameserver to 8.8.8.8
, so run sudo nano /etc/resolv.conf
, edit and save the file.
However, this is more or less a temporary solution, as you'll need to do this every time WSL starts. You might wanna run a script that checks the nameserver and updates it to 8.8.8.8 every time you reboot wsl. This change does work for my WSL2-Debian. But I don't need to restart the WSL.
Jundl
Updated on July 05, 2022Comments
-
Jundl almost 2 years
After upgrading from WSL to WSL2
sudo apt-get update
not works any longer. After:
wsl --set-version Ubuntu-18.04 2
Output is:
> sudo apt-get update Err:1 http://security.ubuntu.com/ubuntu bionic-security InRelease Temporary failure resolving 'security.ubuntu.com' Err:2 http://archive.ubuntu.com/ubuntu bionic InRelease Temporary failure resolving 'archive.ubuntu.com' Err:3 http://archive.ubuntu.com/ubuntu bionic-updates InRelease Temporary failure resolving 'archive.ubuntu.com' Err:4 http://archive.ubuntu.com/ubuntu bionic-backports InRelease Temporary failure resolving 'archive.ubuntu.com' Reading package lists... Done W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic/InRelease Temporary failure resolving 'archive.ubuntu.com' W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic-updates/InRelease Temporary failure resolving 'archive.ubuntu.com' W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic-backports/InRelease Temporary failure resolving 'archive.ubuntu.com' W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/bionic-security/InRelease Temporary failure resolving 'security.ubuntu.com' W: Some index files failed to download. They have been ignored, or old ones used instead.
After going back to WSL1 the problem disappears again. Same on Debian and analogous in CentOS .. so WSL2 must have a bug.
The Windows10 build Version is 19041 and was installed today.
Any work around for WSL2? Regards
-
Jundl about 4 yearsthx, customizing firewall settings seems to be a plausible solution, but unfortunatily access is restricted by admin. WSL2 is usable without enabling hyper-v, so alternative solution is not applicable in my case.
-
Jundl about 4 yearsanyway an out-of-the-box solution should be found by ms:)
-
Denis Savenko almost 4 yearsThank you. It's so simple, but it's helped like a charm)
-
oczkoisse almost 4 yearsIf you are using a VPN, you may want to avoid using Google's DNS server that the answer mentions. For example, Mullvad VPN suggests using 10.8.0.1 instead.
-
fegyi001 over 3 yearsthis should be the accepted answer. works like a charm, thanks!
-
Jundl over 3 yearsthe only thing is, that you have to repeat it on every wsl2 start;)
-
nuclear over 3 yearsif you want use external DNS server permanently, add
[network] generateResolvConf = false
to /etc/wsl.conf :) -
Jundl over 3 yearsit should, but changing /etc/wsl.conf didn't worked for me
-
rpf3 over 3 yearsI also had to run
sudo unlink /etc/resolv.conf
and then manually create a new/etc/resolv.conf
-
Natorious over 3 yearsI use a pihole so I had to update
/etc/resolv.conf
to use my pihole's IP, and the key for me to get the changes to stick (after updating the file) was shutting down WSL2 withwsl --shutdown
in a CMD window, and then opening WSL2 again. Once I did that, my nameserver changes remained every time. -
Jon about 3 yearsNote if you're behind a VPN, it's likely 8.8.8.8 will be blocked. In this case, you will need to discover your network's DNS servers and put those in as
nameserver
entries in /etc/resolve.conf file. These can be viewed in Windows withnetsh interface ip show dnsservers
-
Jon about 3 yearsNote if you're behind a VPN, it's likely 8.8.8.8 will be blocked. In this case, you will need to discover your network's DNS servers and put those in as
nameserver
entries in /etc/resolve.conf file. These can be viewed in Windows withnetsh interface ip show dnsservers
-
Edison Pebojot about 3 yearsHello, about the script, do I need to store it to .sh extension. For example, wsl.sh?
-
Eduard Florinescu about 3 years@EdisonPebojot The script is in PowerShell and you run it in Windows, also this problem was in WSL2 at the beginning with user beta testing, I think if the problem is still there you should check first maybe the second most voted answer and then try the steps here
-
Edison Pebojot about 3 yearsThe second most voted answer doesn't work for me. However, the issue you reference to your answer is related to my problem. And it seems a lot of developers is encountering the same problem and the issue is still open. I open an issue on WSL. You might know about the issue, you can help me if you have time. github.com/microsoft/WSL/issues/6660
-
Eduard Florinescu about 3 years@EdisonPebojot Do you have the latest WSL2 if yes, can you just reinstall wsl2?
-
Edison Pebojot about 3 yearsI back to WSL 1. But I found an issue here which is very helpful, and they are working now to solve this issue, github.com/microsoft/WSL/issues/5256
-
Dezzamondo about 3 yearsYOU BEAUTY!! Thanks @Jundl! (inb4 "thanks comments are discouraged" commentators)
-
JwJosefy almost 3 yearsThanks @Jundl!!! I just ran
sudo nano /etc/resolve.conf
and added a linenameserver 8.8.8.8
as the first entry, and voilá! -
Carlos Chávez over 2 yearsThank you, it worked for me I'm using ubuntu 18.04
-
Élio Pereira over 2 years@Jon, +1, that did the trick to me.
-
abhiTronix over 2 yearsThis one worked flawlessly. Thank you.
-
shaswata pal almost 2 yearseven though I wasn't behind a VPN 8.8.8.8 didn't work. querying my DNS server using netsh interface ip show dnsservers as mentioned above and replacing resolv.conf with that value worked for me
-
001001 almost 2 yearsHow do you fix
[ Error writing /etc/resolv.conf: No such file or directory ]
?