Linux: 代理

系统环境变量

  curl wget Ruby Python Go
http_proxy (lowercase) Yes Yes Yes Yes Yes
HTTP_PROXY (Uppercase) No No Yes (warning) Yes (if REQUEST_METHOD not in env) Yes
https_proxy (lowercase) Yes Yes Yes Yes Yes
HTTPS_PROXY (Uppercase) Yes No Yes Yes Yes
no_proxy (lowercase) Yes Yes Yes Yes Yes
NO_PROXY (Uppercase) Yes No Yes Yes Yes
Case precedence lowercase lowercase only lowercase lowercase Uppercase

参考:https://about.gitlab.com/blog/2021/01/27/we-need-to-talk-no-proxy/

windows

PROXY
HTTP_PROXY
HTTPS_PROXY
ALL_PROXY
NO_PROXY
proxy
http_proxy
https_proxy
all_proxy
no_proxy

linux

大小写变量都可以

# 开启系统代理
function proxy_on() {
  PROXY="http://127.0.0.1:1080"
  NO_PROXY="localhost,127.0.0.1,.company.com"
  export PROXY=$PROXY
  export HTTP_PROXY=$PROXY
  export HTTPS_PROXY=$PROXY
  export ALL_PROXY=$PROXY
  export NO_PROXY=$NO_PROXY
  export proxy=$PROXY
  export http_proxy=$PROXY
  export https_proxy=$PROXY
  export all_proxy=$PROXY
  export no_proxy=$NO_PROXY
  echo -e "\033[32m[√] 已开启代理\033[0m"
}

function proxy_off(){
  unset HTTP_PROXY
  unset HTTPS_PROXY
  unset ALL_PROXY
  unset NO_PROXY
  unset http_proxy
  unset https_proxy
  unset all_proxy
  unset no_proxy
  echo -e "\033[31m[×] 已关闭代理\033[0m"
}
#proxy_on #开启代理
#proxy_off #关闭代理
#如果某些地址不想要使用代理,可以配置在no_proxy变量上,用逗号分隔。
export no_proxy='a.test.com,127.0.0.1,2.2.2.2'

#全都走代理
export all_proxy='http://127.0.0.1:1080'

#有时候希望yum也使用代理,那么在/etc/yum.conf中加上:
proxy=http://127.0.0.1:1080

命令行启动服务,加上代理

#格式
http_proxy=your-proxy-server-ip:port Command

#emacs
http_proxy=http://127.0.0.1:1080 emacs -nw
https_proxy=http://127.0.0.1:1080 curl https://gitlab.com

安装代理服务

ssh代理

范例

#ssh -p 65522 -i dev-server.pem -D 10800 [email protected] -Ng

#~/.ssh/config
Host dhost
  Hostname 3.x.x.71
  Port 65522
  User root
  IdentityFile /d/dev-server.pem
  DynamicForward 10800

ssh dhost -Ng #-f 后台启动

#浏览器设置代理socket5 127.0.0.1:10800

范例

ssh -L 1080:localhost:9999 JumpHost -t ssh -D 9999 HostB
#这条命令会在登录JumpHost时,建立本机1080端口到JumpHost 9999端口的转发,
#同时在JumpHost上执行ssh登录HostB,同时监听9999端口动态转发到HostB。
#于是,所有到本机1080端口的连接,都被代理到了远程的HostB上去

v2raya代理

机场 + v2raya + GFWList 的全局透明代理的效果最好, 透明代理配置好以后, 就不再需要 SwitchyOmega 和 proxy-ns 其他工具, 省心方便, 下面是具体的攻略:

购买机场

我主要用这个机场, 这个机场的好处是全平台支持, 非 Linux 平台都有专用客户端, 开箱即用很方便。

安装 v2raya

ArchLinux 用yay -S v2raya 命令一键安装 v2raya, 然后在浏览器打开 http://127.0.0.1:2017 进行如下配置:

  • 订阅机场: 拷贝机场订阅 URL, 点击导入按钮导入
  • 选择服务器: 选择 S.JISUSUB.CC 标签, 选择一个合适的服务器, 然后选择左上角启动按钮
  • 更新 GFWList: 点击页面右上角设置按钮, 在设置对话框右上角点击更新按钮更新 GFWLIST, 然后再按照下面的步骤对设置页面进行配置
  • 透明代理/系统代理: 启用: 分流规则与规则端口所选模式一致
  • 透明代理/系统代理实现方式: redirect
  • 规则端口的分流模式: RoutingA, 右边的规则配置见下面
  • 防止 DNS 污染: DNS-over-HTTPS
  • 特殊模式: supervisor
  • TCPFastOpen: 关闭
  • 多路复用: 关闭
  • 自动更新 GFWList: 每个 1 小时自动更新
  • 自动更新订阅: 每个 1 小时自动更新
  • 解析订阅地址/更新时优先使用: 不进行分流

规则配置

default: proxy

