跳到主要内容

首页 / 博客 / 部署实战

开发工具代理配置速查:Git、npm、pip、Go、Maven、SSH

部署实战2026-06-101653 字约 4 分钟
开发工具代理配置速查:Git、npm、pip、Go、Maven、SSH

命令行工具不读系统代理。这篇把常用工具的配置方法集中列出来,按需要取用。

假设你的混合端口是 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:7897

SSH 协议的 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 %p

npm / 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.com

Python / 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/simple

requests 库自己也读环境变量,代码里也能显式指定:

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:7897

Go 的 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.com

JVM 命令行参数

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.jar

Rust / 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⚠️ 部分版本
pippip.conf
Go环境变量 + GOPROXY
Mavensettings.xml
Gradlegradle.properties
Cargo.cargo/config.toml
Docker daemonsystemd drop-in
apt / dnf各自配置文件⚠️ sudo 下会丢
Homebrew环境变量

为什么设了环境变量还是不行

常见原因工具有自己的配置系统(Maven、Gradle、Docker daemon)—— 必须写配置文件用了 sudo,环境变量没传过去 —— 加 -E环境变量只在当前 shell 生效 —— 新开的终端没有大小写问题 —— 有的工具只认小写 http_proxy工具走的是 SSH 协议而不是 HTTP —— 需要配 ProxyCommandno_proxy 太宽,把目标域名也排除了

更省事的方案:开 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
两种思路的取舍逐个配置精确可控,只有想不需要额外权限工具多时很繁琐适合服务器TUN 模式一次配好,所有工需要虚拟网卡权限可能与容器VPN 冲突适合开发机

验证

# 看当前出口 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,但要配好内网直连规则

相关:Docker 与 WSL2 代理进程规则详解


相关文档

让 Docker 和 WSL2 用上宿主机的 Clash 代理
部署实战 让 Docker 和 WSL2 用上宿主机的 Clash 代理

容器里的 127.0.0.1 不是宿主机。这篇给出 Docker 构建期、运行期、daemon 拉取镜像三种场景的配置方法,以及 WSL2 网络模式差异下的两套方案。

2026-06-141716 字约 4 分钟