服务器A网卡文件如下
cat /etc/sysconfig/network-scripts/ifcfg-ens4f0
DEVICE=ens4f0
ONBOOT=yes
BOOTPROTO=static
TYPE=Ethernet
IPADDR=172.31.192.201
NETMASK=255.255.255.0
#GATEWAY=172.31.192.254
cat /etc/sysconfig/network-scripts/ifcfg-ens9f0
DEVICE=ens9f0
ONBOOT=yes
BOOTPROTO=static
TYPE=Ethernet
IPADDR=172.31.196.1
NETMASK=255.255.255.0
#GATEWAY=172.31.196.254
策略路由配置:
#编辑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
自动生效写入/etc/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