Linux 策略路由配置

时间:2023-03-08 22:10:57

策略路由配置

#编辑rt_tables
echo "192 net_192 " >> /etc/iproute2/rt_tables
echo "196 net_196 " >> /etc/iproute2/rt_tables #清空net_192路由表
ip route flush table net_192
# 添加一个路由规则到 net_192 表,这条规则是 net_192 这个路由表中数据包默认使用源 IP 172.31.192.201 通过 ens4f0 走网关 172.31.192.254
ip route add default via 172.31.192.254 dev ens4f0 src 172.31.192.201 table net_192
#来自 172.31.192.201 的数据包,使用 net_192 路由表的路由规则
ip rule add from 172.31.192.201 table net_192 #清空net_196路由表
ip route flush table net_196
#添加一个路由规则到 net_196 表,这条规则是 net_196 这个路由表中数据包默认使用源 IP 172.31.196.1 通过 ens9f0 走网关 172.31.196.254
ip route add default via 172.31.196.254 dev ens9f0 src 172.31.196.1 table net_196
#来自 172.31.196.1 的数据包,使用 net_196 路由表的路由规则
ip rule add from 172.31.196.1 table net_196 #添加默认网关
route add default gw 172.31.192.254 #如果需要自启动生效可以写进配置文件也可以加入rc.local
vi /etc/rc.local ip route flush table net_192
ip route add default via 172.31.192.254 dev ens4f0 src 172.31.192.201 table net_192
ip rule add from 172.31.192.201 table net_192
ip route flush table net_196
ip route add default via 172.31.196.254 dev ens9f0 src 172.31.196.1 table net_196
ip rule add from 172.31.196.1 table net_196
route add default gw 172.31.192.254 #查看路由表
route -n Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
169.254.0.0 0.0.0.0 255.255.0.0 U ens9f0
169.254.0.0 0.0.0.0 255.255.0.0 U ens4f0
169.254.0.0 0.0.0.0 255.255.0.0 U br-ex
169.254.0.0 0.0.0.0 255.255.0.0 U br-int
172.31.192.0 0.0.0.0 255.255.255.0 U ens4f0
172.31.196.0 0.0.0.0 255.255.255.0 U ens9f0 #在接入测试服务器上验证连通性
ping 172.31.192.201
ping 172.31.196.1 作者:王奥OX
链接:https://www.jianshu.com/p/19444d8f9b61
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。