最近在学Linux的IP命令,在做实验的时候发现一个问题。按照文档的描述,指令:ip link set dev eth0 up
的作用和指令ifup eth0
的作用是一样的,但是在试验过程中发现,执行ip link set dev eth0 up
后,网卡的状态虽然显示为UP,但是网络却不通。通过仔细检查,发现执行完上述指令后,系统虽然启用了网卡,但是并没有添加路由信息,导致网络不通,具体情况如下:
首先,先来看一下当前的系统情况:
[root@linux ~]# ip addr show
1: lo: mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:b5:e4:0a brd ff:ff:ff:ff:ff:ff
inet 192.168.137.100/24 brd 192.168.137.255 scope global eth0
inet6 fe80::20c:29ff:feb5:e40a/64 scope link
valid_lft forever preferred_lft forever
3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:b5:e4:14 brd ff:ff:ff:ff:ff:ff
inet 172.16.1.1/24 brd 172.16.1.255 scope global eth1
inet6 fe80::20c:29ff:feb5:e414/64 scope link
valid_lft forever preferred_lft forever
[root@linux ~]#
当前eth0网卡的状态是UP。再来看一下目前的路由情况:[root@linux ~]# ip route show
172.16.1.0/24 dev eth1 proto kernel scope link src 172.16.1.1
192.168.137.0/24 dev eth0 proto kernel scope link src 192.168.137.100
169.254.0.0/16 dev eth0 scope link metric 1002
169.254.0.0/16 dev eth1 scope link metric 1003
default via 192.168.137.2 dev eth0
从路由表中可以看出,当前的默认网关是192.168.137.2
现在我们用ip命令禁用网卡eth0,并查看路由表
[root@linux ~]# ip link set dev eth0 down
[root@linux ~]# ip route show
[root@linux Desktop]# ip route show
172.16.1.0/24 dev eth1 proto kernel scope link src 172.16.1.1
169.254.0.0/16 dev eth1 scope link metric 1003
[root@linux Desktop]#
从上可以看出,禁用网卡后,默认路由也被删除了。
现在重启网卡:[root@linux Desktop]# ip link set dev eth0 up
[root@linux Desktop]# ip addr show
1: lo: mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:b5:e4:0a brd ff:ff:ff:ff:ff:ff
inet 192.168.137.100/24 brd 192.168.137.255 scope global eth0
inet6 fe80::20c:29ff:feb5:e40a/64 scope link
valid_lft forever preferred_lft forever
3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:b5:e4:14 brd ff:ff:ff:ff:ff:ff
inet 172.16.1.1/24 brd 172.16.1.255 scope global eth1
inet6 fe80::20c:29ff:feb5:e414/64 scope link
valid_lft forever preferred_lft forever
[root@linux Desktop]#
从上可以看出,此时eth0网卡已经是up状态,但是无法ping通公网IP[root@linux Desktop]# ping 8.8.8.8
connect: Network is unreachable
[root@linux Desktop]#
查看路由:[root@linux Desktop]# ip route show
172.16.1.0/24 dev eth1 proto kernel scope link src 172.16.1.1
192.168.137.0/24 dev eth0 proto kernel scope link src 192.168.137.100
169.254.0.0/16 dev eth1 scope link metric 1003
[root@linux Desktop]#
发现缺少默认路由
手动配置路由后,继续测试,结果如下:[root@linux Desktop]# ip route add default via 192.168.137.2
[root@linux Desktop]# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=2 ttl=128 time=50.1 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=128 time=50.1 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=128 time=49.9 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=128 time=50.7 ms
^C
--- 8.8.8.8 ping statistics ---
5 packets transmitted, 4 received, 20% packet loss, time 4756ms
rtt min/avg/max/mdev = 49.955/50.247/50.739/0.336 ms
[root@linux Desktop]#
总结:
虽然都说指令 ip link set eth0 up和指令ifup eth0的作用是一样的,但是通过实践表明两者还是有区别的。ip link set eth0 up仅仅启用网卡,但是不会配置路由;而ifup指令不仅启用网卡,同时会配置默认路由。