macOS & Linux
通过设置 http_proxy、https_proxy,可以让终端走指定的代理。 配置脚本如下,在终端直接执行,只会临时生效:
# 设置代理
export http_proxy=http://127.0.0.1:8899
export https_proxy=$http_proxy
# 或是一行命令
export https_proxy=http://127.0.0.1:8899 http_proxy=http://127.0.0.1:8899
# 还原代理
unset http_proxy https_proxy
8899 是 http 代理的端口,请根据你的实际情况修改。
Windows
以下代理生效情况均使用 curl -I http://www.google.com 进行验证。
Git Bash
设置方法同 “macOS & Linux”
# 设置代理
export http_proxy=http://127.0.0.1:8899
export https_proxy=$http_proxy
# -I/–head 只显示请求头信息
$ curl -I http://www.google.com
...
# 还原代理
unset http_proxy https_proxy
$ curl -I http://www.google.com
curl: (28) Failed to connect to www.google.com port 80: Timed out
PowerShell
# PowerShell 中的 curl 语法:
curl.exe [options] <url>
# 你可以运行以下命令以获取有关 curl 命令及其选项(例如 -a、-C 等)的更多信息…
curl.exe --help
# 设置代理
$env:http_proxy="http://127.0.0.1:8899"
$env:https_proxy="http://127.0.0.1:8899"
curl.exe -I http://www.google.com
HTTP/1.1 200 OK
...
# 还原代理
$env:http_proxy=""
$env:https_proxy=""
curl.exe -I http://www.google.com
curl: (7) Failed to connect to www.google.com port 80: Timed out
cmd
注意:在 Windows 下使用全局代理方式不会对 cmd 生效
# 设置代理
set http_proxy=http://127.0.0.1:8899
set https_proxy=http://127.0.0.1:8899
# 还原代理
set http_proxy=
set https_proxy=
其他代理设置
git 代理
# 查看全局配置
git config --list
# 设置
git config --global http.proxy 'http://127.0.0.1:8899'
git config --global https.proxy 'https://127.0.0.1:8899'
# 恢复
git config --global --unset http.proxy
git config --global --unset https.proxy
npm
# 查看全局配置
npm config list -g
# 设置
npm config set proxy http://127.0.0.1:8899 -g
npm config set https-proxy http://127.0.0.1:8899 -g
# 恢复
npm config delete proxy
npm config delete https-proxy