Linux雙網口連接外網問題

環境

eth0需要連接外網,eth1連接內網。實際測試時,發現通過eth0無法ping通外網。

配置文件信息,cat /etc/network/interfaces

# /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)
 
# The loopback interface
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
   address 172.32.11.60
   netmask 255.255.255.0
   network 172.32.11.1
   gateway 172.32.11.1

auto eth1
iface eth1 inet static
   address 192.168.254.241
   netmask 255.255.255.240
   network 192.168.254.241
   gateway 192.168.254.241

調試

查看路由表,發現有兩個默認網關,eth1的mx6qsabresd.lo(192.168.254.241)和eth0的172.32.11.1

root@imx6qsabresd:~# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         imx6qsabresd.lo 0.0.0.0         UG    0      0        0 eth1
default         172.32.11.1     0.0.0.0         UG    0      0        0 eth0
172.32.11.0     *               255.255.255.0   U     0      0        0 eth0
192.168.254.240 *               255.255.255.240 U     0      0        0 eth1

路由跟蹤外網ip(國內通用DNS地址:114.114.114.114),發現報文沒有通過eth0的網關,而是跳到了eth1的網關,然後就停止了

root@imx6qsabresd:~# traceroute 114.114.114.114
traceroute to 114.114.114.114 (114.114.114.114), 30 hops max, 38 byte packets
 1  imx6qsabresd.lo.local (192.168.254.241)  2999.070 ms !H  2995.744 ms !H  2999.838 ms !H

解決

1.屏蔽掉eth1

2.刪除路由表中eth1的默認網關

3.調整路由表中eth1的默認網關優先級

現在按照方法3解決,在啓動腳本中添加

# modify priority of eth1 gateway 
gw_eth1=`route -n | grep 'UG' | grep 'eth1' | awk '{print $2}'`
if [ ! $gw_eth1 ]; then
  echo "There is not found default gateway on eth1."
else
  route del default gw $gw_eth1 dev eth1
  route add default gw $gw_eth1 dev eth1 metric 100
fi

腳本解釋

route命令是按域名顯示網關的,route -n命令會解析對應的域名,按ip數字形式顯示。這裏需要加上參數-n,因爲route del或者route add不能識別eth1的網關域名“imx6qsabresd.lo”,只能識別“192.168.254.241”

root@imx6qsabresd:~# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.254.241 0.0.0.0         UG    0      0        0 eth1
0.0.0.0         172.32.11.1     0.0.0.0         UG    0      0        0 eth0
172.32.11.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.254.240 0.0.0.0         255.255.255.240 U     0      0        0 eth1
root@imx6qsabresd:~# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         imx6qsabresd.lo 0.0.0.0         UG    0      0        0 eth1
default         172.32.11.1     0.0.0.0         UG    0      0        0 eth0
172.32.11.0     *               255.255.255.0   U     0      0        0 eth0
192.168.254.240 *               255.255.255.240 U     0      0        0 eth1
root@imx6qsabresd:~# route -n | grep 'UG' | grep 'eth1' | awk '{print $2}'
192.168.254.241
root@imx6qsabresd:~# route | grep 'default' | grep 'eth1' | awk '{print $2}'
imx6qsabresd.lo

路由表中的metric下的數字爲路由指定所需躍點數的整數值(範圍是1~9999),它用來在路由表裏的多個路由中選擇與轉發包中的目標地址最爲匹配的路由。所選的路由具有最少的躍點數。簡單來說,metric 值越小,優先級越高

驗證結果

上面修改eth1的默認網關優先級後,metric修改爲100

root@imx6qsabresd:~# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         172.32.11.1     0.0.0.0         UG    0      0        0 eth0
default         imx6qsabresd.lo 0.0.0.0         UG    100    0        0 eth1
172.32.11.0     *               255.255.255.0   U     0      0        0 eth0
192.168.254.240 *               255.255.255.240 U     0      0        0 eth1

路由跟蹤外網(www.baidu.com)。注:本機DNS已經驗證OK,若DNS沒有添加好,則要改成外網的某個ip地址。

root@imx6qsabresd:~# traceroute www.baidu.com
traceroute to www.baidu.com (14.215.177.38), 30 hops max, 38 byte packets
 1  172.32.11.1 (172.32.11.1)  2.976 ms  3.899 ms  2.977 ms
 2  *  *  *
 3  119.139.196.1 (119.139.196.1)  30.250 ms  2.636 ms  2.032 ms
 4  121.34.247.69 (121.34.247.69)  2.013 ms  202.105.153.225 (202.105.153.225)  1.684 ms  2.350 ms
 5  202.105.158.34 (202.105.158.34)  5.207 ms  202.105.158.54 (202.105.158.54)  6.393 ms  202.105.158.58 (202.105.158.58)  3.519 ms
 6  113.96.4.78 (113.96.4.78)  12.373 ms  113.96.4.118 (113.96.4.118)  5.366 ms  113.96.4.46 (113.96.4.46)  7.555 ms
 7  106.96.135.219.broad.fs.gd.dynamic.163data.com.cn (219.135.96.106)  5.493 ms  5.118 ms  5.378 ms
 8  14.29.121.190 (14.29.121.190)  4.511 ms  14.215.32.94 (14.215.32.94)  6.072 ms  14.29.121.186 (14.29.121.186)  38.263 ms
 9  *  *  *
10  *  *  *

上面的報文是從eth0的網關出去的,可以ping通外網了

root@imx6qsabresd:~# ping www.baidu.com
PING www.baidu.com (14.215.177.39): 56 data bytes
64 bytes from 14.215.177.39: seq=0 ttl=55 time=6.247 ms
64 bytes from 14.215.177.39: seq=1 ttl=55 time=5.916 ms
64 bytes from 14.215.177.39: seq=2 ttl=55 time=5.989 ms

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章