Windows10 WSL2 Ubuntu / Debian # apt-get update failed # no network

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


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"

  1. Create a file: /etc/wsl.conf.
  2. Put the following lines in the file
[network]
generateResolvConf = false
  1. In a cmd window, run wsl --shutdown
  2. Restart WSL2
  3. Create a file: /etc/resolv.conf. If it exists, replace existing one with this new file.
  4. Put the following lines in the file
nameserver 8.8.8.8
  1. Repeat step 3 and 4. You will see git working fine now.

Credits to NonStatic who shared it on github

2 Remove corrupted Network Interface Drivers (could be permanent)

  1. First go to device manager enter image description here

  2. Show hidden devices enter image description here

  3. Delete all Hyper-V Virtual Switch Extension Adapter enter image description here

Credits to Jaysonsantos who shared it on GitHub


Most probably, the Distribution gets its own virtual adapter, first there are some steps you might try:

  1. Need to check if the packets really go through the Windows firewall enter image description here enter image description here Then check %systemroot%\system32\LogFiles\Firewall\pfirewall.log

  2. 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

  1. 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
[email protected](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 enter image description here