80

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
  • 3,084
  • 3
  • 15
  • 16

15 Answers15

164

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

Jundl
  • 3,084
  • 3
  • 15
  • 16
  • 2
    If you are using a VPN, you may want to avoid using Google's DNS server that the answer mentions. For example, Mullvad VPN [suggests](https://mullvad.net/en/help/dns-leaks/) using 10.8.0.1 instead. – oczkoisse Jun 25 '20 at 05:24
  • 1
    the only thing is, that you have to repeat it on every wsl2 start;) – Jundl Sep 03 '20 at 07:53
  • 7
    if you want use external DNS server permanently, add `[network] generateResolvConf = false` to /etc/wsl.conf :) – nuclear Oct 13 '20 at 15:16
  • it should, but changing /etc/wsl.conf didn't worked for me – Jundl Oct 14 '20 at 13:08
  • 1
    I also had to run `sudo unlink /etc/resolv.conf` and then manually create a new `/etc/resolv.conf` – rpf3 Nov 18 '20 at 23:12
  • I 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 with `wsl --shutdown` in a CMD window, and then opening WSL2 again. Once I did that, my nameserver changes remained every time. – Natorious Jan 06 '21 at 04:38
  • 2
    Note 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 with `netsh interface ip show dnsservers` – Jon Feb 21 '21 at 03:31
  • Jon's addition is crucial, @Jundl, for some of us. It would be good for others who come to your answer if you could add the step to check for nameserves with `netsh interface ip show dnsservers`. After thinking the VPN was blocking, just getting the right DNS servers was such a relief when it started downloading. – NeilG May 04 '23 at 23:28
57

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/resolv.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

w5m
  • 2,286
  • 3
  • 34
  • 46
JesusIniesta
  • 10,412
  • 1
  • 35
  • 28
  • 9
    Note 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 with `netsh interface ip show dnsservers` – Jon Feb 21 '21 at 03:29
  • even 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 – shaswata pal Jun 29 '22 at 11:16
13

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.

jiawei li
  • 11
  • 2
kelvinelove
  • 550
  • 7
  • 19
9

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
$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 enter image description here

Eduard Florinescu
  • 16,747
  • 28
  • 113
  • 179
  • 1
    thx, 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 Feb 27 '20 at 14:33
  • 1
    anyway an out-of-the-box solution should be found by ms:) – Jundl Feb 27 '20 at 14:41
  • 1
    Hello, about the script, do I need to store it to **.sh** extension. For example, **wsl.sh**? – Edison Pebojot Mar 10 '21 at 02:08
  • @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 – Eduard Florinescu Mar 10 '21 at 10:11
  • 1
    The 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. https://github.com/microsoft/WSL/issues/6660 – Edison Pebojot Mar 10 '21 at 12:17
  • @EdisonPebojot Do you have the latest WSL2 if yes, can you just reinstall wsl2? – Eduard Florinescu Mar 10 '21 at 13:27
  • 1
    I back to WSL 1. But I found an issue here which is very helpful, and they are working now to solve this issue, https://github.com/microsoft/WSL/issues/5256 – Edison Pebojot Mar 12 '21 at 00:52
7

I solved the issue changing /etc/resolv.conf to:

nameserver 8.8.8.8

and then

$ sudo chattr -f +i /etc/resolv.conf

actually 'locks' the file with the +i==immutable attribute and hence cannot be regenerated by the OS. This way the user-modified /etc/resolv.conf will be persistent.

I found creating the /etc/wsl.conf didn't work, but left it anyway:

[network]
generateResolvConf = false
Sergio
  • 124
  • 2
  • 10
5

Run these commands on cmd:

$ wsl --shutdown
$ netsh winsock reset
$ netsh int ip reset all
$ netsh winhttp reset proxy
$ ipconfig /flushdns
$ netsh winsock reset
$ shutdown /r 
srburton
  • 349
  • 3
  • 8
3

