【问题标题】:PulseSecure VPN prevents WSL2 internet connectivityPulseSecure VPN 阻止 WSL2 互联网连接
【发布时间】:2021-01-06 09:03:57
【问题描述】:

在我的企业中,在听到社区的好评后,我们开始迁移到基于 WSL2 的解决方案。

但是,在我们连接到企业 VPN 后,WSL 无法连接到外部网站(以及 VPN 内部的网站)。

好像是windows里面的路由问题,怎么解决?

【问题讨论】:

    标签: vpn windows-subsystem-for-linux pulsesecure


    【解决方案1】:

    问题主要出在windows的路由表上。

    解决方案包括:

    • 找出路线
    • 重复的路由条目
    • 修改两个条目的指标
    • 调整 DNS

    找出想要的路由规则

    使用route PRINT -4可以看到现有的路由条目。

    您可以通过手动删除任何 WSL 条目来找出您的 WSL 需要哪些路由条目,直到 WSL 中的连接丢失。 找到之后,可以通过禁用和启用WSL网络来恢复WSL路由条目:

    netsh interface set interface "vEthernet (WSL)" disable
    netsh interface set interface "vEthernet (WSL)" enable
    

    假设我们的规则如下所示:

    192.168.74.32  255.255.255.240         On-link     192.168.74.33   5256
    ^ inet         ^ mask of subnet                    ^Interface      ^Metric (cost of routing)
    

    在此示例中,我们路由到的接口是 WSL 接口。 激活VPN后,这条规则的接口应该改为VPN接口。

    或者,我可以在 WSL 中使用 route,然后查看网关 IP。这实际上是我必须在我的 Windows 机器中的 route PRINT -4 中查找的规则 ip。

    重复条目

    假设我们从route 找到的规则是:

    192.168.74.32  255.255.255.240         On-link     172.22.0.1   1
    ^ inet         ^ mask of subnet                    ^Interface   ^Metric (cost of routing)
    

    172.22.0.1 明明是VPN接口,之前改过。 我们仍然希望任何发往 192.168.74.32 的流量都能到达我们的 WSL 设备。因此,我们将通过以下方式创建另一个路由条目:

    route ADD 192.168.74.32 MASK 255.255.255.240 0.0.0.0 METRIC 2 IF NN
    NN -> interface index of WSL. you can find it out easily from ipconfig /all
    

    很好。现在我们的表中有两个条目

    192.168.74.32  255.255.255.240         On-link     172.22.0.1   1
    192.168.74.32  255.255.255.240         On-link     192.168.74.33   522
    

    请注意,路由成本可能与您在命令中设置的不同。但是,VPN 更有可能获得较小的权重(因此,优先于 WSL 路由条目)。

    修改两个条目的指标

    在这个阶段,我们将让 VPN 路由条目的吸引力低于类似的 WSL 路由条目。

    为此,我们将调整 VPN 路由条目指标,使其高于 WSL 的路由条目。为此,只需执行:

    Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Juniper"} | Set-NetIPInterface -InterfaceMetric 6000
    

    注意:

    • 确保字符串“Juniper”唯一描述您的 VPN 接口描述。 (同样,很容易来自 ipconfig /all
    • 如果您的 WSL 规则高于该值,您可以将 InterfaceMetric 值设置为高于 6000。

    完成后,Mazal Tov,链接完成,您的 WSL 可以连接到互联网(理论上)

    调整 DNS

    这部分可能不会影响您,但仍然是我必须提供的解决方案的一部分。 这个问题已经讨论得够多了,解决方案已经存在。 例如this github repo

    在这个解决方案中,我们只需将nameserver 8.8.8.8 (Google DNS) 添加到 WSL 内的 /etc/resolv.conf 文件中。 无需重新启动,保存更改后应立即应用

    总结成脚本

    我在这个简单但非常原始的脚本中自动化了这个过程。 请注意,该脚本假定 WSL 运行 Centos7 并且不会自动执行 DNS 修改(这需要 WSL 内部的 root 权限)

    #Test VPN is ON
    $VpnIsOn = Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Juniper"} | Measure-Object -Line
    if ($VpnIsOn.Lines -eq 0)
    {
            Write-Host "No VPN is ON!";
            exit
    }
    
    $activeWSLDist = wsl -l -q --running
    if (-Not $activeWSLDist -Contains 'Centos7')
    {
            Write-Host "Turn ON WSL!";
            exit
    }
    
    $dns = wsl -- grep -e allot -e rdlab /etc/resolv.conf | wc -l
    if ($dns -eq "0")
    {
            Write-Host "add 'nameserver 8.8.8.8' in /etc/resolv.conf"
    }
    else
    {
            Write-Host "DNS Already configured"
    }
    
    $gateway_ip = wsl -- route -ne | grep ^[1-9] | cut -d" " -f1
    $gw_mask    = wsl -- route -ne | grep ^[1-9] | column -t | cut -d" " -f5
    $tmp        =  Get-NetIPConfiguration -InterfaceAlias "*WSL*" | select InterfaceIndex
    $tmp -match "\d{1,4}"
    $ifindex = $matches[0]
    
    # recreate the route entry to WSL (pulse secure turs the GW to it own NIC)
    route ADD $gateway_ip MASK $gw_mask 0.0.0.0 METRIC 2 IF $ifindex
    
    # set priorities - VPN to 6000 WSL to 4000 (WSL should be done)
    Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Juniper"} | Set-NetIPInterface -InterfaceMetric 6000
    # Not needed - but good to mention anyways
    #Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "WSL"} | Set-NetIPInterface -InterfaceMetric 4000
    

    【讨论】:

    • 对于真正不专业的Powershell脚本我感到很抱歉,但这实际上是我写下的第一个。它为我的测试用例完成了这项工作
    • 另见杰米的答案,它更简洁,可能在 powershell 中写得更专业
    【解决方案2】:

    将 Pulse DNS 服务器从 Windows 和 DNS 后缀添加到 WSL2 中的 /etc/resolv.conf 后,它对我有用

    现在看起来像下面

    # This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
    # [network]
    # generateResolvConf = false
    search internal.*.com
    nameserver **.***.*.*
    nameserver **.***.*.*
    nameserver ***.**.***.***
    

    上面的*是通配符

    此外,您必须按照上面 cmets 中的说明编辑 /etc/wsl.conf。否则 /etc/resolv.conf 将被覆盖

    我想当这些 DNS 服务器发生变化时我必须刷新上面的地址

    【讨论】:

    • 这与 VPN 无关,您只需在 wsl 上添加正确的 PULSE DNS,它现在正在工作。如果问题是调谐,这不会纠正它。
    【解决方案3】:

    我修改了@Aviv 发布的脚本,并在我包装了 | 之后取得了一些成功。双引号中的字符并取出centos的检查。以下是对我有用的最终产品。

    #Test VPN is ON
    $VpnIsOn = Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Juniper"} | Measure-Object -Line
    if ($VpnIsOn.Lines -eq 0)
    {
            Write-Host "No VPN is ON!";
            exit
    }
    
    $gateway_ip = wsl -- route -ne "|" grep ^[1-9] "|" cut -d" " -f1
    $gw_mask    = wsl -- route -ne "|" grep ^[1-9] "|" column -t "|" cut -d" " -f5
    $tmp        =  Get-NetIPConfiguration -InterfaceAlias "*WSL*" | select InterfaceIndex
    $tmp -match "\d{1,4}"
    $ifindex = $matches[0]
    
    # recreate the route entry to WSL (pulse secure turs the GW to it own NIC)
    route ADD $gateway_ip MASK $gw_mask 0.0.0.0 METRIC 2 IF $ifindex
    
    # set priorities - VPN to 6000 WSL to 4000 (WSL should be done)
    Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Juniper"} | Set-NetIPInterface -InterfaceMetric 6000
    

    【讨论】:

    • 这个脚本对我有用!我还必须编辑 /etc/resolv.conf 并添加在 PulseSecure 虚拟适配器属性中找到的 DNS 条目。
    【解决方案4】:

    这也给我带来了 WSL2 和我们的 Pulse Secure VPN 客户端的问题,但我最终通过安装 wsl-vpn 解决了这个问题

    我尝试了我在网上找到的许多其他解决方法,包括手动添加 DNS 服务器(更改 /etc/resolv.conf)、在启动 VPN 客户端之前启动 WSL2 发行版等,但没有一个对我有用。

    wsl-vpn 脚本化解决方法对我来说确实很有效。

    【讨论】:

    • 这个解决方案不是也成功了吗?
    猜你喜欢
    • 2015-08-22
    • 1970-01-01
    • 2021-06-01
    • 2014-06-14
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多