WSL2 VPN连接配置

Created on: February 24, 2025

Answered using GPT-4o by Chat01

Question

向我推荐如何使用wsl-vpnkit来连接windows和wsl2的流量。要求步骤详细,区分好终端和linux终端的操作。

Answer

使用 wsl-vpnkit 连接 Windows 和 WSL2 的流量,可以让 WSL2 共享 Windows 主机的 VPN 连接。下面是详细的步骤,包括 Windows 终端和 WSL2 终端的操作。


步骤 1:安装 wsl-vpnkit

(在 Windows 终端中运行)

  1. 打开 PowerShell 或 CMD(管理员权限)

  2. 安装 scoop(如果尚未安装)

    powershell
    Set-ExecutionPolicy RemoteSigned -Scope CurrentUser irm get.scoop.sh | iex
  3. 使用 scoop 安装 wsl-vpnkit

    powershell
    scoop install wsl-vpnkit

    或者手动下载 wsl-vpnkit 释放到一个合适的目录。

  4. 运行 wsl-vpnkit 并检查是否工作

    powershell
    wsl-vpnkit.exe

    这将启动 wsl-vpnkit,默认监听 np:\\.\pipe\wsl-vpnkit


步骤 2:配置 WSL2 以使用 wsl-vpnkit

(在 WSL2 终端中运行)

  1. 创建 WSL2 的 resolv.conf 配置 先禁用 WSL2 自动生成的 DNS:

    sh
    sudo rm /etc/resolv.conf echo "nameserver 192.168.67.1" | sudo tee /etc/resolv.conf
  2. 修改 WSL2 网络配置 运行以下命令,让 WSL2 使用 wsl-vpnkit 提供的网络:

    sh
    export HTTP_PROXY=http://192.168.67.1:3128 export HTTPS_PROXY=http://192.168.67.1:3128 export ALL_PROXY=socks5://192.168.67.1:1080
  3. 写入 .bashrc.zshrc(可选) 这样每次启动 WSL2 都会自动应用:

    sh
    echo "export HTTP_PROXY=http://192.168.67.1:3128" >> ~/.bashrc echo "export HTTPS_PROXY=http://192.168.67.1:3128" >> ~/.bashrc echo "export ALL_PROXY=socks5://192.168.67.1:1080" >> ~/.bashrc source ~/.bashrc

步骤 3:测试连接

(在 WSL2 终端中运行)

  1. 检查 DNS 解析

    sh
    nslookup google.com

    如果返回正常的 IP 地址,说明 WSL2 能正确解析域名。

  2. 测试外部网络连通性

    sh
    curl -I https://www.google.com

    如果返回 HTTP 200/301/302,说明代理已正确配置。

  3. 如果 VPN 连接断开,重启 wsl-vpnkit (在 Windows 终端中运行)

    powershell
    taskkill /IM wsl-vpnkit.exe /F wsl-vpnkit.exe

    (在 WSL2 终端中运行)

    sh
    sudo service networking restart

可选优化

1. 设置 Windows 启动时自动运行 wsl-vpnkit

(在 Windows 终端中运行)

powershell
New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name "wsl-vpnkit" -Value "C:\Users\<你的用户名>\scoop\apps\wsl-vpnkit\current\wsl-vpnkit.exe" -PropertyType String

2. 在 WSL2 内部自动连接

可以在 ~/.profile 里加上:

sh
pgrep wsl-vpnkit >/dev/null || nohup /mnt/c/Users/<你的用户名>/scoop/apps/wsl-vpnkit/current/wsl-vpnkit.exe &

这样配置完成后,WSL2 就可以使用 Windows 连接的 VPN 进行访问了。

Question

PS C:\WINDOWS\system32> Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
PS C:\WINDOWS\system32> irm get.scoop.sh | iex
Initializing...
Running the installer as administrator is disabled by default, see https://github.com/ScoopInstaller/Install#for-admin for details.

Answer

你遇到的问题是 Scoop 的安装默认不允许以 管理员权限 运行。你需要使用 普通用户 权限的 PowerShell 终端,而不是 管理员模式 运行。


解决方法

方法 1:用普通用户权限打开 PowerShell

  1. 关闭当前的 PowerShell 终端(因为你现在是管理员模式)。
  2. 重新打开 PowerShell(以普通用户身份)
    • Windows 搜索 PowerShell
    • 右键单击 Windows PowerShell
    • 选择 “以普通用户运行”(不要选择“以管理员身份运行”)
  3. 然后再次执行 Scoop 安装命令
    powershell
    Set-ExecutionPolicy RemoteSigned -Scope CurrentUser irm get.scoop.sh | iex
    这应该可以正常安装 Scoop。

方法 2:强制允许管理员模式(不推荐)

如果你必须在管理员模式下运行 Scoop(一般不推荐),你可以手动启用:

  1. 运行:
    powershell
    irm get.scoop.sh | iex -RunAsAdmin
    但是 Scoop 官方不建议这么做,还是推荐方法 1

方法 3:手动安装 Scoop

如果你仍然遇到问题,可以手动安装:

  1. 打开 PowerShell(非管理员模式)。
  2. 运行:
    powershell
    git clone https://github.com/ScoopInstaller/Scoop.git $env:SCOOP
  3. 配置 Scoop:
    powershell
    [System.Environment]::SetEnvironmentVariable('SCOOP', $env:SCOOP, 'User') $env:Path = "$env:SCOOP\shims;$env:Path"
  4. 运行:
    powershell
    scoop install wsl-vpnkit

如果遇到任何问题,可以告诉我具体的错误信息,我会进一步帮助你!

Share this Q&A