WSL-2 DNS 无法在 Win 10 上使用 VPN 连接

Hba*_*l99 35 ubuntu vpn windows-10 windows-subsystem-for-linux wsl-2

我的 Windows 10 笔记本电脑上运行着 WSL 版本 2。我使用的是 WSL 发行版 Ubuntu-20.04。当我连接到 VPN 网络时,域名解析不起作用,因此我无法访问 Internet。

使用的 VPN 客户端是“Cisco AnyConnect 安全移动客户端”

我尝试了以下步骤来解决此问题。

  1. 在管理模式下打开 Windows 命令提示符
  2. 执行以下命令
netsh winsock reset
netsh int ip reset all
netsh winhttp reset proxy
ipconfig /flushdns
reboot
Run Code Online (Sandbox Code Playgroud)

这一次有效,我可以访问互联网。但当我断开VPN连接并再次连接时,我又遇到了同样的问题。我尝试再次执行命令并重新启动,但现在不再起作用了。

什么是永久解决此问题的方法?

kra*_*ego 53

使用 VPN 时,WSL2 中的 DNS 转发存在问题(请参阅 github 问题)。另外, Cisco AnyConnect也存在问题。因此,这里有一个解决这些问题的方法。应该适用于 Ubuntu 和 Debian。

解决方法(新 - 自动)

该解决方案是自动的,由EdwardCooke创建(请参阅https://www.frakkingsweet.com/automatic-dns-configuration-with-wsl-and-anyconnect-client/)。这只是他的解决方案在启动 WSL 时更新 resolv.conf 的第一部分。

  1. 重新启用自动生成 resolv.conf(如果禁用)

    通过评论禁用#

    sudo nano /etc/wsl.conf
    
    Run Code Online (Sandbox Code Playgroud)
    sudo nano /etc/wsl.conf
    
    Run Code Online (Sandbox Code Playgroud)
  2. 创建脚本

    #[network]
    #generateResolvConf = false
    
    Run Code Online (Sandbox Code Playgroud)
    sudo nano /bin/vpn-dns.sh
    
    Run Code Online (Sandbox Code Playgroud)
  3. 使其可执行/以 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)
  4. 让它在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

解决方法(旧手册)

  1. 使用Windows powershell查找名称服务器(在 VPN 会话期间)

    nslookup
    
    Run Code Online (Sandbox Code Playgroud)

    您将获得公司名称服务器的 IPv4 地址,复制此地址。

  2. 在 wsl 中禁用resolv.conf生成:

    echo "/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)
  3. 在 wsl 中将您的公司名称服务器添加到resolv.conf

    sudo nano /etc/wsl.conf
    
    Run Code Online (Sandbox Code Playgroud)

    删除其他条目并添加您的公司名称服务器 IP(如果您有辅助名称服务器,请将其添加到单独的行中)

    • nameserver X.X.X.X(其中 XXXX 是您在步骤 1 中获得的地址)
  4. 设置您的 VPN 适配器(如果您有Cisco AnyConnect打开管理员 powershell

    • 找出您的 VPN 适配器名称:(Get-NetIPInterface在我的例子中"Cisco AnyConnect":)
    • 设置适配器指标(将 -Match 替换为您的名字),在我的情况下,我必须在重新启动或 VPN 重新连接后运行此命令
    Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000
    
    Run Code Online (Sandbox Code Playgroud)

    什么是接口度量:用于确定路由,windows使用具有最低度量的接口)

  5. 在 powershell 中重新启动 wsl :wsl.exe --shutdown

  6. 在 wsl run 中测试它:wget google.com- 如果此命令有效,则您已完成。

就我而言,当我尝试通过浏览器(在 Windows 10 上,fe:Intranet)连接到内部内容时,我遇到了 DNS 问题,这是由步骤 4 中设置的高指标值引起的(基本上是禁用 VPN 路由)。所以这里是解决方法的解决方法:

  1. 在 powershell 中检查您的默认指标(VPN 接口)(将 -Match 替换为您的接口名称)
Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Get-NetIPInterface
Run Code Online (Sandbox Code Playgroud)
  1. 当在 Windows 10 上遇到问题时,使用admin powershell恢复此默认值(将末尾的值替换为您的默认值):
Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 1
Run Code Online (Sandbox Code Playgroud)


小智 9

这对我有用。

\n
\n

Anyconnect v4.9 如何打破它:它为 wsl2 添加一条度量为 2 的低\n路由,低于 5256,这会导致 vpn 成为所选路由,\n当然这永远不会起作用。如下所示。c:> route.exe打印\ nnote:\ xe2 \ x80 \ x9c172.17.228.192 255.255.255.240 \ xe2 \ x80 \ x9d是WSL2目标子网。 5256\n172.17.228.192 255.255.255.240 10.255.0.1 10.255.0.71 2 当我将 vpn 路由指标更改为 5500(高于\n5256)时,此问题得到解决,方法如下:控制面板 \xe2\x80\x93 网络 \ xe2\x80\x93 单击 vpn \xe2\x80\x93 属性\n\xe2\x80\x93 IPv4 \xe2\x80\x93 属性,高级 \xe2\x80\x93 自动指标:取消选中它并输入\n5500。

\n
\n

来源:https ://riwingwp.wordpress.com/2020/12/13/anyconnect-bug/

\n

  • 太长了;`控制面板 -> 网络适配器 -> VPN 适配器属性 -> IPv4 -> 高级 -> 接口指标 5500` (2认同)

小智 4

我的 Windows 10 上运行的 WSL 中的 VPN 集成存在问题。您需要将 WSL 重定向到 VPN,请按照以下步骤操作:

步骤 1:从 Windows Power Shell 获取 DNS 地址

>nslookup
Servidor predeterminado:  yyyy.com
Address:  x.x.x.x
Run Code Online (Sandbox Code Playgroud)

或者

>ipconfig /all
Run Code Online (Sandbox Code Playgroud)

STEP-2 打开 Ubuntu-20.04 版本 2 WSL 并打开 /etc/resolv.conf

步骤 3 修改 /etc/resolv.conf 。在第一个位置添加VPN地址(我删除了其他说明,但没有必要),保存文件,然后再次尝试访问。我的文件如下所示:

nameserver X.X.X.X
Run Code Online (Sandbox Code Playgroud)