CentOS下双网卡双IP不同IP段配置
环境:
eth0:10.0.7.2 gw :10.0.7.254 netmask:255.255.255.0
eth1:168.6.101.2 gw :168.6.101.254 netmask:255.255.255.0
一、配置网卡:
cat /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE="Ethernet" BOOTPROTO="static" UUID="b57f0cc6-7ef2-4c1b-9e25-60b9db343523" DEVICE="eth0" ONBOOT="yes" IPADDR="10.0.7.2" NETMASK="255.255.255.0" GATEWAY="10.0.7.254"
cat /etc/sysconfig/network-scripts/ifcfg-eth1 TYPE="Ethernet" BOOTPROTO="static" UUID="b57f0cc6-7ef2-4c1b-9e25-60b9db343524" DEVICE="eth1" ONBOOT="yes" IPADDR="168.6.101.2" NETMASK="255.255.255.0"
可以看出第二张网卡没有网关,因为如果两张网卡同时配置不同的网关会造成冲突。
二、配置路由表
添加两张路由表,分别用于网卡1和网卡2的默认路由,因为默认路由在同一个路由表上只能默认有一个0.0.0.0/24指向一个网关,这也是上面为什么第二块网卡上不配置网关。
cat /etc/iproute2/rt_tables # # reserved values # 255 local 254 main 253 default 252 table1 251 table2 0 unspec # # local # #1 inr.ruhep
这里的路由表左侧ID从上往下递减,然后匹配顺序同理。
在开机启动增加如下路由表配置:
cat /etc/rc.local
ip route flush table table1 ip route add default via 10.0.7.254 dev eth0 src 10.0.7.2 table table1 ip rule add from 10.0.7.2 table table1 ip route flush table table2 ip route add default via 168.6.101.254 dev eth1 src 168.6.101.2 table table2 ip rule add from 168.6.101.2 table table2
参考:
https://www.linuxidc.com/Linux/2018-05/152427.htm
http://blog.51cto.com/hao360/1346723
https://blog.csdn.net/zm9273672/article/details/3498234
http://blog.51cto.com/ghostlan/1299090
https://blog.csdn.net/Bristy_pu/article/details/12969903
https://blog.csdn.net/l1028386804/article/details/77455929
https://blog.csdn.net/l1028386804/article/details/77487639
https://blog.csdn.net/younger_china/article/details/72081779