终端代理命令:git 代理、npm、Docker、WSL2 的设置、检查与取消
填写代理地址并选择系统,就能得到每个程序开启代理、检查和取消代理的命令。所有命令都按各程序的官方文档核对,不支持 SOCKS5 的会直接说明。
在浏览器中计算,不上传任何内容
使用 TUN 模式的 VPN(Veli 就是这样工作的)时不需要这些:所有程序的流量会自动走 VPN。WSL 和 Docker 有自己的网络,可能仍需设置。
类型混合:同一端口同时支持 HTTP 和 SOCKS5
常用端口
系统
程序
环境变量
官方文档仅当前窗口
$env:HTTP_PROXY = "http://127.0.0.1:7890" $env:HTTPS_PROXY = "http://127.0.0.1:7890" $env:NO_PROXY = "localhost,127.0.0.1"
永久
[Environment]::SetEnvironmentVariable("HTTP_PROXY", "http://127.0.0.1:7890", "User")
[Environment]::SetEnvironmentVariable("HTTPS_PROXY", "http://127.0.0.1:7890", "User")
[Environment]::SetEnvironmentVariable("NO_PROXY", "localhost,127.0.0.1", "User")- 永久设置的变量在新窗口中生效。
Git
官方文档永久
git config --global http.proxy http://127.0.0.1:7890
仅一个网站
git config --global http.https://github.com.proxy http://127.0.0.1:7890
仅一条命令
git -c http.proxy=http://127.0.0.1:7890 ls-remote https://github.com/git/git.git HEAD
- http.proxy 对 https:// 地址同样有效。
- git@… 地址走 SSH,见“Git(SSH)”。
npm
官方文档永久
npm config set proxy http://127.0.0.1:7890 npm config set https-proxy http://127.0.0.1:7890 npm config set noproxy "localhost,127.0.0.1"
仅一条命令
npm install --proxy http://127.0.0.1:7890 --https-proxy http://127.0.0.1:7890
pip
官方文档永久
py -m pip config set global.proxy http://127.0.0.1:7890
仅一条命令
py -m pip install --proxy http://127.0.0.1:7890 requests
curl
官方文档仅一条命令
curl.exe -x http://127.0.0.1:7890 -I https://example.com
写入文件$env:USERPROFILE\.curlrc
proxy = "http://127.0.0.1:7890" noproxy = "localhost,127.0.0.1"
- 在 Windows PowerShell 5.1 中 curl 是 Invoke-WebRequest 的别名,所以要写 curl.exe。
Docker:拉取镜像
官方文档Settings → Resources → ProxiesDocker Desktop
Web Server (HTTP): http://127.0.0.1:7890 Secure Web Server (HTTPS): http://127.0.0.1:7890 Bypass: localhost,127.0.0.1
- Docker Desktop:Settings → Resources → Proxies → “Manual proxy configuration”。它会忽略 daemon.json 中的代理。
全部在你的浏览器中计算:密钥和地址不会发送到任何地方。
在 Veli 中可以按网站和应用设置,不需要列表:哪些走 VPN,哪些直连。还有适用于 OpenWrt 路由器的软件包。免费,无需注册。
下载 Veli参考
各程序的代理写在哪里
以 127.0.0.1:7890 的 HTTP 代理和 Linux 为例(winget 为 Windows)。地址和系统可在上方更改。
Git
开启
git config --global http.proxy http://127.0.0.1:7890
取消
git config --global --unset http.proxy
SOCKS5:支持
npm
开启
npm config set proxy http://127.0.0.1:7890 npm config set https-proxy http://127.0.0.1:7890
取消
npm config delete proxy npm config delete https-proxy npm config delete noproxy
SOCKS5:支持
pnpm
开启
pnpm config set --global https-proxy http://127.0.0.1:7890 pnpm config set --global http-proxy http://127.0.0.1:7890
取消
pnpm config delete --global https-proxy pnpm config delete --global http-proxy pnpm config delete --global noproxy
SOCKS5:支持
Yarn
开启
yarn config set -H httpProxy http://127.0.0.1:7890 yarn config set -H httpsProxy http://127.0.0.1:7890
取消
yarn config unset -H httpProxy yarn config unset -H httpsProxy
SOCKS5:不支持
pip
开启
python3 -m pip config set global.proxy http://127.0.0.1:7890
取消
python3 -m pip config unset global.proxy
SOCKS5:需 PySocks
curl
开启
cat >> ~/.curlrc <<'EOF' # >>> proxy >>> proxy = "http://127.0.0.1:7890" # <<< proxy <<< EOF
取消
sed -i.bak '/# >>> proxy >>>/,/# <<< proxy <<</d' ~/.curlrc
SOCKS5:支持
wget
开启
cat >> ~/.wgetrc <<'EOF' # >>> proxy >>> use_proxy = on http_proxy = http://127.0.0.1:7890 https_proxy = http://127.0.0.1:7890 # <<< proxy <<< EOF
取消
sed -i.bak '/# >>> proxy >>>/,/# <<< proxy <<</d' ~/.wgetrc
SOCKS5:不支持
apt
开启
sudo tee /etc/apt/apt.conf.d/95proxy <<'EOF' Acquire::http::Proxy "http://127.0.0.1:7890/"; Acquire::https::Proxy "http://127.0.0.1:7890/"; EOF
取消
sudo rm /etc/apt/apt.conf.d/95proxy
SOCKS5:支持
dnf
开启
sudo sed -i.bak '/^proxy=/d' /etc/dnf/dnf.conf echo "proxy=http://127.0.0.1:7890" | sudo tee -a /etc/dnf/dnf.conf
取消
sudo sed -i.bak '/^proxy=/d' /etc/dnf/dnf.conf
SOCKS5:文档未说明
Docker:拉取镜像
开启
/etc/docker/daemon.json { "proxies": { "http-proxy": "http://127.0.0.1:7890", "https-proxy": "http://127.0.0.1:7890" } } sudo systemctl restart docker取消
/etc/docker/daemon.json - "proxies": { … } sudo systemctl restart dockerSOCKS5:不支持
Cargo
开启
~/.cargo/config.toml [http] proxy = "http://127.0.0.1:7890"
取消
~/.cargo/config.toml [http] # proxy = "http://127.0.0.1:7890"
SOCKS5:支持
Go
开启
export http_proxy="http://127.0.0.1:7890" export HTTP_PROXY="http://127.0.0.1:7890" export https_proxy="http://127.0.0.1:7890" export HTTPS_PROXY="http://127.0.0.1:7890"
取消
unset http_proxy HTTP_PROXY https_proxy HTTPS_PROXY all_proxy ALL_PROXY no_proxy NO_PROXY
SOCKS5:支持
Gradle
开启
cat >> ~/.gradle/gradle.properties <<'EOF' # >>> proxy >>> systemProp.http.proxyHost=127.0.0.1 systemProp.http.proxyPort=7890 systemProp.https.proxyHost=127.0.0.1 systemProp.https.proxyPort=7890 # <<< proxy <<< EOF
取消
sed -i.bak '/# >>> proxy >>>/,/# <<< proxy <<</d' ~/.gradle/gradle.properties
SOCKS5:支持
Maven
开启
~/.m2/settings.xml <settings> <proxies> <proxy> <id>local</id> <active>true</active> <protocol>http</protocol> <host>127.0.0.1</host> <port>7890</port> </proxy> </proxies> </settings>取消
~/.m2/settings.xml <settings> <proxies> <proxy> <id>local</id> <active>false</active> <protocol>http</protocol> <host>127.0.0.1</host> <port>7890</port> </proxy> </proxies> </settings>SOCKS5:不支持
VS Code
开启
~/.config/Code/User/settings.json "http.proxy": "http://127.0.0.1:7890"
取消
~/.config/Code/User/settings.json "http.proxy": ""
SOCKS5:支持
winget
开启
winget settings set DefaultProxy http://127.0.0.1:7890
取消
winget settings reset DefaultProxy winget settings --disable ProxyCommandLineOptions
SOCKS5:不支持
常见问题
git 如何设置代理,如何取消代理?
git config --global http.proxy http://127.0.0.1:7890 对 http 和 https 地址都有效:git 没有单独的 https.proxy。git config --global --unset http.proxy 取消代理。只对一个网站生效时用 http.https://github.com.proxy。git@github.com 这样的地址走 SSH,不看 http.proxy。
为什么 npm、pip 或 apt 不走浏览器的代理?
浏览器使用系统的代理设置,而终端程序大多不用:它们读取环境变量(HTTP_PROXY、HTTPS_PROXY)或自己的配置文件。sudo 还会丢弃这些变量,所以 apt 和 dnf 需要在自己的配置里写代理。
终端里用 HTTP 代理还是 SOCKS5?
代理支持 HTTP 就用 HTTP:本页所有程序都支持。git、curl、npm、cargo、Go 和 apt 支持 SOCKS5,pip 需要安装 PySocks,wget、Yarn、Maven、winget 和 Docker 没有 SOCKS5 设置。很多本地代理在同一端口同时支持两者,这时选“混合”。
WSL2 里的代理地址是什么?
使用 networkingMode=mirrored(Windows 11 22H2 及以上)时是 127.0.0.1,与 Windows 相同。默认的 NAT 模式下,127.0.0.1 是 WSL 自己:Windows 的地址是 ip route show 显示的网关,重启后会变,而且代理必须允许来自网络的连接。
为什么电脑上代理可用,Docker 容器里却不行?
在容器里 127.0.0.1 是容器自己。请使用 host.docker.internal(Linux 上加 --add-host=host.docker.internal:host-gateway),并且代理不能只监听 127.0.0.1。拉取镜像由守护进程完成,它有单独的设置。
如何一次取消所有代理?
打开“取消”标签:那里有每个程序的反向命令,“下载文件”会把它们写进一个脚本。代理关闭后 git 或 npm 连不上,常常就是因为忘了取消代理设置。