在终端会话中设置代理

在终端会话中设置代理

1、前提

对于 linux 或者 macos 系统而言,如果在命令行环境变量中存在 http_proxy、https_proxy。那么某些命令就会自动使用这两个代理设置。

 

2、手动设置环境变量

在终端窗口中输入:

export http_proxy=http://127.0.0.1:7890
export https_proxy=$http_proxy

注意,手工设置的环境变量只会影响当前会话,亦即只在当前终端窗口生效。

另外,建议一律使用小写的 http_proxy 而不是大写 HTTP_PROXY。

因为有位网友做过测试,测试大小写  http_proxy 环境变量对不同工具是否起作用,如下图所示(我没有验证过)

 

3、通过 zsh 的配置文件添加 proxy 环境变量

编辑用户的 ~/.zprofile 文件,增加如下内容:

# 添加 proxysetup 命令(可以使用 "echo $http_proxy" 命令来检查)
# 在当前会话中设置 http_proxy, https_proxy, all_proxy 环境变量
alias proxysetup='export http_proxy=http://127.0.0.1:7890 https_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890'

# 添加 proxyunset 命令
# 删除当前会话中的 http_proxy, https_proxy, all_proxy 环境变量
alias proxyunset='unset http_proxy https_proxy all_proxy'

# 自动设置代理
proxysetup

 

4、测试

Leave a Reply

Your email address will not be published. Required fields are marked *

TOC