Linux下配置代理

1、下载和生成配置文件

这里使用v2ray来做为系统或apt代理

先在https://github.com/v2ray/v2ray-core/releases这里下载v2ray-linux-64.zip

从其他地方的导出一个节点的配置做为客户端配置使用,导出以后是一个json文件

2、测试是否正常

# ./v2ray -test -config config.json 
V2Ray 4.28.2 (V2Fly, a community-driven edition of V2Ray.) Custom (go1.15.2 linux/amd64)
A unified platform for anti-censorship.
2024/08/21 01:40:28 [Info] v2ray.com/core/common/platform/ctlcmd: <v2ctl message> 
v2ctl> Read config:  config.json
Configuration OK.

3、添加系统启动项使用

v2ray-linux-64.zip这里边带的有一个v2ray.service,拷贝到/usr/lib/systemd/system/下修改为正确的路径

v2ray.service中的启动用户改成root

systemctl daemon-reload

systemctl start v2ray

systemctl enable v2ray

4、设置其它应用使用代理

netstat -tunlp | grep v2ray 查看下端口,我这里是两个10808和10809,10808是socks的,10809是http的端口

这里设置apt使用代理来安装docker

在下边目录下新建一个proxy.conf
cat /etc/apt/apt.conf.d/proxy.conf
Acquire::http::Proxy "http://127.0.0.1:10809/";
Acquire::https::Proxy "http://127.0.0.1:10809/";

再用apt update就可以获取到docker源相关的信息了

5、还可以设置curl、wget使用代理

1、修改/etc/profile
# 设置http代理
export http_proxy=socks5://127.0.0.1:10808
# 设置https代理
export https_proxy=socks5://127.0.0.1:10808
# 设置ftp代理
export ftp_proxy=socks5://127.0.0.1:10808​
# 17.16.x.x为云服务器的内网IP 配置为no_proxy代表内网传输不走代理
export no_proxy="172.16.x.x"

执行source /etc/profile 使代理生效

2、仅对当前终端生效:
添加代理
export http_proxy=socks5://127.0.0.1:10808
export https_proxy=socks5://127.0.0.1:10808
export ftp_proxy=socks5://127.0.0.1:10808
export no_proxy="172.16.x.x" 
取消代理
unset http_proxy
unset https_proxy
unset ftp_proxy
unset no_proxy

3、针对间用户生效
编辑文件 vim ~/.bashrc 添加以下内容
# set proxy
function setproxy() {
    export http_proxy=socks5://127.0.0.1:10808
    export https_proxy=socks5://127.0.0.1:10808
    export ftp_proxy=socks5://127.0.0.1:10808
    export no_proxy="172.16.x.x"
}
​
# unset proxy
function unsetproxy() {
    unset http_proxy https_proxy ftp_proxy no_proxy
}

执行 source ~/.bashrc ,使得配置立即生效
在终端执行 setproxy 使代理生效
在终端执行 unsetproxy 取消代理