Ubuntu/Debian系通过clash增强模式,搭建旁路网关,实现旁路由(与软路由同效)
Ubuntu/Debian系通过clash增强模式,搭建旁路网关,实现旁路由(与软路由同效)(全程root执行,省略sudo)。在Ubuntu 20.04 LTS上成功
安装依赖
apt install git iproute2 iptables nftables jq -y
git clone https://github.com/Kr328/clash-premium-installer
cd clash-premium-installer
./installer.sh install
然后下载核心文件
github.com/Dreamacro/clash/releases/tag/v1.11.4
wget https://github.com/Dreamacro/clash/releases/download/v1.11.4/clash-linux-amd64-v3-v1.11.4.gz
解压,改名,赋权,复制到/usr/bin
gzip -d clash-linux-amd64-v3-v1.11.4.gz
mv clash-linux-amd64-v3-v1.11.4 clash
chmod 775 clash
cp clash /usr/bin
将配置文件config.yaml复制到/srv/clash/
cp <你的yaml> /srv/clash/config.yaml
config开头为
port: 7890
socks-port: 7891
allow-lan: true
mode: Rule
log-level: info
external-controller: :9090
dns:
enable: true
enhanced-mode: fake-ip
nameserver:
- 114.114.114.114 # 真实请求DNS,可多设置几个
tun:
enable: true
stack: system # 或 gvisor
dns-hijack:
- 1.0.0.1:53 # 请勿更改
后面不改,注意缩进
确认无误后,启动
systemctl start clash
查看是否正确
systemctl status clash
出现 Active: active (running)表示正常
设置开机启动
systemctl enable clash
安装面板yacd
apt install docker.io -y
docker run -p 80:80 -d --name yacd --rm ghcr.io/haishanh/yacd:master
#如果80端口已占用
docker run -p 1234:80 -d --name yacd --rm ghcr.io/haishanh/yacd:master
打开127.0.0.1或者你的局域网IP
输入http://127.0.0.1:9090
即可进入面板
理论上已经生效,但有些无线网卡只能本机生效,无法做旁路网关,
先安装iptables
apt install iptables -y
#不要照抄、请确保53端口没有被占用、以下假设的机器IP为192.168.1.2,局域网段为192.168.1.0/24
iptables -t nat -N clash
iptables -t nat -N clash_dns
# 这个是fake-ip对应的dns地址,一般不用动
iptables -t nat -A PREROUTING -p tcp --dport 53 -d 192.168.0.0/24 -j clash_dns
iptables -t nat -A PREROUTING -p udp --dport 53 -d 192.168.0.0/24 -j clash_dns
iptables -t nat -A PREROUTING -p tcp -j clash
# 让当前机器成为一个网关服务器
iptables -t filter -A FORWARD -j ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.0.0/16 -j MASQUERADE
# 这里需要注意的是,下面两行最后的 192.168.1.2 是当前旁路由的 IP 地址,请根据你自己的实际情况修改
# 目的DNS端口是后面clash配置里面的53
iptables -t nat -A clash_dns -p udp --dport 53 -d 192.168.1.2/24 -j DNAT --to-destination 192.168.1.2:53
iptables -t nat -A clash_dns -p tcp --dport 53 -d 192.168.1.2/24 -j DNAT --to-destination 192.168.1.2:53
# 绕过一些内网地址,(RETURN 表示退出当前Chain,返回到上一级的Chain继续匹配)
iptables -t nat -A clash -d 0.0.0.0/8 -j RETURN
iptables -t nat -A clash -d 10.0.0.0/8 -j RETURN
iptables -t nat -A clash -d 127.0.0.0/8 -j RETURN
iptables -t nat -A clash -d 169.254.0.0/16 -j RETURN
iptables -t nat -A clash -d 172.16.0.0/12 -j RETURN
iptables -t nat -A clash -d 192.168.0.0/16 -j RETURN
iptables -t nat -A clash -d 224.0.0.0/4 -j RETURN
iptables -t nat -A clash -d 240.0.0.0/4 -j RETURN
不过每次重启都要重新输入
以上192.168.x.x都要根据你的局域网IP修改,如果是10.x.x.x,则修改至相应IP
#可以试试
curl ip.sb
事实上不仅可以用iptables也可以用nftables
去设置网关和DNS到你的机器(如192.168.1.2)即可
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。