终端中设置代理

2022/1/23·3 views

教程为命令行下教程,如果是桌面系统,可以直接在设置中修改

linux

方法一:设置环境变量

临时

shell 复制代码
export all_proxy=http://ip:port // 全部
export http_proxy=http://ip:port
export https_proxy=http://ip:port // socks5h://ip:port
export no_proxy=localhost,127.0.0.1,::1 # 不通过代理

或者永久,加入到/etc/profile 中,初次需要重新加载 . /etc/profile 或者 source /etc/profile

如果需要取消设置,只需要unset即可,例如unset http_proxy

可以通过

shell 复制代码
env | grep proxy 

查看代理配置

curl通过-x设置代理

shell 复制代码
curl -x socks5h://127.0.0.1:1080 www.baidu.com

方法二:使用proxychains

shell 复制代码
apt install proxychains

如果提示 ERROR: ld.so: object 'libproxychains.so.3' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored. 就修改下proxychains 文件,/etc/proxychainsLD_PRELOAD修改为libproxychains.so.3 绝对路径,可以通过下面命令查找文件位置

shell 复制代码
find /usr/ -name libproxychains.so.3 -print

或者编译安装

shell 复制代码
git clone https://github.com/rofl0r/proxychains-ng
cd proxychains-ng
./configure
sudo make && make install
shell 复制代码
vim /etc/proxychains.conf # 找到[ProxyList]段,根据帮助加入配置

例如

plain 复制代码
http 127.0.0.1 10809

执行命令

shell 复制代码
proxychains curl google.com

windows

shell 复制代码
set all_proxy=http://ip:port // 全部
set http_proxy=http://127.0.0.1:7897
set https_proxy=http://127.0.0.1:7897
set no_proxy=localhost,::1,127.0.0.1

终端关闭后失效。

git

如果只在本仓库使用,需要去掉--global

shell 复制代码
# socks5协议,1080端口修改成自己的本地代理端口
git config --global http.proxy socks5://127.0.0.1:1080
git config --global https.proxy socks5://127.0.0.1:1080
# http协议,1081端口修改成自己的本地代理端口
git config --global http.proxy http://127.0.0.1:1081
git config --global https.proxy https://127.0.0.1:1081

移除代理配置

shell 复制代码
# reset 代理设置
git config --global --unset http.proxy
git config --global --unset https.proxy

windows最佳实践

创建一个bin目录,将这个目录添加到环境变量

proxy.bat

shell 复制代码
@echo off
chcp 65001 >nul
set all_proxy=http://127.0.0.1:7897

set "GIT_HTTP_PROXY=http://127.0.0.1:7897"
set "GIT_HTTPS_PROXY=http://127.0.0.1:7897"

git config --global http.proxy socks5://127.0.0.1:7897
git config --global https.proxy socks5://127.0.0.1:7897
git config --global http.proxy http://127.0.0.1:7897
git config --global https.proxy https://127.0.0.1:7897

echo 已成功设置代理为 http://127.0.0.1:7897
echo 若要验证,可执行 'set http_proxy' 命令

unsetproxy.bat

shell 复制代码
@echo off
chcp 65001 >nul

set all_proxy=

set "GIT_HTTP_PROXY="
set "GIT_HTTPS_PROXY="

git config --global --unset http.proxy
git config --global --unset https.proxy

echo 已成功移除代理 
echo 若要验证,可执行 'set http_proxy' 命令