Terminal & Git 代理设置
环境变量说明
关于代理环境变量的说明,可以参考 curl
官网的一段描述:
ENVIRONMENT VARIABLES
Curl reads and understands the following environment variables:
http_proxy, HTTPS_PROXY, FTP_PROXYThey should be set for protocol-specific proxies. General proxy should be set with
ALL_PROXYA comma-separated list of host names that shouldn’t go through any proxy is set in (only an asterisk, ‘*’ matches all hosts)
NO_PROXYIf 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_PROXY
和 http_proxy
在 Windows
上的效果一样。但不同的工具对大小写的支持却有细微的差别,比如 这里 统计的关于 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_proxy
和 https_proxy
是被各类工具都支持的,而大写的则不然。
本文整理了各类终端和 Git
中设置代理的命令,其中 proxyhost
是代理的域名或 IP
地址。注意,在终端窗口中使用 set
或 export
设置的代理仅对当前窗口生效。
Windows CMD 代理设置
1 | # HTTP 代理 |
查看 这里 关于更多 CMD 环境变量的设置和使用。
Windows PowerShell 代理设置
1 | # HTTP 代理 |
查看 这里、这里 关于更多关于 PowerShell 的用法。
Linux / macOS 终端代理设置
临时代理设置
1 | # HTTP 代理 |
永久代理设置
将代理命令写入配置文件 ~/.profile
或 ~/.bashrc
或 ~/.zshrc
中:
1 | # HTTP 代理 |
Git 设置代理
代理格式 [protocol://][user[:password]@]proxyhost[:port]
参考 https://git-scm.com/docs/git-config
1 | # HTTP 代理 |
更详细的 Git 代理用法,可以参考 这里。