Posted on 

linux终端设置代理

Linux 让终端走代理的几种方法 - 知乎

方法一:(推荐使用)

为什么说这个方法推荐使用呢?因为他只作用于当前终端中,不会影响环境,而且命令比较简单

在终端中直接运行:

1
2
3
4
export http_proxy=http://proxyAddress:port

export ALL_PROXY=socks5://127.0.0.1:1080

如果你是SSR,并且走的http的代理端口是12333,想执行wget或者curl来下载国外的东西,可以使用如下命令:

1
export http_proxy=http://127.0.0.1:12333

如果是https那么就经过如下命令:

1
export https_proxy=http://127.0.0.1:12333

方法二 :

这个办法的好处是把代理服务器永久保存了,下次就可以直接用了

把代理服务器地址写入shell配置文件.bashrc或者.zshrc 直接在.bashrc或者.zshrc添加下面内容

1
2
export http_proxy="http://localhost:port"
export https_proxy="http://localhost:port"

或者走socket5协议(ss,ssr)的话,代理端口是1080

1
2
export http_proxy="socks5://127.0.0.1:1080"
export https_proxy="socks5://127.0.0.1:1080"

或者干脆直接设置ALL_PROXY

1
export ALL_PROXY=socks5://127.0.0.1:1080

最后在执行如下命令应用设置

1
source ~/.bashrc

或者通过设置alias简写来简化操作,每次要用的时候输入setproxy,不用了就unsetproxy。

1
alias setproxy="export ALL_PROXY=socks5://127.0.0.1:1080" alias unsetproxy="unset ALL_PROXY"

这样 curl wget 是都走代理了,包括 git npm yarn .

全局代理设置命令比较烦,可以集成到一个 shell 环境变量里,我这里用的 shell 是 zsh,若以修改 zsh 配置文件:

1
subl ~/.zshrc
1
2
3
# proxy list
alias proxy='export all_proxy=socks5://127.0.0.1:1080'
alias unproxy='unset all_proxy'

参考

给终端设置代理超简单的,直接一句命令搞定:

1
export ALL_PROXY="socks5://127.0.0.1080"

这样 curl wget 是都走代理了,但是 git npm yarn 这些不知为何不跟随终端的环境变量设置。一种解决方案是分别设置:

1
2
3
4
5
6
git config --global http.proxy "socks5://127.0.0.1080"
git config --global https.proxy "socks5://127.0.0.1080"
npm config set proxy "http://127.0.0.1090"
npm config set https-proxy "http://127.0.0.1090"
yarn config set proxy "http://127.0.0.1090"
yarn config set https-proxy "http://127.0.0.1090"

但是苏卡卡发现了一个奇怪的解决方法,就是除了设置 ALL_RPOXY 以外再添加一个 all_proxy

1
export all_proxy="socks5://127.0.0.1080"

这样不仅 git npm yarn 这些读取环境变量不支持大小写的辣鸡,其它绝大部分终端应用程序不走代理的问题就都解决了~

设置代理#

1
2
3
4
5
export http_proxy=http://127.0.0.1:49776
export https_proxy=$http_proxy
export ftp_proxy=$http_proxy
export rsync_proxy=$http_proxy
export no_proxy="localhost,127.0.0.1"

查看当前代理#

1
2
echo $http_proxy
echo $https_proxy

取消代理#

1
2
3
4
unset http_proxy
unset https_proxy
unset ftp_proxy
unset rsync_proxy