如果有多張網(wǎng)卡,那么就會出現(xiàn)沖突問題了。沖突的原因是GATEWAY和DNS的配置,這兩個(gè)配置的表示默認(rèn)的地址。多張網(wǎng)卡的配置ifcfg-eth1,ifcfg-eth2,ifcfg-eth3…,后面的配置就會把前面的配置直接進(jìn)行覆蓋操作。啥意思呢?就是eth1的網(wǎng)關(guān)是A,eth2的網(wǎng)關(guān)是B,啟動的時(shí)候系統(tǒng)會去讀取這些配置文件的信息,加載eth1的配置時(shí),默認(rèn)網(wǎng)關(guān)是A;接著,加載eth2的配置時(shí),默認(rèn)網(wǎng)關(guān)變成了B。這樣A的IP就走不通了。
所以在有多個(gè)網(wǎng)卡的情況,不應(yīng)該只使用一個(gè)默認(rèn)的網(wǎng)關(guān)來實(shí)現(xiàn)通信,因?yàn)椴煌W(wǎng)卡的網(wǎng)關(guān)不一樣會導(dǎo)致一部分網(wǎng)卡由于網(wǎng)關(guān)不適用導(dǎo)致不能使用的情況。
配置步驟:
- 切換到/etc/sysconfig/network-scripts目錄下,把配置寫到對應(yīng)的網(wǎng)卡配置文件中,修改配置如下(不需要再配置GATEWAY)
if-ens33網(wǎng)卡配置文件
DEVICE=ens33 #網(wǎng)卡名稱
HWADDR=14:18:77:3B:6F:01 #MAC地址
TYPE=Ethernet #網(wǎng)卡類型
UuiD=314d1b96-70b2-498a-8711-6cb5ab264464 #唯一標(biāo)識
ONBOOT=yes #開機(jī)激活
BOOTPROTO=static #網(wǎng)卡獲取IP地址的方式,一般有
ipadDR=100.196.184.188
NETMASK=255.255.255.248
DNS1=51.136.192.6
DNS2=50.196.165.2
if-ens34網(wǎng)卡配置文件
DEVICE=ens33 #網(wǎng)卡名稱
HWADDR=14:18:77:3B:6F:02 #MAC地址
TYPE=Ethernet #網(wǎng)卡類型
UuiD=314d1b96-70b2-498a-8711-6cb11b264464 #唯一標(biāo)識
ONBOOT=yes #開機(jī)激活
BOOTPROTO=static
ipadDR=88.196.184.188
NETMASK=255.255.255.248
DNS1=51.136.192.6
DNS2=50.196.165.2
# 做在下面兩步之前,如果不做,route1 route2 改為數(shù)字即可
echo “101 route1” >> /etc/iproute2/rt_tables
echo “102 route2” >> /etc/iproute2/rt_tables
- 配置ens33的默認(rèn)路由
【配置規(guī)則如下】:
ip rule add from 網(wǎng)卡IP table 表數(shù)字 ip route add default via 網(wǎng)關(guān) dev 網(wǎng)卡名 table 表數(shù)字
配置ens33的路由規(guī)則(table后面的數(shù)字可以自己取,如果沖突系統(tǒng)會提示已存在,換一個(gè)不存在的就好,可以通過【ip rule show】命令查看已配置的內(nèi)容)
范例:
ip rule add from 100.196.184.188 table route1
# 配置ens33的路由表(可以通過【ip route show】命令查看已配置的內(nèi)容)
ip route add default via 100.196.184.19 dev ens33 table route1
范例:
ip rule add from 88.196.184.188 table route2
# 配置ens34的路由表(可以通過【ip route show】命令查看已配置的內(nèi)容)
ip route add default via 88.196.184.19 dev ens34 table route2
- 解決重啟不生效
方法1:
#添加默認(rèn)網(wǎng)關(guān)(route add default gw 網(wǎng)關(guān)地址 dev 網(wǎng)卡名稱):
route add default gw 100.196.184.19 dev ens33
route add default gw 88.196.184.19 dev ens34
方法2:
# 將以下命令
ip route add default via 100.196.184.19 dev ens33 table route1
ip route add default via 88.196.184.19 dev ens34 table route2
ip rule add from 100.196.184.188 table route1
ip rule add from 88.196.184.188 table route2