WSL2 VPN连接配置
बनाया गया: 24 फ़रवरी 2025
बनाया गया: 24 फ़रवरी 2025
向我推荐如何使用wsl-vpnkit来连接windows和wsl2的流量。要求步骤详细,区分好终端和linux终端的操作。
使用 wsl-vpnkit
连接 Windows 和 WSL2 的流量,可以让 WSL2 共享 Windows 主机的 VPN 连接。下面是详细的步骤,包括 Windows 终端和 WSL2 终端的操作。
(在 Windows 终端中运行)
打开 PowerShell 或 CMD(管理员权限)
安装 scoop(如果尚未安装):
powershellSet-ExecutionPolicy RemoteSigned -Scope CurrentUser irm get.scoop.sh | iex
使用 scoop 安装 wsl-vpnkit
:
powershellscoop install wsl-vpnkit
或者手动下载 wsl-vpnkit
释放到一个合适的目录。
运行 wsl-vpnkit
并检查是否工作:
powershellwsl-vpnkit.exe
这将启动 wsl-vpnkit
,默认监听 np:\\.\pipe\wsl-vpnkit
。
(在 WSL2 终端中运行)
创建 WSL2 的 resolv.conf 配置 先禁用 WSL2 自动生成的 DNS:
shsudo rm /etc/resolv.conf echo "nameserver 192.168.67.1" | sudo tee /etc/resolv.conf
修改 WSL2 网络配置
运行以下命令,让 WSL2 使用 wsl-vpnkit
提供的网络:
shexport 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
写入 .bashrc
或 .zshrc
(可选)
这样每次启动 WSL2 都会自动应用:
shecho "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
(在 WSL2 终端中运行)
检查 DNS 解析
shnslookup google.com
如果返回正常的 IP 地址,说明 WSL2 能正确解析域名。
测试外部网络连通性
shcurl -I https://www.google.com
如果返回 HTTP 200/301/302,说明代理已正确配置。
如果 VPN 连接断开,重启 wsl-vpnkit
(在 Windows 终端中运行)
powershelltaskkill /IM wsl-vpnkit.exe /F wsl-vpnkit.exe
(在 WSL2 终端中运行)
shsudo service networking restart
wsl-vpnkit
(在 Windows 终端中运行)
powershellNew-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name "wsl-vpnkit" -Value "C:\Users\<你的用户名>\scoop\apps\wsl-vpnkit\current\wsl-vpnkit.exe" -PropertyType String
可以在 ~/.profile
里加上:
shpgrep wsl-vpnkit >/dev/null || nohup /mnt/c/Users/<你的用户名>/scoop/apps/wsl-vpnkit/current/wsl-vpnkit.exe &
这样配置完成后,WSL2 就可以使用 Windows 连接的 VPN 进行访问了。
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.
你遇到的问题是 Scoop 的安装默认不允许以 管理员权限 运行。你需要使用 普通用户 权限的 PowerShell 终端,而不是 管理员模式 运行。
PowerShell
Windows PowerShell
这应该可以正常安装 Scoop。powershellSet-ExecutionPolicy RemoteSigned -Scope CurrentUser irm get.scoop.sh | iex
如果你必须在管理员模式下运行 Scoop(一般不推荐),你可以手动启用:
但是 Scoop 官方不建议这么做,还是推荐方法 1。powershellirm get.scoop.sh | iex -RunAsAdmin
如果你仍然遇到问题,可以手动安装:
powershellgit clone https://github.com/ScoopInstaller/Scoop.git $env:SCOOP
powershell[System.Environment]::SetEnvironmentVariable('SCOOP', $env:SCOOP, 'User') $env:Path = "$env:SCOOP\shims;$env:Path"
powershellscoop install wsl-vpnkit
如果遇到任何问题,可以告诉我具体的错误信息,我会进一步帮助你!