外观切换
Clash Verge Rev 配合 WSL2、Docker 与 Git 终端代理配置
一、开发工具代理方案快速决策矩阵
| 工具环境 | 方案 1:仅开 TUN 模式是否够用 | 方案 2:不用 TUN 时的显式直指写法 | 推荐度最高做法 |
|---|---|---|---|
| PowerShell / CMD | 是 | $env:HTTPS_PROXY="http://127.0.0.1:7897" | TUN 或临时变量 |
| Git(仅加速 GitHub) | 是 | git config --global http.https://github.com.proxy http://127.0.0.1:7897 | 按域名定向配 Git 代理最不影响内网 |
| Docker Desktop | 视 WSL 后端设置而定 | Settings → Resources → Proxies 填 http://127.0.0.1:7897 | 建议在 Docker 设置显式填写 + Bypass |
| WSL2 (Win11 22H2+) | Mirrored 模式下极佳 | .wslconfig 配 networkingMode=mirrored + autoProxy=true | 微软原生镜像网络模式 |
| WSL2 (Windows 10 老 NAT) | 需配合防火墙放行 | Clash 开 允许局域网 (Allow LAN) + 指向宿主机网关 IP | 升级 Win11 或加防火墙入站规则 |
二、Git 与 PowerShell / npm / pip 精细代理
1. Git 推荐写法(只让 GitHub 走代理,绝不影响公司内网 GitLab)
与其全局把所有 Git 仓库塞进代理导致内网 gitlab.corp 报错,不如按域名前缀精准挂载代理:
bash
# 仅针对 GitHub 走 Clash Verge 默认混合端口 7897(内网/国内仓库保持直连)
git config --global http.https://github.com.proxy http://127.0.0.1:7897
git config --global https.https://github.com.proxy http://127.0.0.1:7897
# 若使用 Git SSH (ssh://git@github.com) 被阻断,可加进 ~/.ssh/config 走 443 + HTTP 代理:
# Host github.com
# Hostname ssh.github.com
# Port 443
# ProxyCommand connect -H 127.0.0.1:7897 %h %p2. PowerShell 当前会话代理函数(放进 $PROFILE 随叫随用)
powershell
function proxy-on {
$env:HTTP_PROXY = "http://127.0.0.1:7897"
$env:HTTPS_PROXY = "http://127.0.0.1:7897"
$env:ALL_PROXY = "socks5://127.0.0.1:7897"
Write-Host "Shell proxy -> 127.0.0.1:7897" -ForegroundColor Green
}
function proxy-off {
Remove-Item Env:HTTP_PROXY, Env:HTTPS_PROXY, Env:ALL_PROXY -ErrorAction SilentlyContinue
Write-Host "Shell proxy cleared" -ForegroundColor Yellow
}三、WSL2 完整配置(Win11 Mirrored 首选 vs Win10 NAT 兼容)
方案 A(推荐 · Windows 11 22H2+):开启 Mirrored 镜像网络
在 Windows 当前用户目录 C:\Users\<你的用户名>\.wslconfig 新建或追加以下内容:
ini
[wsl2]
networkingMode=mirrored
dnsTunneling=true
autoProxy=true保存后在 PowerShell 运行 wsl --shutdown 彻底重启 WSL,再次进入 Ubuntu 后执行测试:
bash
# 检查自动同步到的宿主机代理变量与 127.0.0.1 互通情况
echo $HTTP_PROXY
curl -I https://www.google.com方案 B(Windows 10 / 传统 NAT 架构)
Windows 10 NAT 下 WSL2 处于独立子网,无法直接把 127.0.0.1 指回宿主机:
- 在 Clash Verge Rev「设置」打开 允许局域网(Allow LAN);
- 用管理员 PowerShell 允许 WSL 虚拟网卡访问宿主端口,避免 Windows 防火墙静默拦截:powershell
New-NetFirewallRule -DisplayName "WSL Clash Proxy" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action Allow - 在 WSL2
~/.bashrc或当前终端获取宿主机网关 IP 并导出代理:bashexport HOST_IP=$(ip route show | grep -i default | awk '{ print $3}') export http_proxy="http://${HOST_IP}:7897" export https_proxy="http://${HOST_IP}:7897"
四、Docker Desktop 与原生 Linux dockerd 镜像拉取代理
1. Windows / macOS Docker Desktop
打开 Docker Desktop → Settings → Resources → Proxies → 开启 Manual proxy configuration:
- Web Server (HTTP):
http://127.0.0.1:7897 - Secure Web Server (HTTPS):
http://127.0.0.1:7897 - Bypass proxy settings for these hosts & domains:
localhost,127.0.0.1,*.local - 点击 Apply & Restart。
2. 原生 Linux 服务器或 WSL 内独立 Docker Engine (systemd)
注意在容器构建阶段(docker build)如果还要走代理,仅仅靠环境变量不够,拉取镜像走的是 dockerd 守护进程,需创建 /etc/systemd/system/docker.service.d/http-proxy.conf:
ini
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:7897"
Environment="HTTPS_PROXY=http://127.0.0.1:7897"
Environment="NO_PROXY=localhost,127.0.0.1,docker.mirrors.internal"随后执行 sudo systemctl daemon-reload && sudo systemctl restart docker 重载生效。
五、常见问题(FAQ)
开发与容器代理 FAQ
Windows 11 下让 WSL2 走代理,用 Mirrored 镜像模式还是传统 NAT + Allow LAN 好?
Windows 11 22H2+ 强烈推荐使用官方 networkingMode=mirrored(镜像网络模式)并配合 autoProxy=true,WSL2 可以直接访问宿主机 127.0.0.1:7897 且不用开允许局域网和改复杂防火墙规则;仅老版本 Windows 10 才需退回传统 NAT + Allow LAN。
为什么已经在 Clash Verge 开了系统代理,git clone 和 docker pull 还是超慢或 Connection timed out?
Git 与 Docker 守护进程默认不会主动读取 Windows 用户态的 IE/WinINet 系统代理设置。Git 需要单独设 http.proxy,Docker Desktop 需要在 Settings → Resources → Proxies 填入 127.0.0.1:7897(或者直接开启 Clash Verge TUN 模式)。
开启了 TUN 模式之后,WSL2 还是连接不到外网怎么办?
检查两个位置:1. 确保 Clash Verge 的 TUN 配置启用了 DNS 劫持和 Auto Route;2. 若 WSL2 在传统 NAT 模式下把 DNS 设死向宿主机虚拟网关却被 Windows 防火墙拦截了 UDP 53 入站,建议切回 Mirrored 模式并开启 dnsTunneling=true。
配置了 Git 代理后如何临时访问国内 Gitee 或公司内网 GitLab 不绕远?
使用 git config --global http.https://github.com.proxy http://127.0.0.1:7897 仅针对 GitHub 指定域名走代理,其余内网与国内仓库天然直连!