无法从 WSL2 访问网络

Ani*_*jee 10 networking windows-10 windows-subsystem-for-linux

我无法从 WSL2 子系统访问我的家庭网络。我在里面安装了 Ubuntu。

anirban@Desktop:/mnt/g/BASH_WORKSPACE$ lsb_release -a                                                             No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.2 LTS
Release:        18.04
Codename:       bionic
anirban@Desktop:/mnt/g/BASH_WORKSPACE$ uname -r                                                                   4.19.57-microsoft-standard
Run Code Online (Sandbox Code Playgroud)

WSL2 中的 eth0 具有以下配置:

anirban@Desktop:/mnt/g/BASH_WORKSPACE$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.20.213.230  netmask 255.255.240.0  broadcast 172.20.223.255
        inet6 fe80::215:5dff:fecf:39a5  prefixlen 64  scopeid 0x20<link>
        ether 00:15:5d:cf:39:a5  txqueuelen 1000  (Ethernet)
        RX packets 100  bytes 10544 (10.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12  bytes 936 (936.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

anirban@Desktop:/mnt/g/BASH_WORKSPACE$ ip addr show                                                               1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: bond0: <BROADCAST,MULTICAST,MASTER> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 92:ce:02:d5:ab:9b brd ff:ff:ff:ff:ff:ff
3: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether ce:ec:a2:b5:b2:b0 brd ff:ff:ff:ff:ff:ff
4: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:15:5d:cf:39:a5 brd ff:ff:ff:ff:ff:ff
    inet 172.20.213.230/20 brd 172.20.223.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::215:5dff:fecf:39a5/64 scope link
       valid_lft forever preferred_lft forever
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
Run Code Online (Sandbox Code Playgroud)

WSL2 的路由没有默认路由。所以,我通过 eth0 添加了一个默认路由。

anirban@Desktop:/mnt/g/BASH_WORKSPACE$ ip route
172.20.208.0/20 dev eth0 proto kernel scope link src 172.20.213.230
anirban@Desktop:/mnt/g/BASH_WORKSPACE$ sudo ip route  add default dev eth0                                        anirban@Desktop:/mnt/g/BASH_WORKSPACE$ ip route                                                                   default dev eth0 scope link
172.20.208.0/20 dev eth0 proto kernel scope link src 172.20.213.230
Run Code Online (Sandbox Code Playgroud)

Windows 中的 vEthernet 具有默认配置,并且令人惊讶地不属于 WSL 的子网。

PS C:\Users\tukai> ipconfig

Windows IP Configuration


Ethernet adapter Ethernet:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Ethernet adapter VirtualBox Host-Only Network #3:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::61be:a6f4:e22d:e985%54
   IPv4 Address. . . . . . . . . . . : 192.168.99.1
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :

Wireless LAN adapter Local Area Connection* 1:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter Local Area Connection* 2:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . :
   IPv6 Address. . . . . . . . . . . : 2a02:8108:45c0:263c::67fd
   IPv6 Address. . . . . . . . . . . : 2a02:8108:45c0:263c:a847:a373:36ab:c040
   Temporary IPv6 Address. . . . . . : 2a02:8108:45c0:263c:18a:4a6a:fb9a:3018
   Link-local IPv6 Address . . . . . : fe80::a847:a373:36ab:c040%11
   IPv4 Address. . . . . . . . . . . : 192.168.0.96
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : fe80::f2af:85ff:fea5:c6e6%11
                                       192.168.0.1

Ethernet adapter Bluetooth Network Connection:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Ethernet adapter vEthernet (WSL):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::41b:9492:2bea:cf6c%49
   IPv4 Address. . . . . . . . . . . : 172.20.208.1
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . :
Run Code Online (Sandbox Code Playgroud)

任何人对问题可能是什么有任何线索?注意:我的 wifi 显然可以访问互联网,我正在尝试从 WSL2 子系统 ping 8.8.8.8 以检查互联网连接。我正在使用 Windows 预览版 10.0.18956

谢谢,

Edd*_*Edd 9

我遇到了同样的问题,并且能够通过运行以下命令使其再次工作(我从其他地方得到的,抱歉我不记得在哪里):

在 wsl

修复网络问题,删除自动生成的文件

[root@PC-NAME user]# rm /etc/resolv.conf || true
[root@PC-NAME user]# rm /etc/wsl.conf || true
Run Code Online (Sandbox Code Playgroud)

启用更改 /etc/resolv.conf,启用 Windows 驱动器上的扩展属性

[root@PC-NAME user]# cat <<EOF > /etc/wsl.conf
[network]
generateResolvConf = false
[automount]
enabled = true
options = "metadata"
mountFsTab = false
EOF
Run Code Online (Sandbox Code Playgroud)

使用谷歌域名服务器进行 DNS 解析

[root@PC-NAME user]# cat <<EOF > /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
EOF
Run Code Online (Sandbox Code Playgroud)

退出 Linux WSL

在 Windows 中,cmd 作为管理员:

wsl --shutdown
netsh winsock reset
netsh int ip reset all
netsh winhttp reset proxy
ipconfig /flushdns
Run Code Online (Sandbox Code Playgroud)

Windows 搜索 > 网络重置

  • 我跳过了最后一个网络重置部分,但其余部分仍然有效。在 ubuntu 18.04 和 20.04 上测试。 (2认同)

Rob*_*Coe 1

Best I can do is provide a working example. Note that in my config, the WSL2 adapter (eth0) is on a different subnet from the default gateway, but it will still use the default when the interface has no override. The gateway IP is a virtual tap interface that's provided by the windows host. So it needs to match the IP given to the vEthernet (WSL) interface of the windows host.

WSL2 Bash:

$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.31.121.217  netmask 255.255.240.0  broadcast 172.31.127.255
        inet6 fe80::215:5dff:fe1c:7078  prefixlen 64  scopeid 0x20<link>
        ether 00:15:5d:1c:70:78  txqueuelen 1000  (Ethernet)
        RX packets 247038  bytes 264215849 (264.2 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 81867  bytes 5902917 (5.9 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


$ ip route
default via 172.31.112.1 dev eth0
172.31.112.0/20 dev eth0 proto kernel scope link src 172.31.121.217
Run Code Online (Sandbox Code Playgroud)

From the host (cmd.exe)

Ethernet adapter vEthernet (WSL):

   Connection-specific DNS Suffix  . : 
   IPv4 Address. . . . . . . . . . . : 172.31.112.1
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . :
Run Code Online (Sandbox Code Playgroud)

In your routing table, you added a default gateway to a subnet, instead of a specific IP. Try changing it to match the vEthernet interface:

172.20.208.1
Run Code Online (Sandbox Code Playgroud)

But I would be concerned about messing with the WSL2 networking, in case something changes between now and the final release candidate. I would consider installing a new image and moving your files across. Alternatively, you can use cygwin as a Bash file system for storing files that you want accessible to WSL. This will protect you from munged WSL images, which you may have suffered, depending on how long you've been part of the preview. If you go this route, this should help, https://medium.com/@rcoe67/tips-for-seamlessly-using-cygwin-and-wsl-in-windows-10-9ed1a72ace6c