I had the same error when I applied Windows NTFS drive compression on disk C:. Further investigation shown, that compression of only two folders leads to that error:

  1. WSL folder (%LocalAppData%\Packages\CanonicalGroupLimited*)
  2. %TEMP% (i.e. C:\Users\<Username>\AppData\Local\Temp)

So you if you have Temporary failure resolving... error with sudo apt-get update on WSL I recommend not to apply NTFS disk compression to system drive at all, or at least keep that two directories uncompressed.

Also don't forget to reboot your WSL in CMD or PowerShell terminal after uncompression with wsl --shutdown

Ornstein89
  • 598
  • 1
  • 6
  • 15
2

For me, I had a fresh Ubuntu 20.04 WSL instance, after installing apache and allowing the apache profile using this command

sudo ufw allow in "Apache"

Then,

sudo service ufw start && sudo ufw enable

I noticed that this issue has triggered and after disabling the firewall again using

sudo ufw disable && sudo service ufw stop

everything worked just fine.

Shaaer
  • 527
  • 5
  • 17
2

it worked for me, i'm using CiscoAnyConnect for VPN

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

Get-NetIPInterface -InterfaceAlias "vEthernet (WSL)" | Set-NetIPInterface -InterfaceMetric 1

Credits: Cisco AnyConnect

Gabriel
  • 31
  • 3
2

Maybe it was a coincidence, but activating Hyper-V by running Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All in Powershell as administrator and restarting appeared to solve the issue for me when nothing else did. I had tried most solutions except the one involving Hyper-V Manager; I was activating Hyper-V to try that one but ultimately I didn't have to do anything else.

If it was a coincidence, it is possible the archive.ubuntu.com servers were temporarily unavailable. The fact that I never lost the ability to ping other addresses supports this. A comment on this AskUbuntu question states:

Sometimes you find the servers unavailable or being down . . . Usually the problem solves itself after a while.

In other words, sometimes this issue is server-side. I don't think anyone here has mentioned that possibility yet.

wyrdcurt
  • 21
  • 2
  • I tried changing the dns in /etc/resolv.conf and ofcourse it works. but its temporary, so while I search for a permanent solution. I removed the hidden hyper-v switch before running the command you mentioned and restart. and now it works without changing anything inside wsl. probably running this command do all the job. without needing to do anything else – Saber Hosney Jan 26 '23 at 16:34
1

Check what antivirus you are using. I was having McAfee and endpoint security enabled. If disconnect End Point security firewall connectivity will work straightaway (no reboot required).

https://kc.mcafee.com/corporate/index?page=content&id=KB91411

1

after editing in /etc/resolv.conf . it resolved issue permanently

# 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 8.8.8.8
Rockinroll
  • 394
  • 6
  • 11
1

This is a known limitation of WSL 2 (WSL 1 vs WSL 2).

Ideal solution is to use WSL 1. Microsoft has recommended work arounds as well (doc), but those require admin privilege:

enter image description here

Arnab Biswas
  • 4,495
  • 3
  • 42
  • 60
0

my solution in windows 10 wsl2 ubuntu 20.04 with a company is this one : windows : ipconfig /all you take the information

Serveurs DNS. . .  . . . . . . . . . . : 10.54.X.X

and on ubuntu

sudo vi /etc/resolv.conf

change the nameserver X.X.X.X by the address serveurs DNS

daday001
  • 91
  • 1
  • 9
-1

If it worked fine before and now it doesn't.

Had the same error, now I can solve it with the following steps:

-Uninstall installed districts. -Uninstall Docker (if you have it) -Open your console in admin mode, and type:

wsl -l

to list the installed distros. -Remove all additional distros from the console with the following command:

wsl --unregister NAME_DISTRO

In NAME_DISTRO change with your distro. Delete each of the distros until you have none.

It will provide you with the following commands, execute them with the terminal in admin mode.

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi

Install kernel if you have it already installed, you should not install it again.

wsl --set-default-version 2

Now if you can install the distro you want from the Microsoft Store.

Zoe
  • 27,060
  • 21
  • 118
  • 148
KO Dev
  • 139
  • 1
  • 5