黑海洋 黑海洋 WIKI

Linux 设置全局代理

安全 · 约 3 分钟 · 黑海洋 · 5.6万
站内热度 站内前 11% 5.6万 次浏览

今天有人问在国内的linux上设置代理,刨坟给给大家分享一下。

linux设置代理确实是比较麻烦,各种环境变量,各个软件的代理设置多多少少又有点区别,所以设置全局代理就比较方便点。

使用iptables+redoskcs。

1、安装 redsocks:

apt install redsocks

2、配置 /etc/redsocks.conf 文件

ip和port就是你自己代理的ip和端口。

base {
    log_debug = on;
    log_info = on;
    log = "file:/var/log/redsocks.log";
    daemon = on;
    redirector = iptables;
}

redsocks {
    local_ip = 127.0.0.1;
    local_port = 12345;
    ip = your_proxy_address;
    port = your_proxy_port;
    type = socks5;  # 可以是 http-relay, socks4, socks5 等
}

3、配置iptables 转发规则

iptables -t nat -N REDSOCKS

iptables -t nat -A REDSOCKS -d 0.0.0.0/8 -j RETURN
iptables -t nat -A REDSOCKS -d 10.0.0.0/8 -j RETURN
iptables -t nat -A REDSOCKS -d 127.0.0.0/8 -j RETURN
iptables -t nat -A REDSOCKS -d 169.254.0.0/16 -j RETURN
iptables -t nat -A REDSOCKS -d 172.16.0.0/12 -j RETURN
iptables -t nat -A REDSOCKS -d 192.168.0.0/16 -j RETURN
iptables -t nat -A REDSOCKS -d 224.0.0.0/4 -j RETURN
iptables -t nat -A REDSOCKS -d 240.0.0.0/4 -j RETURN

iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-ports 12345 

iptables -t nat -A OUTPUT -p tcp --dport 443 -j REDSOCKS
iptables -t nat -A OUTPUT -p tcp --dport 80 -j REDSOCKS

4、如果要取消

iptables -F
iptables -X
iptables -Z
iptables -t nat -F
iptables -t nat -X
iptables -t nat -Z


相关文献:http://tiven.wang/articles/how-to-get-network-in-cn/

相关文档

评论与补充0

有问题或想补充的,写在下面 · 选中正文文字再点「报告错误」会自动引用

还没有评论。有疑问、补充,或者发现错字,都可以写在下面。

就事论事,别发广告 · 提交后需审核