命令行工具不读系统代理。这篇把常用工具的配置方法集中列出来,按需要取用。
假设你的混合端口是 7897(在 Clash Verge 的端口设置里确认,不同版本默认值不同)。
先试环境变量:能覆盖一大半
大部分遵循 Unix 惯例的工具都认这几个变量:
# macOS / Linux / WSL
export http_proxy=http://127.0.0.1:7897
export https_proxy=http://127.0.0.1:7897
export all_proxy=socks5://127.0.0.1:7897
export no_proxy="localhost,127.0.0.1,::1,*.local,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16"# Windows PowerShell(当前会话)
$env:HTTP_PROXY = "http://127.0.0.1:7897"
$env:HTTPS_PROXY = "http://127.0.0.1:7897"
$env:NO_PROXY = "localhost,127.0.0.1,::1,*.local":: Windows CMD
set HTTP_PROXY=http://127.0.0.1:7897
set HTTPS_PROXY=http://127.0.0.1:7897做成 shell 函数方便开关:
# ~/.zshrc 或 ~/.bashrc
proxyon() {
export http_proxy=http://127.0.0.1:7897
export https_proxy=$http_proxy
export all_proxy=socks5://127.0.0.1:7897
export no_proxy="localhost,127.0.0.1,::1,*.local"
echo "✓ proxy on"
}
proxyoff() {
unset http_proxy https_proxy all_proxy no_proxy
echo "✗ proxy off"
}Git
Git 有自己的配置系统,环境变量对 HTTPS 协议有效,对 SSH 协议无效。
# HTTPS 协议
git config --global http.proxy http://127.0.0.1:7897
git config --global https.proxy http://127.0.0.1:7897
# 取消
git config --global --unset http.proxy
git config --global --unset https.proxy
# 查看当前配置
git config --global --get-regexp proxy只对特定域名走代理(推荐,避免公司内网 Git 也走代理):
git config --global http.https://github.com.proxy http://127.0.0.1:7897SSH 协议的 Git([email protected]:xxx.git)不走 http.proxy,需要配 SSH:
# ~/.ssh/config
Host github.com
HostName github.com
User git
Port 22
# macOS / Linux
ProxyCommand nc -X connect -x 127.0.0.1:7897 %h %p
# Windows(Git Bash 自带 connect.exe)
# ProxyCommand connect -H 127.0.0.1:7897 %h %pnpm / yarn / pnpm
# npm
npm config set proxy http://127.0.0.1:7897
npm config set https-proxy http://127.0.0.1:7897
npm config delete proxy && npm config delete https-proxy # 取消
# yarn (v1)
yarn config set proxy http://127.0.0.1:7897
yarn config set https-proxy http://127.0.0.1:7897
# pnpm
pnpm config set proxy http://127.0.0.1:7897
pnpm config set https-proxy http://127.0.0.1:7897配置写在 ~/.npmrc,可以直接编辑:
proxy=http://127.0.0.1:7897
https-proxy=http://127.0.0.1:7897
noproxy=localhost,127.0.0.1,.company.comPython / pip
# 单次使用
pip install --proxy http://127.0.0.1:7897 requests
# 永久配置
pip config set global.proxy http://127.0.0.1:7897
pip config unset global.proxy配置文件位置:
- Windows:
%APPDATA%\pip\pip.ini - macOS/Linux:
~/.config/pip/pip.conf
[global]
proxy = http://127.0.0.1:7897
index-url = https://pypi.tuna.tsinghua.edu.cn/simplerequests 库自己也读环境变量,代码里也能显式指定:
proxies = {
"http": "http://127.0.0.1:7897",
"https": "http://127.0.0.1:7897",
}
requests.get(url, proxies=proxies)Go
# Go modules 走官方代理(通常够用,不需要走本地代理)
go env -w GOPROXY=https://proxy.golang.org,direct
# 国内镜像
go env -w GOPROXY=https://goproxy.cn,direct
# 私有仓库不走 proxy
go env -w GOPRIVATE=*.company.com,github.com/myorg/*
# 需要走本地代理时(比如拉私有 GitHub 仓库)
go env -w GOPROXY=direct
export HTTPS_PROXY=http://127.0.0.1:7897Go 的 go get 遵循 HTTP_PROXY / HTTPS_PROXY 环境变量。
Java / Maven / Gradle
Maven(~/.m2/settings.xml):
<settings>
<proxies>
<proxy>
<id>clash</id>
<active>true</active>
<protocol>http</protocol>
<host>127.0.0.1</host>
<port>7897</port>
<nonProxyHosts>localhost|127.0.0.1|*.company.com</nonProxyHosts>
</proxy>
</proxies>
</settings>Gradle(~/.gradle/gradle.properties):
systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=7897
systemProp.https.proxyHost=127.0.0.1
systemProp.https.proxyPort=7897
systemProp.http.nonProxyHosts=localhost|127.0.0.1|*.company.comJVM 命令行参数:
java -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=7897 \
-Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=7897 \
-Dhttp.nonProxyHosts="localhost|127.0.0.1" -jar app.jarRust / Cargo
~/.cargo/config.toml:
[http]
proxy = "http://127.0.0.1:7897"
[https]
proxy = "http://127.0.0.1:7897"curl / wget
# curl 单次
curl -x http://127.0.0.1:7897 https://example.com
curl --socks5 127.0.0.1:7897 https://example.com
# curl 永久(~/.curlrc)
echo 'proxy = "http://127.0.0.1:7897"' >> ~/.curlrc
# wget 永久(~/.wgetrc)
cat >> ~/.wgetrc <<'EOF'
use_proxy = on
http_proxy = http://127.0.0.1:7897
https_proxy = http://127.0.0.1:7897
EOF系统包管理器
# apt(/etc/apt/apt.conf.d/95proxy)
echo 'Acquire::http::Proxy "http://127.0.0.1:7897";' | sudo tee /etc/apt/apt.conf.d/95proxy
echo 'Acquire::https::Proxy "http://127.0.0.1:7897";' | sudo tee -a /etc/apt/apt.conf.d/95proxy
# dnf/yum(/etc/dnf/dnf.conf 里加)
# proxy=http://127.0.0.1:7897
# Homebrew(macOS)—— 读环境变量
export ALL_PROXY=socks5://127.0.0.1:7897
brew install xxx一张速查表
| 工具 | 配置方式 | 是否读环境变量 |
|---|---|---|
| curl / wget | -x 参数或 .curlrc | ✅ |
| Git (HTTPS) | git config http.proxy | ✅ |
| Git (SSH) | ~/.ssh/config 的 ProxyCommand | ❌ |
| npm / yarn / pnpm | .npmrc | ⚠️ 部分版本 |
| pip | pip.conf | ✅ |
| Go | 环境变量 + GOPROXY | ✅ |
| Maven | settings.xml | ❌ |
| Gradle | gradle.properties | ❌ |
| Cargo | .cargo/config.toml | ✅ |
| Docker daemon | systemd drop-in | ❌ |
| apt / dnf | 各自配置文件 | ⚠️ sudo 下会丢 |
| Homebrew | 环境变量 | ✅ |
为什么设了环境变量还是不行
更省事的方案:开 TUN
上面这些配置加起来很繁琐,而且每装一个新工具可能又要配一遍。
开 TUN 模式后,所有命令行工具自动走代理,一个都不用配。
代价是需要虚拟网卡权限,且要注意把内网段加进直连规则:
prepend-rules:
- IP-CIDR,10.0.0.0/8,DIRECT,no-resolve
- IP-CIDR,172.16.0.0/12,DIRECT,no-resolve
- IP-CIDR,192.168.0.0/16,DIRECT,no-resolve
- DOMAIN-SUFFIX,company.com,DIRECT验证
# 看当前出口 IP
curl https://api.ipify.org
# 看 Git 走没走代理(-v 会打印连接信息)
GIT_CURL_VERBOSE=1 git ls-remote https://github.com/git/git 2>&1 | head -20
# 看 npm 配置
npm config list | grep -i proxy也可以打开 Clash Verge 的连接页,执行命令时观察有没有新连接出现——有就说明流量走过来了。
小结
- 先试环境变量,能覆盖 curl、pip、Go、Cargo、Homebrew 等一大批
- Git、npm、Maven、Gradle、Docker 有自己的配置系统,必须单独配
- SSH 协议要用 ProxyCommand,或者改用 443 端口
no_proxy必须设,否则本地和内网服务全废sudo记得加-E- 嫌麻烦就开 TUN,但要配好内网直连规则
相关文档
容器里的 127.0.0.1 不是宿主机。这篇给出 Docker 构建期、运行期、daemon 拉取镜像三种场景的配置方法,以及 WSL2 网络模式差异下的两套方案。
让全家设备零配置接入。讲清楚三种接入方式(旁路由、TProxy、TUN)的取舍、完整的配置与 systemd 服务、防火墙规则,以及不影响家人上网的容错设计。
external-controller 提供的 RESTful API 全表,怎么用它切换节点、查连接、重载配置,怎么部署 Web 面板,以及几个实用的自动化脚本示例。