《开发板命令 — route命令学习》

1.route命令

  route命令用于显示和操作IP路由表。要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现。在Linux系统中,设置路由通常是 为了解决以下问题:该Linux系统在一个局域网中,局域网中有一个网关,能够让机器访问Internet,那么就需要将这台机器的IP地址设置为 Linux机器的默认路由。要注意的是,直接在命令行下执行route命令来添加路由,不会永久保存,当网卡重启或者机器重启之后,该路由就失效了;要想永久保存,可以保存到配置文件。linux 默认只支持一条默认路由,当重新启动网口时,会把其他默认路由去掉,只剩下一条该网口生成的默认路由。当然可以通过 route 命令手动添加多条默认路由,如果多条路由一样,则选择最开始找到的路由(排在前面的路由)。

2.route命令使用

route -n

[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.233.0   0.0.0.0         255.255.255.0   U     0      0        0 eth1

实际使用:

   (1)我们通过路由表可以知道有两条相同默认路由可以选择,由于先找到192.168.233.2 网关路由,所以最后选择了 192.168.233.2 网关。

[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.233.2   0.0.0.0         UG    0      0        0 eth1
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.233.0   0.0.0.0         255.255.255.0   U     0      0        0 eth1
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# traceroute www.baidu.com
traceroute to www.baidu.com (14.215.177.39), 30 hops max, 60 byte packets
 1  gateway (192.168.233.2)  0.200 ms  0.109 ms  0.141 ms
 2  * * *
 3  * * *
 4  * * *

  (2) 删除原先路由,重新添加路由

  重新添加路由后,我们通过路由表可以知道有两条相同默认路由可以选择,由于先找到192.168.1.1 网关路由,所以最后选择了 192.168.1.1 网关。

[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.233.2   0.0.0.0         UG    0      0        0 eth1
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.233.0   0.0.0.0         255.255.255.0   U     0      0        0 eth1
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# route del default gw 192.168.233.2
[root@localhost ~]# 
[root@localhost ~]# route del default gw 192.168.1.1
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# route add default gw 192.168.233.2
[root@localhost ~]# 
[root@localhost ~]# route add default gw 192.168.1.1
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
0.0.0.0         192.168.233.2   0.0.0.0         UG    0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.233.0   0.0.0.0         255.255.255.0   U     0      0        0 eth1
[root@localhost ~]#
[root@localhost ~]# traceroute www.baidu.com
traceroute to www.baidu.com (14.215.177.39), 30 hops max, 60 byte packets
 1  gateway (192.168.1.1)  2.683 ms  2.432 ms  15.680 ms
 2  * * *
 3  * * *
 4  * * *

  

默认路由:

[root@localhost ~]# route add default  gw 192.168.233.2 dev eth1
# 添加默认路由方式一,default
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.233.2   0.0.0.0         UG    0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.233.0   0.0.0.0         255.255.255.0   U     0      0        0 eth1
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# route del default gw 192.168.233.2 dev eth1
# 删除默认路由方式一,default
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.233.0   0.0.0.0         255.255.255.0   U     0      0        0 eth1
[root@localhost ~]#
[root@localhost ~]# route add -net 0.0.0.0 netmask 0.0.0.0 gw 192.168.233.2 dev eth1
# 添加默认路由方式二,-net 0.0.0.0 netmask 0.0.0.0
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.233.2   0.0.0.0         UG    0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.233.0   0.0.0.0         255.255.255.0   U     0      0        0 eth1
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# route del -net 0.0.0.0 netmask 0.0.0.0 gw 192.168.233.2 dev eth1
# 删除默认路由方式二,default
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.233.0   0.0.0.0         255.255.255.0   U     0      0        0 eth1

  

原文地址:https://www.cnblogs.com/zhuangquan/p/13522372.html