Terminal & Git 代理设置

环境变量说明

关于代理环境变量的说明,可以参考 curl 官网的一段描述:

ENVIRONMENT VARIABLES

Curl reads and understands the following environment variables:
http_proxy, HTTPS_PROXY, FTP_PROXY

They should be set for protocol-specific proxies. General proxy should be set with
ALL_PROXY

A comma-separated list of host names that shouldn’t go through any proxy is set in (only an asterisk, ‘*’ matches all hosts)
NO_PROXY

If the host name matches one of these strings, or the host is within the domain of one of these strings, transactions with that node will not be proxied.

以上代理环境变量是一种约定俗成而非标准,Windows Linux macOS 以及常见的 Git curl wget 工具都支持这些环境变量。与 Linux macOS 不同,在 Windows 上,环境变量的名字大小写不敏感,因此 HTTP_PROXYhttp_proxyWindows 上的效果一样。但不同的工具对大小写的支持却有细微的差别,比如 这里 统计的关于 curl wget Ruby Python Go 对变量大小写的支持:

curl wget Ruby Python
http_proxy Yes Yes Yes Yes
HTTP_PROXY No No Yes warning Yes (if REQUEST_METHOD not in env)
https_proxy Yes Yes Yes Yes
HTTPS_PROXY Yes No Yes Yes
Case precedence lowercase lowercase only lowercase lowercase
Reference Source Source Source Source

从上表中可以看到,小写的 http_proxyhttps_proxy 是被各类工具都支持的,而大写的则不然。

本文整理了各类终端和 Git 中设置代理的命令,其中 proxyhost 是代理的域名或 IP 地址。注意,在终端窗口中使用 setexport 设置的代理仅对当前窗口生效。

Windows CMD 代理设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# HTTP 代理
set http_proxy=http://proxyhost:port
set https_proxy=http://proxyhost:port

# SOCKS 代理
set http_proxy=socks5://proxyhost:port
set https_proxy=socks5://proxyhost:port

# 查看当前环境变量或代理
set # 列出全部环境变量
set http # 列出 http 开头的环境变量

# 关闭当前 CMD 窗口即可取消代理,或通过把变量置空
set http_proxy=
set https_proxy=

查看 这里 关于更多 CMD 环境变量的设置和使用。

Windows PowerShell 代理设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# HTTP 代理
$env:http_proxy="http://proxyhost:port"
$env:https_proxy="http://proxyhost:port"

# SOCKS 代理
$env:http_proxy="socks5://proxyhost:port"
$env:https_proxy="socks5://proxyhost:port"
$env:all_proxy="socks5://proxyhost:port" # 针对特定协议可以设置 all_proxy

# 查看当前代理
ls env: # 列出全部环境变量
ls env:http_proxy # 查看 http_proxy

# 关闭当前窗口代理
del env:http_proxy
del env:https_proxy

查看 这里这里 关于更多关于 PowerShell 的用法。

Linux / macOS 终端代理设置

临时代理设置

1
2
3
4
5
6
7
8
9
10
11
12
13
# HTTP 代理
export http_proxy=http://proxyhost:port
export https_proxy=http://proxyhost:port

# socks5 代理
export http_proxy=socks5://proxyhost:port
export https_proxy=socks5://proxyhost:port
export all_proxy=socks5://proxyhost:port

# 取消代理
unset http_proxy
unset https_proxy
unset all_proxy

永久代理设置

将代理命令写入配置文件 ~/.profile~/.bashrc~/.zshrc 中:

1
2
3
4
5
6
7
8
9
10
11
# HTTP 代理
export http_proxy=http://proxyhost:port
export https_proxy=http://proxyhost:port

# socks5 代理
export http_proxy=socks5://proxyhost:port
export https_proxy=socks5://proxyhost:port
export all_proxy=socks5://proxyhost:port

# 指定 IP 或域名不使用代理
export NO_PROXY=127.0.0.1;no-proxy-domain.com

Git 设置代理

代理格式 [protocol://][user[:password]@]proxyhost[:port]
参考 https://git-scm.com/docs/git-config

1
2
3
4
5
6
7
8
9
10
11
# HTTP 代理
git config --global http.proxy http://proxyhost:port
git config --global https.proxy http://proxyhost:port

# socks5 代理
git config --global http.proxy socks5://proxyhost:port
git config --global https.proxy socks5://proxyhost:port

# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy

更详细的 Git 代理用法,可以参考 这里