# 直连
domain(domain:qq.com, domain:163.com)->direct
domain(geosite:cn)->direct
ip(geoip:private)->direct
ip(geoip:cn)->direct
  • 默认走代理: default: proxy
  • 微信网易云音乐走直连: domain(domain:qq.com, domain:163.com)->direct
  • 大陆域名走直连: domain(geosite:cn)->direct
  • 内网服务器走直连: ip(geoip:private)->direct
  • 大陆 IP 走直连: ip(geoip:cn)->direct

这样设置不影响微信客户端启动, 比 GFWList 方便, 因为防火墙列表不一定全面, 很多新认证的网站范围上不了。

故障解决

failed to start v2ray-core: LocateServerRaw: ID or Sub exceed range

这个错误,删除 “/etc/v2raya” 目录下所有文件, 然后重启 v2raya sudo systemctl restart v2raya 后, 重新导入机场地址即可。

SS5 代理

centos7搭建SS5服务器

安装ss5

yum -y install gcc automake make pam-devel openldap-devel cyrus-sasl-devel openssl-devel

#解压源码包

wget https://jaist.dl.sourceforge.net/project/ss5/ss5/3.8.9-8/ss5-3.8.9-8.tar.gz

tar xvf ss5-3.8.9-8.tar.gz

cd ss5-3.8.9

./configure && make && make install

配置

修改身份验证方法

:CUSTOM_ID: 修改身份验证方法

/etc/opt/ss5/ss5.conf,取消

auth 0.0.0.0/0 - -

permit - 0.0.0.0/0 - 0.0.0.0/0 - - - - -

这两行的注释,这时是不用验证的,如果要验证,则修改为

auth 0.0.0.0/0 - u

permit u 0.0.0.0/0 - 0.0.0.0/0 - - - - -

