使用Debia实现软路由
设备为六网口工控机,之前使用pve-proxmox+lede,最近因为磁盘空间不足升级时pve发生错误不能正常使用,本人没有能力修复所以参考了文章使用debian当软路由重装了系统。记录下折腾过程
- 光猫修改为桥接,Lan: 192.168.1.1
光猫超级用户密码获取
- debian Lan: 10.10.10.2
- debian Wan: 192.168.1.2(方便连接光猫)
- Ap(k2p-斐讯路由) Lan: 10.10.10.1
安装Debian
因为手里有个U盘(Ventoy)正好里面有Debian10镜像,没有再去下载11,就直接安装了。
分区直接使用默认推荐vg-root,vg-home
debian安装完成还不能直接拔号安装软件,所以使用k2p拔号,网线连接k2p Lan和工控机左-右第六口(准备用来当Debian软路由Wan口)
修改apt源
使用清华镜像源
- 备份apt源清单文件
sources.list
cp /etc/apt/sources.list /etc/apt/sources.list.bak
- 使用http清华源
cat > /etc/apt/sources.list << EOF
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free
EOF
2
3
4
5
6
7
8
9
10
11
12
- 安装
apt-transport-https ca-certificates
后改成https源
apt update
apt install apt-transport-https ca-certificates
sed -i 's/http:/https:/g' /etc/apt/sources.list
apt update
apt upgrade
2
3
4
5
设置网桥
apt insta bridge-utils
- 查看接口名称
ip -f inet addr|grep :
, 我的是enp1s0 enp2s0...enp6s0 - 设置enp1s0-enp4s0为Lan口(左-右,前4个网口)
cat > /etc/network/interface <<EOF
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
auto enp1s0
allow-hotplug enp1s0
iface enp1s0 inet manual
auto enp2s0
allow-hotplug enp2s0
iface enp2s0 inet manual
auto enp3s0
allow-hotplug enp3s0
iface enp3s0 inet manual
auto enp4s0
allow-hotplug enp4s0
iface enp4s0 inet manual
auto enp5s0
allow-hotplug enp5s0
iface enp5s0 inet manual
auto enp6s0
allow-hotplug enp6s0
iface enp6s0 inet static
address 192.168.1.2/24
auto vmbrlan
iface vmbrlan inet static
address 10.10.10.2
netmask 255.255.255.0
bridge-ports enp1s0 enp2s0 enp3s0 enp4s0
bridge-stp off
bridge-fd 0
iface vmbrlan inet6 static
address fc00:a:b::2
netmask 64
bridge-ports enp1s0 enp2s0 enp3s0 enp4s0
bridge-stp off
bridge-fd 0
EOF
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
- 重启网络服务生效
systemctl restart networking
设置pppoe拔号
- 安装pppoe配置工具
apt install pppoeconf
- 断开k2p拔号,将k2p wan口和debian wan口网线拔下,光猫Lan口-debian wan口。debian Lan口-k2p Lan口。
- 配置pppoe
pppoeconf
会自动扫描到debian Wan口,输入用户密码,/etc/network/interface 会在底部增加
+ auto dsl-provider
+ iface dsl-provider inet ppp
+ pre-up /bin/ip link set enp6s0 up # line maintained by pppoeconf
+ provider dsl-provider
2
3
4
现在应该已经拔号成功了
- 查看log
plog
- 拔号
pon dsl-provider
- 断开
poff
不知道是不是自动拔号,网上有人设置设置了脚本实现自动重拔
mdkir -p /home/username/app/repppoe
cat > /home/username/app/repppoe/repppoe.sh << EOF
#!/bin/bash
if ! ping -c3 223.5.5.5 >/dev/null 2>&1
then
/usr/bin/pon dsl-provider
fi
EOF
2
3
4
5
6
7
8
9
开启转发和设置iptables
- 编辑 /etc/sysctl.conf
+ net.ipv4.ip_forward=1
+ net.ipv6.conf.all.forwarding=1
# ppp0是拔号生成的网卡
+ net.ipv6.conf.ppp0.forwarding=1
+ net.ipv6.conf.ppp0.accept_ra=2
# 开启bbr,听说是内核5以上才有效
+ net.core.default_qdisc = fq
+ net.ipv4.tcp_congestion_control = bbr
2
3
4
5
6
7
8
systcl -p
生效- 设置iptables
# 转发给局域网客户端实现路由器基本功能,设置后客户端网关
# 填写10.10.10.2可上网,drop通过ppp0进入的流量提升安全性
# ipv4
# nat完成转发
iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
# 允许已建立的连接
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -p icmp -j ACCEPT
iptables -A INPUT -i ppp0 -j DROP
# ipv6
ip6tables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
# 没这个不能获得ipv6公网地址
ip6tables -A INPUT -i ppp0 -p ipv6-icmp -j ACCEPT
ip6tables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
ip6tables -A INPUT -i ppp0 -j DROP
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- iptables 持久化
apt install iptables-persistent
# 安装iptables-persistent
# 实现iptables开机自启,全yes就可
# 会生成 netfilter-persistent
# 开关机时运行一次
# 必须使用root用户 su -
iptables-persistent-save > /etc/iptable/ipv4.rules
ip6tables-persistent-save > /etc/iptables/ipv6.rules
2
3
4
5
6
7
8
9
ipv6
参考文章中作者说不设置/etc/ppp/options
中ipv6 ,
也可获取ipv6,实际上我不能 上面在Lan网桥vmbrlan上设置了ipv6静态地址fc00:a:b::2
是为了方便设置ip6tables的开放端口
dhcp
这里我并没有使用文章中的dhcpcd和dnsmasq, dns和dhcp服务使用了AdGuardHome提供,兼顾去广告功能,还不错,就是ipv6分配存在问题,有的设备有ipv6,有的没有。也不必须用就无所谓了。
使用dnsmasq给局域网分配ip
#sudo vim /etc/dnsmasq.conf
interface=vmbrlan
# 开启ra
enable-ra
# ipv4 range
dhcp-range=vmbrlan,10.10.10.200,10.10.10.250,12h
dhcp-range=vmbrlan,::1,constructor:vmbrlan,ra-names,12h
# 网关
dhcp-option=3,10.10.10.2
# dns server
dhcp-option=6,10.10.10.2
#dhcp-option=option6:dns-server,[fc00:a:b::2]
2
3
4
5
6
7
8
9
10
11
12
静态ip配置在 /etc/dnsmasq.d/static.conf
# win10主机 无线和有线网卡mac 名称为U59
dhcp-host=84:4x:xx:xx:xx:FB,rf:zx:bd:rf:74:df,U59,10.10.10.29,infinite
# 手机
dhcp-host=c4:rd:a9:22:0j:rt,ios,10.10.10.20,infinite
2
3
4
如果想给不同的设备分配不同的dns可以增加类似以下设置
# 10.10.10.2 为你想要为tag指定的dns
dhcp-option=tag:clash,option:dns-server,10.10.10.2
## ipad
dhcp-mac=set:clash,58:25:07:01:5d:bh
2
3
4
5
因为没有使用 dhcpc5
或 wide-dhcpv6-client
获取公网ipv6前辍,这样配置后局域网的设备获得 fc00:a:b::xxx
的ipv6地址, 通过nat的方式访问ipv6网络资源。ip.sb
显示的ipv6为debian的公网ipv6地址
留学功能
使用clash
clash也开了dns, 监听53端口, AdGuardHome的dns监听5353端口, clash国内上游dns服务器设置成AdGuardHome的5353, 国外dns服务使用 https://1.1.1.1/dns-query
和 https://8.8.8.8/dns-query
iptables 中设置需要留学的客户端
-t nat -N CLASH
-t nat -A PREROUTING -s 10.10.10.20/32 ! -d 10.10.10.0/24 -p tcp -j CLASH
-t nat -A CLASH -d 192.168.0.0/16 -j RETURN
-t nat -A CLASH -d 其他本地网段 -j RETURN
-t nat -A CLASH -j REDIRECT --to-port 7389
2
3
4
5
代理本机流量
简单的设置了OUTPUT上的redirect让非clash的流量回到clash的redir-port:7893
实现代理本机流量, 为了避免回环的问题在过滤掉clash出站流量。
# 使用root账户
su -
# 建立用户 `clash` gid设置了12345
echo "clash:x:0:12345:clash for local:/nonexistent:/usr/sbin/nologin" >> /etc/passwd
# 后来加入Adguard用户到clash的组, 个人认为Adguard不需要代理
echo "adguard:x:0:12345:adguard direct:/nonexistent:/usr/sbin/nologin" >> /etc/passwd
echo "clashNotProxy:x:12345:adguard,otherUser" >>/etc/group
exit
# 检查一下
sudo -u clash id
sudo -u adguard id
groups clash
groups adguard
2
3
4
5
6
7
8
9
10
11
12
13
添加iptables规则
# 新建一个管理OUTPUT规则
-t nat -N CLASHLOCAL
# clash和adguard的流量直接放出去,补充其他不需要代理的
-t nat -A CLASHLOCAL -m owner --gid-owner 12345 -j RETURN
# curl wget之类查询dns直接到clash的dns查询
-t nat -A CLASHLICAL -p udp -m udp --dport 53 -j DNAT --to-destination 127.0.0.1:53
# 代理tcp
-t nat -A CLASHLOCAL -p tcp -j REDIRECT --to-ports 7893
# 本机流量是从OUTPUT出的,在这添加
-t nat -A OUTPUT -j CLASHLOCAL
# 现在可以测试下是否代理本机成功, 返回301,200之类
# 没代理本机之前我的debian返回是000
curl -so /dev/null -w "%{http_code}" google.com
# 测试前可以先确保clash是工作正常
# 7890是我的clash配置文件中设定的mix-port
curl -so /dev/null -w "%{http_code}" google.com -x socks5://user:password@127.0.0.1:7890
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
我是小白, 这样设置可能存在问题, 只是暂时没发现什么异常
出现的问题
纯ipv6节点不能正常工作
我有纯ipv4和纯ipv6的两种vps,以前局域设备未使用ipv6,ipv6的vps一直没启用。以为不开启ipv6的的情况下是不能使用ipv6的vps代理。事实上纯ipv6的备用vps(像euserv这种白嫖机)套上cloudflare后是可以使用的。
具体表现是,手机wifi处设置了http代理,填上clash的 mix-port
7890后, euserv的节点正常工作了。redir-host
为什么不能使用呢? 具体细节没仔细了解过,个人感觉是clash给euserv发送了ipv4地址的信息, euserv不能连接。在clash的dns选项中配置 fake-ip
解决了问题。fake-ip
模式可能是发送域名信息到euserv, 因为euserv使用了NS64的DNS, 所以可以工作。本人很菜, 就这样猜想吧。
所以当我在clash使用fake-ip
后,又启用了速度最优的节点选项把白嫖节点也加进来。
使用fake-ip
后部分局域网设备上网异常
dnsmasq设置的默认dns server
是clash的dns(10.10.10.2),这个dns返回的192.168.0.x
的fake-ip
。流量未PREROUTING到clash的客户端直接"王德发?"
解决: 1. 让所有客户端都redir到clash(未使用这种方案, 不能让未成年人接受太多网课教育); 2. 指定客户端dns为AdguardHome的dns。
这里采用了方案2, 因为AdguardHome(10.10.10.2:5353)和clash(10.10.10.2:53)的dns只是端口不同, 我也不知道在dnsmasq中怎么修改默认的dns地址带端口, 所以在iptables中将部分设备的udp目标端口53的流量DNAT到5353(AdguardHome的dns)
这样设置后在AdguardHome的网页端可以随意控制客户端上网(不给小朋友解析游戏的域名就是)
-t nat -N ADGUARDDNS
-t nat -A ADGUARDDNS -i vmbrlan ! -p udp -j RETURN
-t nat -A ADGUARDDNS -m udp ! --dport 53 -j RETURN
# 先排除需要代理的ip或ip范围
-t nat -A ADGUARDDNS -s 10.10.10.20 -j RETURN
-t nat -A ADGUARDDNS -j DNAT --to-destination 10.10.10.2:5353
-t nat -A PREROUTING -j ADGUARDDNS
2
3
4
5
6
7
可能有错误或疏漏,待补充