我已在 Windows 10 桌面上启用了 WSL2。直到最近,我才能够从 WSL2 访问/连接到主机上安装的应用程序。但是,现在出于测试目的,当我 ping 主机时,我收到“目标主机无法访问”的消息。
一个有趣的观察是,一旦我从 PowerShell 重新启动 WSL2
Get-Service LxssManager | Restart-Service
并在 WSL 命令提示符上快速 ping HOST,我看到初始请求的成功响应,但最终主机变得无法访问。
user@MACHINEM5D:/mnt/c/windows/system32$ ping host.docker.internal
PING host.docker.internal (192.168.29.99) 56(84) bytes of data.
64 bytes from host.docker.internal (192.168.29.99): icmp_seq=1 ttl=127 time=0.772 ms
64 bytes from host.docker.internal (192.168.29.99): icmp_seq=2 ttl=127 time=0.676 ms
From 192.168.16.1 (192.168.16.1) icmp_seq=3 Destination Host Unreachable
From 192.168.16.1 (192.168.16.1) icmp_seq=4 Destination Host Unreachable
From 192.168.16.1 (192.168.16.1) icmp_seq=5 Destination Host Unreachable
另外,我可以从 WSL2 访问互联网。
user@MACHINEM5D:/mnt/c/windows/system32$ cat /etc/resolv.conf
nameserver 8.8.8.8
user@MACHINEM5D:/mnt/c/windows/system32$ ping google.com
PING google.com (172.217.167.206) 56(84) bytes of data.
64 bytes from del03s18-in-f14.1e100.net (172.217.167.206): icmp_seq=1 ttl=117 time=104 ms
64 bytes from del03s18-in-f14.1e100.net (172.217.167.206): icmp_seq=2 ttl=117 time=105 ms
64 bytes from del03s18-in-f14.1e100.net (172.217.167.206): icmp_seq=3 ttl=117 time=109 ms
^C
--- google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
在 Windows 上,hosts 文件包含以下条目
# localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
::1 localhost
# Added by Docker Desktop
192.168.29.99 host.docker.internal
192.168.29.99 gateway.docker.internal
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section
WSL2 上 /etc/hosts 的输出。
user@MACHINEM5D:/mnt/c/windows/system32$ cat /etc/hosts
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateHosts = false
127.0.0.1 localhost
127.0.1.1 INNOXXXXXX5D.XXX.XXX.XXX
127.0.0.1 localhost
::1 localhost
192.168.29.99 host.docker.internal
192.168.29.99 gateway.docker.internal
127.0.0.1 kubernetes.docker.internal
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
我还检查过,我能够从 Windows 主机 ping 通 WLS2 eth0 IP。
C:\Users\XXXX27>ping 172.18.81.68
Pinging 172.18.81.68 with 32 bytes of data:
Reply from 172.18.81.68: bytes=32 time<1ms TTL=64
Reply from 172.18.81.68: bytes=32 time<1ms TTL=64
Reply from 172.18.81.68: bytes=32 time<1ms TTL=64
Reply from 172.18.81.68: bytes=32 time<1ms TTL=64
Ping statistics for 172.18.81.68:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
之前可以正常工作的是从 WSL2 访问安装在 Windows Host 上的 MYSQL。现在它已停止工作。
user@MACHINEM5D:/mnt/c/windows/system32$ mysql -u auth_user -p teamup_auth -h 192.168.29.99
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.29.99:3306' (113)
我将非常感激任何能解决该问题的建议。
谢谢,
尼廷
答案1
您正在尝试使用特定于 Docker Desktop 的网络名称。当不专门使用 Docker Desktop 时,请勿使用此名称。由于技术原因,Docker Desktop 运行时该名称可能可用,但在其他情况下您仍不应使用它。
相反,遵循微软推荐的方式并从中检索内部主机 IP 地址resolv.conf
:
grep "nameserver" /etc/resolv.conf | awk '{print $2}'
知名主机名别名不存在默认情况下。您可以使用您的 PC 的网络pcname.local
名称pcname
。不要漏掉.local
。
此外,请记住,实际访问(超出 ping 范围)将会出现问题,因为就 Windows 防火墙而言,WSL 网络是“公共”的。将应用不同的、更严格的防火墙规则。由于 WSL 网络有点隐蔽,因此您不会收到“允许此应用”弹出窗口。您必须使用高级防火墙配置手动启用或创建所需的规则。
WSL 2 不使用地址192.168/16默认情况下根本不会这样做。相反,每次启动时,它都会从172.16/12。