Hba*_*l99 6 vpn ubuntu windows-10 windows-subsystem-for-linux wsl2
我的 Windows 10 笔记本电脑上运行着 Ubuntu-20.04 版本 2 WSL。一切正常,我有互联网连接。但前提是我没有连接到 VPN 网络。
如果我使用 Cisco AnyConnect 连接到我大学的网络,我将无法再通过 WSL 连接到互联网,而在 Windows 系统中使用例如 firefox 一切正常。我得到: ping: google.de: 名称解析暂时失败
我已经尝试了以下方法:
在管理员模式下打开 windows cmd 并键入以下命令:
netsh winsock reset
netsh int ip reset all
netsh winhttp reset proxy
ipconfig /flushdns
reboot
Run Code Online (Sandbox Code Playgroud)
那一次成功了,我可以访问互联网。但是,一旦我断开 VPN 连接并再次连接,我又遇到了同样的问题。我试图再次执行命令并重新启动,但现在不再工作了。
所以我真的不知道还能做什么。我真的需要在通过 VPN 连接时使用 WSL
小智 37
使用 VPN 时,WSL2 中的 DNS 转发存在问题(请参阅 github 问题)。另外还有一个问题另外, Cisco AnyConnect。因此,这里有一个解决这些问题的方法。应该适用于 Ubuntu 和 Debian。
该解决方案是自动的,由EdwardCooke创建(请参阅https://www.frakkingsweet.com/automatic-dns-configuration-with-wsl-and-anyconnect-client/)。这只是他的解决方案在启动 WSL 时更新 resolv.conf 的第一部分。
重新启用自动生成 resolv.conf(如果禁用)
通过评论禁用#
sudo nano /etc/wsl.conf
Run Code Online (Sandbox Code Playgroud)
sudo nano /etc/wsl.conf
Run Code Online (Sandbox Code Playgroud)
创建脚本
#[network]
#generateResolvConf = false
Run Code Online (Sandbox Code Playgroud)
sudo nano /bin/vpn-dns.sh
Run Code Online (Sandbox Code Playgroud)
使其可执行/以 sudo 身份运行
#!/bin/bash
echo "Getting current DNS servers, this takes a couple of seconds"
/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command '
$ErrorActionPreference="SilentlyContinue"
Get-NetAdapter -InterfaceDescription "Cisco AnyConnect*" | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
Get-NetAdapter | ?{-not ($_.InterfaceDescription -like "Cisco AnyConnect*") } | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
' | \
awk 'BEGIN { print "# Generated by vpn fix func on", strftime("%c"); print } { print "nameserver", $1 }' | \
tr -d '\r' > /etc/resolv.conf
clear
Run Code Online (Sandbox Code Playgroud)
让它在wsl 启动时运行
sudo chmod +x /bin/vpn-dns.sh
echo "$(whoami) ALL=(ALL) NOPASSWD: /bin/vpn-dns.sh" | sudo tee /etc/sudoers.d/010-$(whoami)-vpn-dns
Run Code Online (Sandbox Code Playgroud)
您也可以手动运行它:
sudo /bin/vpn-dns.sh
使用Windows powershell查找名称服务器(在 VPN 会话期间)
nslookup
Run Code Online (Sandbox Code Playgroud)
您将获得公司名称服务器的 IPv4 地址,复制此地址。
在 wsl 中禁用resolv.conf生成:
echo "sudo /bin/vpn-dns.sh" | sudo tee /etc/profile.d/vpn-dns.sh
Run Code Online (Sandbox Code Playgroud)
将此文本复制到文件中(以在 wsl 启动时禁用resolve.conf 生成)
nslookup
Run Code Online (Sandbox Code Playgroud)
在 wsl 中将您的公司名称服务器添加到resolv.conf
sudo nano /etc/wsl.conf
Run Code Online (Sandbox Code Playgroud)
删除其他条目并添加您的公司名称服务器 IP(如果您有辅助名称服务器,请将其添加到单独的行中)
nameserver X.X.X.X(其中 XXXX 是您在步骤 1 中获得的地址)设置您的 VPN 适配器(如果您有Cisco AnyConnect)打开管理员 powershell
Get-NetIPInterface在我的例子中:"Cisco AnyConnect":)Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000
Run Code Online (Sandbox Code Playgroud)
(什么是接口度量:用于确定路由,windows使用具有最低度量的接口)
在 powershell 中重新启动 wsl :wsl.exe --shutdown
在 wsl run 中测试它:wget google.com- 如果此命令有效,则您已完成。
就我而言,当我尝试通过浏览器(在 Windows 10 上,fe:Intranet)连接到内部内容时,我遇到了 DNS 问题,这是由步骤 4 中设置的高指标值引起的(基本上是禁用 VPN 路由)。所以这里是解决方法的解决方法:
Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Get-NetIPInterface
Run Code Online (Sandbox Code Playgroud)
Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 1
Run Code Online (Sandbox Code Playgroud)
小智 7
这似乎是 WSL 2 中的一个错误,请参阅https://github.com/microsoft/WSL/issues/4277。
这里提供的解决方法对我有用:卸载 Cisco AnyConnect 客户端并从Microsoft Store安装该版本。
| 归档时间: |
|
| 查看次数: |
3134 次 |
| 最近记录: |