linux route命令学习

route命令用于显示和操作IP路由表。

没有增加路由之前,route命令的结果如下,
sh-# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
sh-#

用户可以使用route add/del来增加或删除一条路由。
由于我的系统中有程序能够自动识别到插入网线的动作,所以程序会自动增加一条路由,
这样就不需要用户自动手动配置了。
sh-# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface

default 192.168.0.1 0.0.0.0 UG 0 0 0 eth0
192.168.0.0 * 255.255.255.0 U 0 0 0 eth0
sh-#

使用-c选项,用来显示路由缓存。
路有缓存用于某个目的地址在路由表中查找到匹配路由项后,就把目的地址对应的路由项缓存起来,
其目的是提高路由查找的效率。
sh-# route -C
Kernel IP routing cache
Source Destination Gateway Flags Metric Ref Use Iface
192.168.0.162 192.168.0.155 192.168.0.155 0 0 0 eth0
192.168.0.162 192.168.0.1 192.168.0.1 0 0 25 eth0
localhost.local localhost.local localhost.local l 0 0 4 lo
192.168.0.150 192.168.0.162 192.168.0.162 il 0 0 21 lo
192.168.0.1 239.255.255.250 239.255.255.250 ml 0 0 161 lo
192.168.0.162 static-reverse. 192.168.0.1 0 0 0 eth0
192.168.0.162 time-a.timefreq 192.168.0.1 0 0 0 eth0
192.168.0.155 192.168.0.162 192.168.0.162 il 0 0 5 lo
192.168.0.162 static-reverse. 192.168.0.1 0 0 0 eth0
192.168.0.162 192.168.0.150 192.168.0.150 0 0 33 eth0
192.168.0.155 255.255.255.255 255.255.255.255 ibl 0 0 0 lo
192.168.0.1 192.168.0.162 192.168.0.162 il 0 0 44 lo
192.168.0.155 255.255.255.255 255.255.255.255 ibl 0 0 0 lo
192.168.0.162 192.168.0.155 192.168.0.155 0 0 1 eth0
192.168.0.162 239.255.255.250 239.255.255.250 ml 0 0 0 eth0
192.168.0.155 239.255.255.250 239.255.255.250 ml 0 0 21 lo
* 255.255.255.255 255.255.255.255 bl 0 0 0 lo
192.168.0.162 192.168.0.1 192.168.0.1 0 0 33 eth0
192.168.0.162 192.168.0.162 192.168.0.162 l 0 0 8 lo
* 255.255.255.255 255.255.255.255 bl 0 0 0 lo
192.168.0.162 192.168.0.1 192.168.0.1 0 0 0 eth0
192.168.0.162 192.168.0.150 192.168.0.150 0 0 0 eth0
192.168.0.155 255.255.255.255 255.255.255.255 ibl 0 0 0 lo
* 255.255.255.255 255.255.255.255 bl 0 0 0 lo
192.168.0.162 192.168.0.150 192.168.0.150 0 0 0 eth0
static-reverse. 192.168.0.162 192.168.0.162 l 0 0 4 lo
time-a.timefreq 192.168.0.162 192.168.0.162 l 0 0 0 lo
192.168.0.162 239.255.255.250 239.255.255.250 ml 0 0 104 eth0
192.168.0.162 static-reverse. 192.168.0.1 0 0 1 eth0

sh-#

使用route命令获得了路由相关的一些信息后,
可以通过ping网关或局域网中的其它主机来测试网络的连通性。
sh-# ping 192.168.0.155
PING 192.168.0.155 (192.168.0.155) 56(84) bytes of data.
64 bytes from 192.168.0.155: icmp_req=1 ttl=64 time=24.8 ms

64 bytes from 192.168.0.155: icmp_req=2 ttl=64 time=44.4 ms
64 bytes from 192.168.0.155: icmp_req=3 ttl=64 time=63.5 ms
^C
--- 192.168.0.155 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2010ms
rtt min/avg/max/mdev = 24.856/44.279/63.523/15.786 ms

sh-#

由于route命令实际上是通过读取/proc/net/route来获取路由信息的,
所以也可以直接查看这个文件来了解路由信息。
sh-# cat /proc/net/route
Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
eth0 00000000 0100A8C0 0003 0 0 0 00000000 0 0 0
eth0 0000A8C0 00000000 0001 0 0 0 00FFFFFF 0 0 0
sh-#

sh-# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface

default 192.168.0.1 0.0.0.0 UG 0 0 0 eth0
192.168.0.0 * 255.255.255.0 U 0 0 0 eth0
sh-#

上面的命令结果中看到一个问题:
为什么192.168.0.1这种IP地址形式的表示法使用cat /proc/net/route来查看确是0100A8C0呢?
这就涉及到字节序了,先了解一下概念:
little endian是数据的高字节位存储在内存的底地址中;
big endian是数据的高字节位存储在内存的高地址中。
网络字节序总是big endian。

而我的linux主机是little endian,所以如果要按字节读取数据,就会发现二者正好反过来了。
而无论系统的体系结构是big endian还是little endian,数据本身的值是不会发生改变的。
关于字节序的内容,更多可参考
http://blog.csdn.net/boyxulin1986/article/details/11660355

ps:关于字节序,一直有种不清不楚的感觉,希望后面有机会再整理一次。

另一种查看路由信息的方法是使用netstat -r命令,可以看到读出的数据格式都是一样的,
sh-# netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
default 192.168.0.1 0.0.0.0 UG 0 0 0 eth0
192.168.0.0 * 255.255.255.0 U 0 0 0 eth0
sh-#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值