環境:
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
https://segmentfault.com/a/1190000004165066