WSL2 网络不可达

Jac*_*rys 6 windows-networking windows-subsystem-for-linux wsl-2

几周前,WSL 突然无法访问任何 IP 地址,也无法解析任何域。甚至无法访问内部网络 IP。

>lsb_release -a
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.1 LTS
Release:        20.04
Codename:       focal

? neofetch
            .-/+oossssoo+/-.               klewis@NOTEBOOK-KLEWIS
        `:+ssssssssssssssssss+:`           ----------------------
      -+ssssssssssssssssssyyssss+-         OS: Ubuntu 20.04.1 LTS on Windows 10 x86_64
    .ossssssssssssssssssdMMMNysssso.       Kernel: 5.4.72-microsoft-standard-WSL2
   /ssssssssssshdmmNNmmyNMMMMhssssss/      Uptime: 1 hour, 20 mins
  +ssssssssshmydMMMMMMMNddddyssssssss+     Packages: 1405 (dpkg)
 /sssssssshNMMMyhhyyyyhmNMMMNhssssssss/    Shell: zsh 5.8
.ssssssssdMMMNhsssssssssshNMMMdssssssss.   Theme: Adwaita [GTK3]
+sssshhhyNMMNyssssssssssssyNMMMysssssss+   Icons: Adwaita [GTK3]
ossyNMMMNyMMhsssssssssssssshmmmhssssssso   Terminal: Windows Terminal
ossyNMMMNyMMhsssssssssssssshmmmhssssssso   CPU: Intel i7-7820HK (8) @ 2.903GHz
+sssshhhyNMMNyssssssssssssyNMMMysssssss+   Memory: 968MiB / 5942MiB
.ssssssssdMMMNhsssssssssshNMMMdssssssss.   Font: Cantarell 11 [GTK3]
 /sssssssshNMMMyhhyyyyhdNMMMNhssssssss/
  +sssssssssdmydMMMMMMMMddddyssssssss+
   /ssssssssssshdmNNNNmyNMMMMhssssss/
    .ossssssssssssssssssdMMMNysssso.
      -+sssssssssssssssssyyyssss+-
        `:+ssssssssssssssssss+:`
            .-/+oossssoo+/-.
Run Code Online (Sandbox Code Playgroud)
? ifconfig

? ccat /etc/resolv.conf
nameserver 9.9.9.9
nameserver 8.8.8.8

? ccat /etc/wsl.conf
[user]
default=klewis

# Now make it look like this and save the file when you're done:
[automount]
root = /
options = "metadata"

[network]
generateResolvConf = false
Run Code Online (Sandbox Code Playgroud)
? ping 127.0.0.1
ping: connect: Network is unreachable
? ping 192.168.0.1
ping: connect: Network is unreachable
? ping 8.8.8.8
ping: connect: Network is unreachable
? ping google.com
ping: google.com: Temporary failure in name resolution
? nslookup google.com 9.9.9.9
net.c:536: probing sendmsg() with IP_TOS=b8 failed: Network is unreachable
;; connection timed out; no servers could be reached

? ip route
Error: ipv4: FIB table does not exist.
Dump terminated

? ip addr show
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 86:9a:be:53:f0:44 brd ff:ff:ff:ff:ff:ff
3: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 1e:ff:ad:a4:c4:a7 brd ff:ff:ff:ff:ff:ff
4: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
    link/sit 0.0.0.0 brd 0.0.0.0
