Linux多可支持255个路由表,查看当前路由表:
ip rule ls
0: from all lookup local
32766: from all lookup main
32767: from all lookup default
根据源IP或目标IP选择路由表:
#新建路由表ID与名称映射
echo '252 haiyun' >> /etc/iproute2/rt_tables
#设置源ip走特定路由表
ip rule add from 192.168.1.5 table haiyun pref 32764
#路由表默认路由
ip route add default via 192.168.1.2 dev pppoe-wan2 table haiyun
#刷新路由缓存
ip route flush cache
基于iptables标记选择路由表:
#iptables标记数据包
iptables -t mangle -APREROUTING -p udp --dport 53 -j MARK --set-mark 20
#iptables标记的数据包走特定路由表
ip rule add fwmark 20 table haiyun pref 32763
#路由表默认路由
ip route add default via 192.168.1.2 dev pppoe-wan2 table haiyun
查看当前路由表规则:
ip rule ls
0: from all lookup local
32764: from 192.168.1.5 lookup haiyun
32765: from all lookup main
32766: from all lookup main
32767: from all lookup default
查看路由表haiyun下路由项:
ip route ls table haiyun
default via 192.168.1.2 dev eth1