添加用户名和密码`=/etc/opt/ss5/ss5.passwd=,每行一个用户一个密码。

username1 pwd1

username2 pwd2

修改代理端口 /etc/sysconfig/ss5

(如果没有设置,默认为1080)

SS5_OPTS=" -u root -b 0.0.0.0:1080"

向bash文件/etc/rc.d/init.d/ss5添加可执行权限

chmod +x /etc/init.d/ss5

日常使用

#在chkconfig工具服务列表中增加此服务,设置开机自启(可选)
chkconfig --add ss5

chkconfig --level 345 ss5 on

#手动启动
service ss5 start

#查看监听端口1080
netstat -an | grep 1080

#默认端口1080,开防火墙

/usr/sbin/iptables -I INPUT -p tcp --dport 1080 -j ACCEPT


#每次重启计算机后或者出现以下错误时:
Can’t create pid file /var/run/ss5/ss5.pid

Can’t unlink pid file /var/run/ss5/ss5.pid

需要先创建 /var/run/ss5 目录,然后再启动 ss5

浏览器使用

  • firefox安装插件foxproxy
  • chrome谷歌浏览器安装插件SwitchyOmega

规则文件:

https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt

搬瓦工

俄罗斯主机商justhost.ru(戳这里:俄罗斯VPS:justhost

加利福尼亚的比较快

yum install epel-release
yum update
yum install git python-setuptools libsodium supervisor
easy_install pip
pip install shadowsocks

[root@node01 ~]# cat /etc/shadowsocks.json
{
    "server":"0.0.0.0",     //监听IP
    "server_port":4434,   //开放端口
    "local_address": "127.0.0.1",  
    "local_port":1080,
    "password":"Hd)$d&%c,?t@E",   //客户验证密码
    "timeout":300,   //超时时间
    "method":"aes-256-cfb",   //加密格式
    "fast_open": false,
    "works": 2   //进程数
}


[root@virtualprotocolnetwork ~]#ssserver -c /etc/shadowsocks.json              //检查并重读配置文件
[root@virtualprotocolnetwork ~]#ssserver -c /etc/shadowsocks.json -d start     //后台启动
[root@virtualprotocolnetwork ~]#netstat -anput | grep 8388                     //检查端口号

客户端支持多种操作系统:Windows,MACos,Linux,IOS等

1)下载一个

Shadowsocks软件客户端

输入:云主机IP、端口、加密规则、密码就可以使用了

2)然后要在阿里云打开一个安全组规则

入方向要开放所有地址段的所有端口

出方向要开放所有地址段的所有端口

然后就大功告成了

# 快速停止启动
[root@vpn-01 ~]# ssserver -d stop
stopped
[root@vpn-01 ~]# ssserver --user nobody -c /etc/shadowsocks.json -d start

官方命令行启动

server端

/usr/bin/python /usr/bin/ssserver -s ::0 -p 443 -k NWU2ZTQ0OT -m aes-256-cfb --user nobody --workers 2 -d start
/usr/bin/python /usr/bin/ssserver -s ::0 -p 443 -k NWU2ZTQ0OT -m aes-256-cfb --user nobody --workers 2 -d start
/usr/bin/python /usr/bin/ssserver -s ::0 -p 443 -k NWU2ZTQ0OT -m aes-256-cfb --user nobody --workers 2 -d start

客户端 ssr

使用代理

包管理工具yum/apt使用代理

yum

临时设置代理

export http_proxy=socks5://127.0.0.1:1080
yum
vim /etc/yum.conf
proxy=socks5://127.0.0.1:1080

apt

临时设置代理, 增加一个参数即可

-o Acquire::http::proxy="http://127.0.0.1:1080/"
-o Acquire::https::Proxy "http://127.0.0.1:6666/";
-o Acquire::socks5::proxy "socks://127.0.0.1:1080/"

sudo apt-get -o Acquire::socks5::proxy="socks5h://127.0.0.1:1080/" update
 vim /etc/apt/apt.conf 或

 #多个代理:
 vi /etc/apt/apt.conf.d/proxy.conf
 Acquire {
  HTTP::proxy "http://127.0.0.1:6666";
  HTTPS::proxy "http://127.0.0.1:1080";
}

容器服务使用代理

containerd

mkdir -p /etc/systemd/system/containerd.service.d
vim /etc/systemd/system/containerd.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://proxy-domain:port"
Environment="HTTPS_PROXY=http://proxy-domain:port"
Environment="NO_PROXY=localhost"

systemctl daemon-reload
systemctl restart containerd

docker

#/usr/lib/systemd/system/docker.service
Environment="HTTPS_PROXY=http://IP:8118"
Environment="NO_PROXY=127.0.0.0/8,172.20.0.0/16"

Git 使用代理

1.准备工作

  • 购买 VPS;
  • 在服务器 VPS 上安装 v2ray server (请自行 Google);
  • 在本机安装 v2ray client, 利用 v2ray client 开启 socks5 代理 (请自行 Google)。

2.配置 Git HTTP/HTTPS 代理

在 ~/.gitconfig 文件中加入以下配置:

[http]
        proxy = socks5://127.0.0.1:1080

注意, 上明配置等同于命令 git config –global http.proxy 'socks5://127.0.0.1:1080'

Git 不认 https.proxy, 设置 http.proxy 就可以支持 https 了。

3.配置 Git SSH 代理

在 ~/.ssh/config 文件中加入以下配置:

Host github.com
HostName github.com
User git
Port 22
ProxyCommand /usr/bin/ncat --proxy 127.0.0.1:1080 --proxy-type socks5 %h %p

命令行

https_proxy=socks5://127.0.0.1:1080 git clone xxxx

4.把 github.com 加入 v2ray 代理路由

上面两个配置只是让 HTTPS/SSH 访问走本地 Socks5 代理, 但是并不能保证一定可以连接上 Github。

需要把 github.com 加入 v2ray 的路由中, 具体请看 v2ray 设置路由

#查看所有设置
git config -l
#查看代理
git config --global --get http.proxy

针对 github.com 的单独配置

#socks5协议,1080端口修改成自己的本地代理端口
git config --global http.https://github.com.proxy socks5://127.0.0.1:1090
git config --global https.https://github.com.proxy socks5://127.0.0.1:1090

#http协议,1081端口修改成自己的本地代理端口
git config --global http.https://github.com.proxy https://127.0.0.1:41091
git config --global https.https://github.com.proxy https://127.0.0.1:41091

取消代理

#reset 代理设置
git config --global --unset http.proxy
git config --global --unset https.proxy

Curl 使用代理

参考:https://curl.se/docs/tutorial.html

方法1:命令行

curl --socks5 xxx.xxx.xxx.xxx:xxxx http://example.com/
或者
curl -x "http_proxy://xxx.xxx.xxx.xxx:xxxx" http://example.com/
或者
env https_proxy=http://non.existent/ no_proxy=.gitlab.com curl https://gitlab.com

方法2:配置文件

#windows
%APPDATA%\_curlrc
%USERPROFILE%\Application Data\_curlrc
#linux
~/.curlrc

#添加内容
https_proxy=http://127.0.0.1:1080
http_proxy=http://127.0.0.1:1080

Java、maven使用代理

JAVA

参考 Java 文档中的网络和代理

#临时
java -Dhttps.proxyHost=10.15.20.25 -Dhttps.proxyPort=1234 -Dhttp.nonProxyHosts=localhost|api.example.com MyApplication

#使用环境变量(以及每个变量的大写版本)
export http_proxy=http://10.15.20.25:1234
export https_proxy=http://10.15.20.25:5678
export no_proxy=localhost,api.example.com

maven

maven clean install -DproxySet=true -DproxyHost=<private-ip-of-proxy-server> -DproxyPort=3128
emacs

Emacs

org-mode

Orgmode

Donations

打赏

Copyright

© 2025 Jasper Hsu

Creative Commons

Creative Commons

Attribute

Attribute

Noncommercial

Noncommercial

Share Alike

Share Alike