告别手动设代理:让 WSL 自动继承 Windows 代理
|
admin
2026年4月23日 9:30
本文热度 20
|
WSL 新版实验功能一行配置搞定,再也不用每次手动 export http_proxy 了。
问题背景
用 WSL 做开发的同学应该都遇到过这个烦恼:Windows 上开着代理软件,但在 WSL 终端里跑 curl、 apt、 pip 依然连不上,还得手动敲:
export http_proxy=http://127.0.0.1:7890export https_proxy=http://127.0.0.1:7890
每次打开新终端都要重来一遍,或者写进 .bashrc 又容易忘记端口改了没更新。
现在有了更优雅的解法——通过 .wslconfig 让 WSL 自动跟 Windows 同步代理。
原理简介
WSL 的实验功能 networkingMode=mirrored 会让 WSL 和 Windows 共用同一套网络栈,两者拥有相同的 IP 地址。在此基础上, autoProxy=true 会让 WSL 自动读取 Windows 的系统代理设置,无需任何手动配置。
关键组合: mirrored 网络模式 + autoProxy,缺一不可。
操作步骤
1. 更新 WSL 到 pre-release 版本
实验功能需要较新的版本,在 PowerShell 中运行:
wsl --update --pre-release
2. 编辑 .wslconfig 文件
该文件位于 Windows 用户目录,用记事本打开:
notepad "$env:USERPROFILE\.wslconfig"
3. 写入以下配置
[wsl2]nestedVirtualization=trueipv6=true[experimental]autoMemoryReclaim=gradualnetworkingMode=mirroreddnsTunneling=truefirewall=trueautoProxy=true
4. 重启 WSL
wsl --shutdownwsl
5. 验证代理是否生效
curl https://ipinfo.io
如果返回的 IP 是代理节点的 IP,说明配置成功。
配置项说明
| |
|---|
networkingMode=mirrored | WSL 与 Windows 共享网络,同一个 IP 地址 |
autoProxy=true | |
dnsTunneling=true | DNS 查询走 Windows 通道,解决 DNS 污染问题 |
firewall=true | |
autoMemoryReclaim=gradual | |
nestedVirtualization=true | 允许在 WSL 内运行虚拟机,Docker 等需要 |
注意事项
- 代理工具需要在 Windows 上保持运行
networkingMode=mirrored 与某些 VPN 软件可能冲突- 如遇到
wsl--shutdown 卡住,可用 taskkill/F/IM wsl.exe 强制终止
配置生效后: 再也不需要在 .bashrc 里写 export http_proxy 了,Windows 代理开着,WSL 自动跟上。
WSL 版本 ≥ 0.67.6 · 需要 pre-release 版本 · 实验性功能
参考资料
- Microsoft Learn — Accessing network applications with WSL
官方文档,介绍 autoProxy、 networkingMode=mirrored、 dnsTunneling 等网络配置项的说明与适用系统要求。 - Microsoft Learn — Advanced settings configuration in WSL (.wslconfig)
.wslconfig 全局配置文件的完整参数参考。 - Microsoft Learn — 使用 systemd 通过 WSL 管理 Linux 服务
WSL systemd 支持文档,涵盖启用方式与架构影响。 - WSL 2.0.0 Release Discussion — microsoft/WSL #10487
WSL 2.0.0 发布讨论,包含 mirrored 网络、autoProxy、autoMemoryReclaim 等实验功能的首次公告。 - MicrosoftDocs/WSL — Network Configuration (DeepWiki)
WSL 网络配置的结构化文档,详细说明 autoProxy 自动设置的环境变量( HTTP_PROXY、 HTTPS_PROXY、 NO_PROXY、 WSL_PAC_URL)。
该文章在 2026/4/27 15:42:37 编辑过