为什么在 VPN 后面使用 WSL 2 的 Ubuntu 中没有网络连接?

Sto*_*row 23 networking vpn ubuntu windows-subsystem-for-linux windows-10-v1909

前提:我正在尝试使用 WSL 将 Ubuntu 作为 Windows 10 应用程序运行。

TL;DR 问题:当 Ubuntu 使用 WSL 2 时,使用公司 VPN 时网络连接“消失”。

试图让 Ubuntu 18.04(从 Microsoft Store 下载)使用WSL 2. 当企业 VPN 关闭,我似乎有网络连接;当我连接到 VPN 时,突然我无法连接(ping等)任何东西。

可能有一系列问题需要处理,所以我将尝试限制这个问题的范围:为什么 Ubuntuwifi0在使用时报告NICWSL 1而在使用时不报告WSL 2(这是否重要)

为什么我只能在使用WSL 2和启用 VPN时连接到任何地方?修复只是添加权利nameserver的问题/etc/resolv.conf吗?如果是这样,什么是有效值?我已经试过了8.8.8.88.8.4.4


Ubuntu 18.04 WSL 1

$ ifconfig wifi0
wifi0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.96  netmask 255.255.255.0  broadcast 10.0.0.255
$ ifconfig eth0
eth0: flags=64<RUNNING>  mtu 1500
        inet 169.254.91.163  netmask 255.255.0.0
Run Code Online (Sandbox Code Playgroud)
$ ping www.google.com # No VPN
PING forcesafesearch.google.com (216.239.38.120) 56(84) bytes of data.
64 bytes from any-in-2678.1e100.net (216.239.38.120): icmp_seq=1 ttl=119 time=19.8 ms
64 bytes from any-in-2678.1e100.net (216.239.38.120): icmp_seq=2 ttl=119 time=22.0 ms
Run Code Online (Sandbox Code Playgroud)
$ ping www.google.com # VPN active
PING forcesafesearch.google.com (216.239.38.120) 56(84) bytes of data.
64 bytes from any-in-2678.1e100.net (216.239.38.120): icmp_seq=1 ttl=119 time=19.8 ms
64 bytes from any-in-2678.1e100.net (216.239.38.120): icmp_seq=2 ttl=119 time=22.0 ms
Run Code Online (Sandbox Code Playgroud)

Ubuntu 18.04 WSL 2

$ ifconfig wifi0
wifi0: error fetching interface information: Device not found
$ ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.34.56  netmask 255.255.255.240  broadcast 192.168.34.63
Run Code Online (Sandbox Code Playgroud)
$ ping www.google.com # No VPN
PING www.google.com (142.250.68.100) 56(84) bytes of data.
64 bytes from lax31s12-in-f4.1e100.net (142.250.68.100): icmp_seq=1 ttl=118 time=27.7 ms
64 bytes from lax31s12-in-f4.1e100.net (142.250.68.100): icmp_seq=2 ttl=118 time=19.6 ms
Run Code Online (Sandbox Code Playgroud)
$ ping www.google.com # VPN active -- Both the generated and hand-edited /etc/resolv.conf failed
^C
Run Code Online (Sandbox Code Playgroud)

更新:
我尝试了@StuartBrock 的有希望的答案,但不幸的是它没有用。

在 Windows 中,ipconfig /all产生了以下内容,我认为是 VPN 适配器的 DNS 值:

Ethernet adapter vEthernet (Default Switch):

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter
...
   DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                       fec0:0:0:ffff::2%1
                                       fec0:0:0:ffff::3%1

Ethernet adapter vEthernet (WSL):

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter #2
...
   DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                       fec0:0:0:ffff::2%1
                                       fec0:0:0:ffff::3%1
Run Code Online (Sandbox Code Playgroud)

以下行已存在于/etc/wsl.conf

[network]
generateResolvConf = false
Run Code Online (Sandbox Code Playgroud)

...并且我已经验证,因此,编辑的内容/etc/resolve.conf在“重新启动”(停止/重新启动 Ubuntu 18.04 LTS Windows 应用程序)后仍然存在。

我不确定fec前缀和%1后缀是什么,但值看起来像 IPV6 地址。所以我继续并相应地更新了我的/etc/resolve.conf

user@LOC-USER-LT:~$ cat /etc/resolv.conf
nameserver 10.0.0.1
nameserver 10.100.98.237
nameserver 10.100.98.21
nameserver fec0:0:0:ffff::1%1
nameserver fec0:0:0:ffff::2%1
nameserver fec0:0:0:ffff::3%1
Run Code Online (Sandbox Code Playgroud)

...随后进行相同的测试,即ping www.google.com,行为与最初描述的行为没有变化。

在 Ubuntu 18.04 LTS Windows 应用程序停止/重新启动后,问题仍然没有改变。

如果我在重新启动 Ubuntu 18.04 LTS Windows 应用程序之前和之后更新/etc/resolve.conf内容以删除fec前缀和%1后缀,问题也没有改变:

nameserver 10.0.0.1
nameserver 10.100.98.237
nameserver 10.100.98.21
nameserver 0:0:0:ffff::1
nameserver 0:0:0:ffff::2
nameserver 0:0:0:ffff::3
Run Code Online (Sandbox Code Playgroud)

我不确定vim的内容有多智能/etc/resolve.conf,但我发现有趣的是它选择红色突出显示这些新的 IPV6 值,好像它认为它们无效:
vim 不喜欢 在此处输入图片说明


更新 2:
我想知道/etc/resolve.conf内容的顺序是否重要,所以我尝试将新的 IPV6 值放在文件的顶部。有趣的是,这确实改变了行为:不是挂起几秒钟然后用 stderr 失败ping: www.google.com: Temporary failure in name resolution,而是立即返回相同的 stderr 消息。

Stu*_*ock 10

我在使用 Cisco AnyConnect 时遇到了类似的问题。我认为 WSL1 是通过 Windows 联网(通过 Hyper-V?),而 WSL2 实际上是一个与 Windows 一起运行虚拟化的 Linux 内核。

我的解决方法是

  1. 在 Windows 中ipconfig /all获取 VPN 适配器的 DNS 值
  2. 在 WSL 中sudo vi /etc/resolv.conf并将 DNS 值添加为行nameserver <DNS IP>

您可以选择添加下面的块/etc/wsl.conf以阻止 resolv.conf 在重新启动时“刷新”(擦除)。但是您需要记住它将来不会自动更新。

   [network]  
   generateResolvConf = false   
Run Code Online (Sandbox Code Playgroud)

至于为什么在连接 VPN 时您没有网络连接,我认为它试图连接到您的 VPN 阻止的“关闭 VPN”DNS。似乎有一些关于 WSL2 和 VPN 的 GitHub 问题,所以我希望在某个时候会出现修复程序。

  • WSL1 的 ***me*** 就是这种情况;) 在这一切结束之前,我们都将成为 resolv.conf 的神。 (2认同)

小智 8

我使用了 ubuntu_20 和 WSL2,我做了以下步骤来解决这个问题:


leo*_*osh 8

有一个简单的解决方法,适用于我在 Windows 10 上使用 WSL2+Ubuntu 20.04 和 Cisco AnyConnect 的情况:

在连接到 VPN 之前启动 WSL:

wsl --shutdown
# disconnect VPN
wsl
# connect VPN again
Run Code Online (Sandbox Code Playgroud)

  • 我对此表示赞同,因为它解决了我的问题。但如果不需要这样做就好了:-) (2认同)

ven*_*ngy 6

我执行了以下操作,在连接到 Windows 主机上的 VPN 的同时,为 WSL 2 VM 提供网络连接。

1.下载发行版wsl-vpnkit.tar.gz并将其复制到C:\Users\<用户名>

2.在C:\Users\<用户名>中打开 PowerShell并运行以下命令

wsl.exe --import wsl-vpnkit $env:USERPROFILE\wsl-vpnkit wsl-vpnkit.tar.gz

wsl.exe -d wsl-vpnkit

wsl.exe -d wsl-vpnkit 服务 wsl-vpnkit 启动

3.为了避免重新运行任何命令,请打开 WSL 2 会话窗口并运行

须藤纳米 ~/.bashrc

在下面添加此命令并保存

wsl.exe -d wsl-vpnkit 服务 wsl-vpnkit 启动 >/​​dev/null 2>&1

.bashrc在每次交互式 shell 启动时运行。

4.关闭并重新打开新的 WSL 2 会话。要确认网络访问,请运行 ping 命令

ping google.com

  • 这救了我的命。wsl-vpnkit 背后的魔力是什么? (2认同)