您的位置:首页 > 运维架构 > Linux

Linux 多网卡多路由实现策略路由

2016-06-16 11:48 369 查看
  两个不在一段的IP看成一个电信一个网通来设置 就可以了。centos无问题
  1. vi /etc/iproute2/rt_tables,增加网通和电信两个路由表
  251 tel 电信路由表
  252 cnc 网通路由表
  2. 给网卡绑定两个地址用于电信和网通两个线路
  ip addr add 192.168.0.2/24 dev eth0
  ip addr add 10.0.0.2/24 dev eth1
  3、分别设置电信和网通的路由表
  电信路由表:
  #确保找到本地子网
  ip route add 192.168.0..0/24 via 192.168.0.2 dev eth0table tel
  #内部回环网络
  ip route add 127.0.0.0/8 dev lo table tel
  #192.168.0.1为电信网络默认网关地址
  ip route add default via 192.168.0.1 dev eth0 table tel
  网通线路路由表:
  #确保找到本地子网
  ip route add 10.0.0.0/24 via 10.0.0.2 dev eth1 table cnc
  #内部回环网络
  ip route add 127.0.0.0/8 dev lo table cnc
  #10.0.0.1是网通的默认网关
  ip route add default via 10.0.0.1 dev eth1 table cnc
  4、电信和网通各有自己的路由表,制定策略,让192.168.0.2的回应数据包走电信的路由表路由,10.0.0.2的回应数据包走网通的路由表路由
  ip rule add from 192.168.0.1 table tel
  ip rule add from 10.0.0.1 table cnc
  最后考虑特殊情况的网管需要批量添加IP到路由才能过,简化一下
  ip rule del from 192.168.0.0/24 table tel
  ip rule del from 10.0.0.0/24 table cnc

其二
网卡显示名称IP地址子网掩码网关备注
ens4f0172.31.192.201255.255.255.0172.31.192.254服务器A
ens9f0172.31.196.1255.255.255.0172.31.196.254服务器A
ens4f0172.31.192.202255.255.255.0172.31.192.254服务器B
ens9f0172.31.196.2255.255.255.0172.31.196.254服务器B
/172.25.168.44255.255.255.0172.25.168.254接入测试
网络配置,以服务器A为例,注意注释默认网关
cat/etc/sysconfig/network-scripts/ifcfg-ens4f0 DEVICE=ens4f0ONBOOT=yesBOOTPROTO=staticTYPE=EthernetIPADDR=172.31.192.201NETMASK=255.255.255.0#GATEWAY=172.31.192.254 cat/etc/sysconfig/network-scripts/ifcfg-ens9f0 DEVICE=ens9f0ONBOOT=yesBOOTPROTO=staticTYPE=EthernetIPADDR=172.31.196.1NETMASK=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 src172.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 src172.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 src172.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 src172.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 1006 0 0 ens9f0
169.254.0.0 0.0.0.0 255.255.0.0 U 1008 0 0 ens4f0
169.254.0.0 0.0.0.0 255.255.0.0 U 1014 0 0 br-ex
169.254.0.0 0.0.0.0 255.255.0.0 U 1015 0 0 br-int
172.31.192.0 0.0.0.0 255.255.255.0 U 0 0 0 ens4f0
172.31.196.0 0.0.0.0 255.255.255.0 U 0 0 0 ens9f0

#在接入测试服务器上验证连通性
ping 172.31.192.201
ping 172.31.196.1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Linux 网卡 路由表