Resolution for WSL2 Network Unreachable Issue on Ubuntu and Other Distros

Kontext Kontext event 2022-06-13 visibility 18,613
more_vert
Resolution for WSL2 Network Unreachable Issue on Ubuntu and Other Distros

When upgrading from WSL1 to WSL2 for my Ubuntu 20.04 distro, the virtual machine cannot connect to network. The error messages include: Network is unreachable; Temporary failure resolving; Temporary failure in name resolution, etc. There are many articles on the websites and many of them didn't resolve the problem. This page summarizes the solution for these issues and hopefully it will save you much time.

Issue context

After installing WSL 2 Ubuntu distro following Install Windows Subsystem for Linux on a Non-System Drive, the following error shows up when trying to update apt:

Err:1 http://archive.ubuntu.com/ubuntu focal InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Err:2 http://security.ubuntu.com/ubuntu focal-security InRelease
  Temporary failure resolving 'security.ubuntu.com'
Err:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Err:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal-updates/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal-backports/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/focal-security/InRelease  Temporary failure resolving 'security.ubuntu.com'
W: Some index files failed to download. They have been ignored, or old ones used instead.

Similarly, when ping 8.8.8.8 or any other IP addresses, the following error shows up:

ping: connect: Network is unreachable

When ping any domain name, it shows a different error:

ping: google.com: Temporary failure in name resolution

The environment details are:

  • OS: Windows 10 (10.0.19044)
  • WSL version: 2
  • Distro: Ubuntu 20.04 x64 version or OpenSUSE 42 or any other distros.

When downgrade to WSL 1, these issues are automatically resolved:

wsl --set-verison Ubuntu 1

However when change it back to version 2, the above issues still exist:

wsl --set-verison Ubuntu 2

General fixes

warning Warning - The following approaches probably don't work for you. Please read through the whole article first before you decide to take any actions. For my scenario, it only works after disable compression for Packages folder. Refer to the last section for more details. 

WSL 2 official documentation and many other websites have suggested the following fix:

  1. Backup and delete auto-generated files in WSL:
    # Backup and delete auto-generated files
    sudo cp /etc/wsl.conf /etc/wsl.conf.bk
    sudo cp /etc/resolv.conf /etc/resolv.conf.bk
    sudo rm /etc/resolv.conf || true
    sudo rm /etc/wsl.conf || true

    If wsl.conf doesn't exist in your distro, you can ignore it.

  2. Recreate /etc/wsl.conf using the following command:
    sudo bash -c 'cat <<EOF > /etc/wsl.conf
    [network]
    generateResolvConf = false
    EOF'
  3. Recreate /etc/resolv.conf using the following command:
    sudo bash -c 'cat <<EOF > /etc/resolv.conf
    nameserver 8.8.8.8
    nameserver 8.8.4.4
    nameserver 192.168.1.254
    EOF'

    The above commands use Google DNS and also my WIFI network default gateway as DNS server. You can change it accordingly. You can get it from ipconfig command in your Windows Command Prompt:

    ipconfig
    Wireless LAN adapter Wi-Fi:
    
       Connection-specific DNS Suffix  . :
       IPv4 Address. . . . . . . . . . . : 192.168.1.135
       Subnet Mask . . . . . . . . . . . : 255.255.255.0
       Default Gateway . . . . . . . . . : 192.168.1.254
  4. Exist WSL.
  5. Run Command Prompt as Administrator in Windows and execute the following commands:
    wsl --shutdown
    netsh winsock reset
    netsh int ip reset all
    netsh winhttp reset proxy
    ipconfig /flushdns
  6. Use Windows Search for Network reset. As described by itself, it will reinstall your network adapters. Please be aware of this before you take any actions
    2022061375043-image.png
  7. Restart Windows.

Usually after restart, your WSL network should now work. But unfortunately this approach doesn't work for me. Thus I keep exploring the others options. 

Run ip addr

Run the following command in WSL:

$ ip addr
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: bond0: <BROADCAST,MULTICAST,MASTER> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether ba:bc:6f:b0:f3:8b brd ff:ff:ff:ff:ff:ff
3: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 36:6f:82:06:6f:9a brd ff:ff:ff:ff:ff:ff
4: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
    link/ipip 0.0.0.0 brd 0.0.0.0
5: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
    link/sit 0.0.0.0 brd 0.0.0.0
6: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 00:15:5d:c2:ee:f9 brd ff:ff:ff:ff:ff:ff

The above texts were printed out in my WSL distro. The problem is that eth0 status is DOWN.

As mentioned earlier, for WSL 1 version, this issue does not exist as the way WSL2 works is different from WSL1:

10: eth0: <> mtu 1500 group default qlen 1
    link/ether 9c:b6:d0:15:31:74
    inet 169.254.143.197/16 brd 169.254.255.255 scope global dynamic
       valid_lft forever preferred_lft forever

Final solution

After I tried all the above steps, none of them worked for me. Finally, I found the solution after tremendous searching on the internet: disable compression for C:\Users\<your user id>\AppData\Local\Packages.

Detailed steps

  • Navigate to  C:\Users\<your user id>\AppData\Local\Packages. Replace the placeholder with your own user ID.
  • Right click and choose Properties and then click Advanced button.
  • Uncheck 'Compress contents to save disk space'.
    20220613111559-image.png
  • Click OK button.
  • Apply the changes to the child items and folders too.
  • Shutdown WSL by running command in Windows Terminal or Command Prompt: wsl --shutdown 
  • Restart the computer.

The issue should be gone after restart:

20220613112124-image.png

Enjoy developing with Ubuntu via WSL 2!

More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts