当 VPN 开启时,如何让 WSL 连接到互联网?

当 VPN 开启时,如何让 WSL 连接到互联网?

我在 Windows 10 上同时使用 VPN(私人互联网访问)和 Ubuntu WSL1,没有任何问题。

然后我升级到了 WSL2 Ubuntu 20.04。我发现 WSL 2 破坏了我的文件重新加载,所以我将版本降回了 WSL1。那段时间我也安装了最新版本的 VPN。(我还安装了最新版本的 VirtualBox(6.1.34),发现它不起作用,所以我重新安装了 6.1.3 版本。)

现在,当我启用 VPN 时,WSL1 Ubuntu 无法连接到互联网。请问我必须检查哪些文件和设置才能解决此问题?

答案1

答案在这里:https://learn.microsoft.com/en-us/windows/wsl/troubleshooting#wsl-has-no-network-connectivity-once-connected-to-a-vpn

  1. 记下 VPN 的 DNS 服务器ipconfig.exe /all
  2. 复制现有的 resolv.confsudo cp /etc/resolv.conf /etc/resolv.conf.new
  3. 取消链接当前的 resolv.confsudo unlink /etc/resolv.conf
  4. sudo mv /etc/resolv.conf.new /etc/resolv.conf
  5. 编辑/etc/wsl.conf并将此内容添加到文件中。(有关此设置的更多信息,请参阅高级设置配置
[network]
generateResolvConf=false
  1. 打开 /etc/resolv.conf 并

    a. 删除文件第一行描述自动生成的注释

    b. 将上面 (1) 中的 DNS 条目添加为 DNS 服务器列表中的第一个条目。c. 关闭该文件。

断开 VPN 连接后,您必须将更改恢复到 /etc/resolv.conf。为此,请执行以下操作:

  1. cd /etc
  2. sudo mv resolv.conf resolv.conf.new
  3. sudo ln -s ../run/resolvconf/resolv.conf resolv.conf

答案2

这可能不是你的问题,但在尝试了许多不同的方法后,我发现这是我的问题。我有这些实验性设置,并且我使用的是 OpenVPN。我在 wsl.conf 中注释了这些设置,一切开始顺利进行。

#[experimental]
#networkingMode=mirrored
#dnsTunneling=true
#autoProxy=true

https://learn.microsoft.com/en-us/windows/wsl/troubleshooting#wsl-has-no-network-connectivity-once-connected-to-a-vpn

WSL connectivity issues with VPNs when Mirrored networking mode is on
Mirrored networking mode is currently an experimental setting in the WSL Configuration. The traditional NAT networking architecture of WSL can be updated to an entirely new networking mode called “Mirrored networking mode”. When the experimental networkingMode is set to mirrored, the network interfaces that you have on Windows are mirrored into Linux to improve compatibility. Learn more in the Command Line blog: WSL September 2023 update.

Some VPNs have been tested and confirmed to be incompatible with WSL, including:

"Bitdefender" version 26.0.2.1
"OpenVPN" version 2.6.501
"Mcafee Safe Connect" version 2.16.1.124

相关内容