我查阅了多个教程,了解如何在两个互联网连接上实现负载平衡。但是,所有流量仍然通过度量值最低的默认路由进行路由,我不知道为什么
iptables -t nat -F
iptables -t mangle -F
iptables -F
iptables -X
ip rule delete from all fwmark 0x1
ip rule delete from all fwmark 0x2
ip rule flush
ip rule add lookup main priority 32766
iptables -A PREROUTING -t mangle -j CONNMARK --restore-mark
iptables -A PREROUTING -t mangle -m mark --mark 0x0 -m statistic \
--mode nth --every 2 --packet 0 -j MARK --set-mark 1
iptables -A PREROUTING -t mangle -m mark --mark 0x0 -m statistic \
--mode nth --every 2 --packet 1 -j MARK --set-mark 2
iptables -A POSTROUTING -t mangle -j CONNMARK --save-mark
ip route flush table wlp3s0
ip route flush table enp0s20u2
ip route add default via 46.37.116.1 table wlp3s0
ip route add 46.37.116.0/23 dev wlp3s0 table wlp3s0
ip route add default via 192.168.42.129 table enp0s20u2
ip route add 192.168.42.0/24 dev enp0s20u2 table enp0s20u2
ip rule add fwmark 1 lookup wlp3s0 priority 1000
ip rule add fwmark 2 lookup enp0s20u2 priority 1000
iptables -A POSTROUTING -t nat -m mark --mark 1 -j SNAT --to-source 46.37.116.111
iptables -A POSTROUTING -t nat -m mark --mark 2 -j SNAT --to-source 192.168.42.130
echo 0 > "/proc/sys/net/ipv4/conf/wlp3s0/rp_filter"
echo 0 > "/proc/sys/net/ipv4/conf/enp0s20u2/rp_filter"
ip route flush cache
IP路由:
ip route
default via 192.168.42.129 dev enp0s20u2 src 192.168.42.130 metric 207
default via 46.37.116.1 dev wlp3s0 src 46.37.116.111 metric 303
46.37.116.0/23 dev wlp3s0 proto kernel scope link src 46.37.116.111 metric 303
192.168.42.0/24 dev enp0s20u2 proto kernel scope link src 192.168.42.130 metric 207
ip 路由列表所有表 wlp3s0
default via 46.37.116.1 dev wlp3s0
46.37.116.0/23 dev wlp3s0 scope link
ip 路由列表所有表 enp0s20u2
default via 192.168.42.129 dev enp0s20u2
192.168.42.0/24 dev enp0s20u2 scope link
ip 规则
0: from all lookup local
1000: from all fwmark 0x1 lookup wlp3s0
1000: from all fwmark 0x2 lookup enp0s20u2
32766: from all lookup main