Skip to main content

Command Palette

Search for a command to run...

使用 redsocks 和 iptables 配置 Linux 透明代理

Updated
View as Markdown
使用 redsocks 和 iptables 配置 Linux 透明代理

冥冥之中,自有天意。

1 配置 redsocks

1 安装 redsocks:apt install redsocks

2 编辑配置文件:vim /etc/redsocks.conf

主要编辑这些部分:

local_ip = 0.0.0.0;    // 只截获本机流量可使用 127.0.0.1
local_port = 12345;

// `ip' and `port' are IP and tcp-port of proxy-server
// You can also use hostname instead of IP, only one (random)
// address of multihomed host will be used.
ip = <socks5服务器地址>;
port = <socks5服务器端口>;

// known types: socks4, socks5, http-connect, http-relay
type = socks5;

// 代理服务器不需要认证的话注释掉
// login = "<socks5服务器账号>";
// password = "<socks5服务器密码>";

3 重启生效:systemctl restart redsocks

查看监听情况: netstat -anltp | grep 12345lsof -i:12345

2 设置 iptables 转发(全局)

一定注意需要绕过 redsocks 到代理服务器的流量,否则就无限循环了。

# 创建新链
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

# 将流量转交到 redsocks 对应的端口
iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-port=12345

# 将流量截获至 RESOCKS 链
iptables -t nat -A OUTPUT -p tcp -j REDSOCKS  # 本机流量
iptables -t nat -A PREROUTING -j REDSOCKS   # 路由流量

3 基于 CloudFlare 搭建 DNS 服务器(DoH)

DoH 等背景介绍略,感兴趣 Google/百度。

CloudFlare DoH 相关介绍:https://developers.cloudflare.com/1.1.1.1/encryption/dns-over-https/dns-over-https-client/

1 下载 CloudFlare 客户端:https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/installation/

2 下载 deb 包 apt 直接安装即可。

image.png

3 检查一下是否安装成功:cloudflared --version

image.png

4 启动相关服务:cloudflared proxy-dns --address 0.0.0.0 --port 15353

可以使用 nohub & 或者用 screen 托管在后台。

image.png

4 设置 DNS 转发(DNAT)

iptables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 15353  # 路由流量
iptables -t nat -A OUTPUT -p udp --dport 53 -j REDIRECT --to-ports 15353   # 本地流量

dig 测试一下行不行:dig +short @127.0.0.1 [www.baidu.com](http://www.baidu.com/)

image.png

注意:如果 53 端口被 systemd-resolved 占用,需先关闭这些程序。 https://devnote.pro/posts/10000050301151

5 更改网络 DNS 设置

6 清理

iptables -t nat -F OUTPUT
iptables -t nat -F REDSOCKS
iptables -t nat -X REDSOCKS

More from this blog

Grafana 配置 Prometheus Proxmox VE Exporter

本文不做基础知识的扫盲。 我一直想将 PVE 上全量的虚拟机纳入 Grafana 中展示,然而又不想一台台的配置 node_exporter。偶然的机会听说了 Prometheus Proxmox VE Exporter 插件,索性配置一下这个插件,将 PVE 的数据接入 Grafana。 整体架构如下图: 1. 配置 PVE 用户 网上的很多教程都是直接拿 root@pam 账户来进行操作,但特权账号用作监控其实是安全风险蛮高的一件事情。这里我们单独建立一个审计(PVEAuditor)账户来...

May 23, 2025
Grafana 配置 Prometheus Proxmox VE Exporter

刘梦熊:问题在经济,根子在政治

导语:在美国和西方发达国家,经济繁荣、衰退、复苏、繁荣周而复始,有其周期性和相应的经济对策。但中国是政治挂帅,政治凌驾经济,而且习惯只算政治账,不算经济账。当前中国经济盛极而衰,正是由于中共只搞局部经济体制改革,迟迟不启动政治体制改革带来的恶果。 当前中国经济面临私企倒闭潮、外企撤离潮、投资大幅萎缩潮、出生人口下降潮、消费严重不足潮、债务连环爆煲潮,员工大规模失业潮,政府财税失血潮……投资、出口、消费三大经济引擎全部动力不足乃至死火。中国国家统计局日前公布的7月份经济数据,不是疲弱下挫就是远...

Aug 29, 2023
刘梦熊:问题在经济,根子在政治

WireGuard 配置 Global IPv6 (公网下放)

众所周知,IPv6 和 IPv4 不同,IPv6 的地址池更加丰富,也摒弃了 IPv4 中目前常见的 NAT 做法(虽然有 NAT6 的存在,但这终究是一种不受推荐的选择)。对于如果 WireGuard 中的一个 Peer 存在 IPv6 前缀(即 IP 地址不是/128),该设备就可以作为 Server,将前缀下的一部分地址分配给 VPN 中的其他设备的。 本文以 WireGuard 为例进行展示,IP地址均为静态指定,不涉及 DHCPv6。 首先给出网络拓扑图: 1. 更改 Linux 配...

Aug 14, 2023
WireGuard 配置 Global IPv6 (公网下放)

Kali 2021.2 安装搜狗输入法

我平时比较懒,不愿意手工安装系统,而是使用官方的 VMware 镜像,但官方镜像的语言支持只提供了 English。对于偶尔需要输入个中文的我来说,还是有些困扰的。 1、 换源 从官网下载 deb 包:https://pinyin.sogou.com/linux/?r=pinyin 推荐使用 apt install ./sogouxxxxx.deb,自动解决依赖问题,省心。 2、左上角图标,所有程序中输入input,找到 Input Method。 在一堆弹窗之后,将输入法切换到 Fcitx。...

Mar 10, 2023
Kali 2021.2 安装搜狗输入法

Xiaoxk's Blog

28 posts

你看那通天的巨塔,每时每刻都有人往下跳。我小时候不懂,以为那是雪花。