5: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 00:15:5d:b7:f4:da brd ff:ff:ff:ff:ff:ff
Run Code Online (Sandbox Code Playgroud)
C:\> ipconfig
Ethernet adapter vEthernet (WSL):

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter #7
   Physical Address. . . . . . . . . : 00-15-5D-E5-0C-1B
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::1962:7d4e:a75e:8d62%78(Preferred)
   IPv4 Address. . . . . . . . . . . : 192.168.16.1(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . :
   DHCPv6 IAID . . . . . . . . . . . : 1308628317
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-27-B3-1F-34-9C-B6-D0-DD-8C-CF
   DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                       fec0:0:0:ffff::2%1
                                       fec0:0:0:ffff::3%1
   NetBIOS over Tcpip. . . . . . . . : Enabled
Run Code Online (Sandbox Code Playgroud)
>sudo ip route add default via 192.168.16.1
Error: Nexthop has invalid gateway.
Run Code Online (Sandbox Code Playgroud)

我浏览了 GH 并找到了对其他人有用的解决方案。我尝试过的事情:

jcr*_*aun 27

我刚刚在我的电脑上解决了这个问题。我的背景是:

\n
    \n
  1. 我正在使用 WSL2
  2. \n
  3. 在 Ubuntu 16.04 和 Ubuntu 20.04 上尝试过
  4. \n
  5. 使用 VPN 时我仍然无法连接到互联网
  6. \n
\n

第 1 步:找出 Windows 上的本地网关地址

\n
Wireless LAN adapter Wi-Fi:\n\n\n\nConnection-specific DNS Suffix  . : home.sercomm\n   IPv4 Address. . . . . . . . . . . : 192.168.1.69\n   Subnet Mask . . . . . . . . . . . : 255.255.255.0\n   Default Gateway . . . . . . . . . : 192.168.1.254 <-- this is what we care about\n
Run Code Online (Sandbox Code Playgroud)\n

步骤 2:创建包含以下内容的 /etc/wsl.conf 文件

\n
cat /etc/wsl.conf\n# Enable DNS \xe2\x80\x93 even though these are turned on by default, we\xe2\x80\x99ll specify here just to be explicit.\n[network]\ngenerateResolvConf = false\n
Run Code Online (Sandbox Code Playgroud)\n

步骤 3:删除 /etc/resolv.conf 上 /run/resolvconf/resolv.conf 的符号链接

\n
cd /etc/\nsudo rm resolv.conf\n
Run Code Online (Sandbox Code Playgroud)\n

步骤4:同时删除/run/resolvconf/resolv.conf,为什么不呢?

\n
cd /run\nsudo rm resolvconf/resolv.conf\n
Run Code Online (Sandbox Code Playgroud)\n

步骤 5:创建自定义 /etc/resolv.conf,其中将包含您的网关地址\nsudo vi /etc/resolv.conf

\n

并粘贴此内容(使用步骤 1 中所示的网关地址)

\n

nameserver 192.168.1.254

\n

步骤 6:打开 powershell 并重新启动 wsl

\n
wsl --shutdown\n
Run Code Online (Sandbox Code Playgroud)\n

步骤 7:ping google.com \n此时,假设系统没有自动擦除您的 resolv.conf,并且您仍然保留我们在步骤 5 中添加的配置,您应该能够访问互联网。

\n

谢谢,如果您有任何问题,请随时与我联系。

\n

  • 谢谢,但它不起作用,但我继续尝试了一遍。仍然不起作用......我现在在我的第二台计算机上,它也在那一台计算机上执行。(╯°□°)╯︵┻⁄┻ (2认同)

Not*_*1ds 19

更新:虽然这对OP不起作用,但它继续在这里和那里获得一些赞成票。根据我现在所知,这个特定的答案可能适用于某些用户,因为他们的交换磁盘被压缩或加密%TEMP%(也称为..\AppData\Local\Temp在您的Windows用户配置文件下)。OP 已经为他们的系统排除了这种情况,但如果禁用交换对您有用,那么请确保您的%TEMP%Windows 既未压缩也未加密。


嗯,您没有提到 WSL Github 问题#5437也没有提到#6427,所以这里还有其他可以尝试的内容:

通过添加禁用交换:

[wsl2]
swap=0
Run Code Online (Sandbox Code Playgroud)

到%userprofile%\.wslconfig

似乎是这个评论中很多人的解决方案。搜索“FIB 表不存在”“hyper-v”时遇到此问题

退出 WSL,然后(从 PowerShell 或 CMD):

wsl --shutdown
Run Code Online (Sandbox Code Playgroud)

  • 是的!尝试一切之后,这有效!根据记录,我的磁盘既没有压缩,也没有加密。我什至确保解压我的整个主目录。我尝试了许多线程、github 问题、脚本和文章中的几乎所有内容......并且做到了!我不知道为什么 WSL2 上的 Ubuntu 在“检测到”压缩磁盘时会像这样关闭所有网络接口。在 Linux 中不使用交换的另一个原因。谢谢! (2认同)

Kri*_*nom 5

运行以下命令(并再次访问控制台)为我修复了“网络无法访问”问题

wsl --shutdown
Run Code Online (Sandbox Code Playgroud)

  • 相同的。为我工作。(在windows下运行命令) (3认同)
  • 它起作用了,因为我的机器没有成功终止 (2认同)

小智 5

除了Juan Carlos Martinez Carrillo之外,我还必须运行此命令才能eth0在 WSL2 环境中正确设置。

sudo ifconfig eth0 inet <your_ip_address> netmask <your_netmask> broadcast <your_braodcast_ip>
Run Code Online (Sandbox Code Playgroud)

假设以上面的示例为例,您192.168.1.254在 Windows 上有默认网关,那么命令应如下所示:

sudo ifconfig eth0 inet 192.168.1.42 netmask 255.255.255.0 broadcast 192.168.1.255
Run Code Online (Sandbox Code Playgroud)

然后关闭并重新启动 WSL

wsl.exe --shutdown
Run Code Online (Sandbox Code Playgroud)

在 Windows 终端中。


Jac*_*rys 1

从 build 19042.1052 开始,这又开始工作了,我这边没有任何变化。我不确定实际原因,但由于我无法再重现它,进一步的故障排除似乎没有意义。