摘自:设置mac终端走代理
使用包管理器的命令下载github的包时,会出现卡住的情况,大多是因为GFW的问题,这时候就需要给命令行设置代理
MacOS
执行以下两条命令。在终端关闭之前有效。端口在ssr里看
export http_proxy="http://localhost:port"
export https_proxy="http://localhost:port"
还有一种更有意思的方式
在~/.zshrc中添加以下内容
# 为终端设置代理
alias proxy='export all_proxy=socks5://127.0.0.1:port'
alias unproxy='unset all_proxy'
# 为终端设置http代理
alias httpproxy='export http_proxy=http://localhost:1087 && export https_proxy="http://localhost:1087"'
alias unhttpproxy='unset http_proxy && unset https_proxy'
这样在终端中使用proxy、httpproxy命令就可以打开代理,使用unproxy、unhttpproxy命令就可以关闭代理
然后在终端运行以后,想通过ping google.com来测试现在终端是否可以连接外网,结果发现不论怎么折腾,死活就是ping不通。
原来,系统只提供了http协议和socks协议的代理,http在应用层,socks在会话层。但是ping命令采用icmp协议来检测网址是否可达,icmp在网络层。上层无法代理下层,所以ping也就无法被代理
但是可以通过curl来测试:curl https://www.google.com、curl https://www.youtube.com等来检测是否成功代理,curl是应用层协议库,所以可以使用代理
Windows
cmd,powershell,git-bash设置代理的方式是不一样的 (因为不同shell"设置环境变量"的语法不同)
powershell
$env:https_proxy = "127.0.0.1:port"
$env:http_proxy = "127.0.0.1:port"
git-bash
export http_proxy='127.0.0.1:port'
export https_proxy='127.0.0.1:port'
cmd
set http_proxy='127.0.0.1:port'
set https_proxy='127.0.0.1:port'
本文指导如何在Mac和Windows的终端中设置代理以解决下载github包时的GFW限制,介绍了http和socks代理设置方法,以及为何ping命令无法通过代理。提供curl作为替代测试工具,并区分了不同shell设置代理的差异。

1013

被折叠的 条评论
为什么被折叠?



