108. route

Date:

2018-09

108.1. 命令格式

命令格式:

route [-CFvnee]

route [-v] [-A family] add [-net|-host] target [netmask Nm] [gw Gw] [metric N] [mss M] [window W] [irtt I] [reject] [mod] [dyn] [reinstate] [[dev] If]

route [-v] [-A family] del [-net|-host] target [gw Gw] [netmask Nm] [metric N] [[dev] If]

route [-V] [–version] [-h] [–help]

108.2. 所属用户

命令路径:

/sbin/route

需要权限:

系统管理员,普通用户只有route查看权限。不能为系统添加/删除路由

108.3. 使用指导

显示或者操作本地系统的路由表(show / manipulate the IP routing table)。

108.4. 参数

参数及参数作用:

参数

作用

-A family

显示指定类型设备的路由条目,(inet/inet6)

-F

显示操作内核的FIB(转发信息库)路由表。这是默认值。

-C

显示操作系统内核缓存的路由信息

-v

显示详细的处理信息

-n

不解析名字,这样加快了route的结果显示速度

-e

netstat 格式显示路由表, -ee 显示更多的信息

del

删除指定路由信息

add

添加指定路由信息

target

目的网络或目的主机,可以指定 ip 地址或者 ip/network

-net

目标网络是一个子网

-host

目标网络是一个主机IP

netmask NM

增加子网路由的时候用 netmask 关键字指定子网掩码 NM

gw GW

设置网关地址。当匹配子网时数据包需要发送经过这个网关地址

metric M

设置路由条目的度量值 M

mss M

设置路由条目最大传输单元(MTU),值为: M bytes

window W

设置TCP链接的窗口大小 W bytes ,这个值只用在AX.25网络

irtt I

设置TCP初始链接往返时间(irtt),默认使用300ms,可以设置1-12000,这通常只在AX.25上使用网络。

reject

将指定路由条目设置为阻塞状态,会让数据转发时查找路由条目失败。

mod,dyn,reinstate

安装动态或修改的路由。这些标志用于诊断目的,通常仅通过路由守护进程设置。

dev If

强制路由条目关联网卡设备,如果没有设置则由内核自动关联设备。

route命令显示的结果中关键字解释

参数

作用

Destination

目的网络(子网/主机)

Gateway

网关地址,即匹配目的子网时的下一条地址,如果是 * 则是没有设置,匹配默认网关

Flags

所有可以用的标记包括:
  • U:up,路由状启用

  • H:host,目标网络是一个主机地址

  • G:gateway,用指定网关地址

  • R:reinstate,使用动态路由重新初始化的路由

  • D:dynamically,此路由是动态性地写入

  • M:modified,此路由是由路由守护程序或导向器动态修改

  • A:addrconf,通过地址配置增加的

  • C:cache,缓存的路由条目

  • !:reject,阻塞状态的路由条目

Metric

目的网络的度量值(可以理解为距离)

Ref

该路由的引用次数,不在linux内核使用这个参数

Use

此路由项被路由软件查找的次数,用参数 -F -C 查看这个值

Iface

网卡名字,例如 eth0

MSS

TCP链接最大的报文长度,单位是MB

Window

指定通过路由表的TCP连接的TCP窗口大小

irtt

初始往返时间(RTT),内核使用它来猜测最好的TCP协议参数

HH

引用缓存路由的硬件标头缓存的ARP条目和缓存路由的数量。如果缓存接口不需要硬件(lo接口),则这个值是 -1

Arp

缓存路由

108.5. 参考实例

108.5.1. 默认网关修改

 1[root@zzjlogin ~]# route
 2Kernel IP routing table
 3Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
 4192.168.6.0     *               255.255.255.0   U     0      0        0 eth1
 5192.168.161.0   *               255.255.255.0   U     0      0        0 eth0
 6link-local      *               255.255.0.0     U     1002   0        0 eth0
 7link-local      *               255.255.0.0     U     1003   0        0 eth1
 8default         192.168.161.2   0.0.0.0         UG    0      0        0 eth0
 9
10[root@zzjlogin ~]# route del default gw 192.168.161.2
11[root@zzjlogin ~]# route
12Kernel IP routing table
13Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
14192.168.6.0     *               255.255.255.0   U     0      0        0 eth1
15192.168.161.0   *               255.255.255.0   U     0      0        0 eth0
16link-local      *               255.255.0.0     U     1002   0        0 eth0
17link-local      *               255.255.0.0     U     1003   0        0 eth1
18[root@zzjlogin ~]# route add default gw 192.168.161.2
19[root@zzjlogin ~]# route
20Kernel IP routing table
21Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
22192.168.6.0     *               255.255.255.0   U     0      0        0 eth1
23192.168.161.0   *               255.255.255.0   U     0      0        0 eth0
24link-local      *               255.255.0.0     U     1002   0        0 eth0
25link-local      *               255.255.0.0     U     1003   0        0 eth1
26default         192.168.161.2   0.0.0.0         UG    0      0        0 eth0
27[root@zzjlogin ~]# route add default gw 192.168.6.2
28[root@zzjlogin ~]# route
29Kernel IP routing table
30Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
31192.168.6.0     *               255.255.255.0   U     0      0        0 eth1
32192.168.161.0   *               255.255.255.0   U     0      0        0 eth0
33link-local      *               255.255.0.0     U     1002   0        0 eth0
34link-local      *               255.255.0.0     U     1003   0        0 eth1
35default         192.168.6.2     0.0.0.0         UG    0      0        0 eth1
36default         192.168.161.2   0.0.0.0         UG    0      0        0 eth0

108.5.2. 普通路由操作

显示当前路由

简单显示(如果可以解析则自动解析路由条目):

小技巧

默认显示的是:操作内核的FIB(转发信息库)路由表。

1[root@zzjlogin ~]# route
2Kernel IP routing table
3Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
4192.168.6.0     *               255.255.255.0   U     0      0        0 eth1
5192.168.161.0   *               255.255.255.0   U     0      0        0 eth0
6link-local      *               255.255.0.0     U     1002   0        0 eth0
7link-local      *               255.255.0.0     U     1003   0        0 eth1
8default         192.168.161.2   0.0.0.0         UG    0      0        0 eth0

显示操作内核的FIB(转发信息库)路由表,并且不解析所有路由条目:

1[root@zzjlogin ~]# route -n
2Kernel IP routing table
3Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
4192.168.6.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
5192.168.161.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
6169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
7169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
80.0.0.0         192.168.161.2   0.0.0.0         UG    0      0        0 eth0

显示缓存路由信息:

 1[root@zzjlogin ~]# route -C
 2Kernel IP routing cache
 3Source          Destination     Gateway         Flags Metric Ref    Use Iface
 4192.168.161.137 192.168.161.2   192.168.161.2         0      0        5 eth0
 5192.168.161.1   192.168.161.137 192.168.161.137 il    0      0      143 lo
 6192.168.161.2   192.168.161.137 192.168.161.137 il    0      0        6 lo
 7192.168.161.137 192.168.161.2   192.168.161.2         0      0        5 eth0
 8192.168.161.137 192.168.161.254 192.168.161.254       0      0        0 eth0
 9192.168.161.254 192.168.161.137 192.168.161.137 il    0      0        0 lo
10192.168.161.137 192.168.161.1   192.168.161.1         0      1        0 eth0

显示更全的路由信息:

 1[root@zzjlogin ~]# route -n -ee
 2Kernel IP routing table
 3Destination     Gateway         Genmask         Flags Metric Ref    Use Iface    MSS   Window irtt
 4192.168.6.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1     0     0      0
 5192.168.161.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0     0     0      0
 6169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0     0     0      0
 7169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1     0     0      0
 80.0.0.0         192.168.161.2   0.0.0.0         UG    0      0        0 eth0     0     0      0
 9
10[root@zzjlogin ~]# route -C -ee
11Kernel IP routing cache
12Source          Destination     Gateway         Flags Metric Ref    Use Iface    MSS   Window irtt  TOS HHRef HHUptod     SpecDst
13192.168.161.137 192.168.161.1   192.168.161.1         0      1        0 eth0     1500  0      0     10  4     1   192.168.161.137
14192.168.161.1   192.168.161.137 192.168.161.137 il    0      0       70 lo       0     0      0     0   -1    0   192.168.161.137
15192.168.161.137 192.168.161.1   192.168.161.1         0      1        0 eth0     1500  0      0     10  4     1   192.168.161.137
16192.168.161.2   192.168.161.137 192.168.161.137 il    0      0        3 lo       0     0      0     0   -1    0   192.168.161.137
17192.168.161.1   192.168.161.137 192.168.161.137 il    0      0       71 lo       0     0      0     0   -1    0   192.168.161.137

用可变前缀添加/删除子网明细路由条目

添加路由条目:

 1[root@zzjlogin ~]# route -n
 2Kernel IP routing table
 3Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
 4192.168.6.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
 5192.168.161.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
 6169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
 7169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
 80.0.0.0         192.168.161.2   0.0.0.0         UG    0      0        0 eth0
 9[root@zzjlogin ~]# route add -net 10.8.0.0/16 gw 192.168.161.2
10[root@zzjlogin ~]# route -n
11Kernel IP routing table
12Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
13192.168.6.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
14192.168.161.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
1510.8.0.0        192.168.161.2   255.255.0.0     UG    0      0        0 eth0
16169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
17169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
180.0.0.0         192.168.161.2   0.0.0.0         UG    0      0        0 eth0

删除上面添加的路由条目:

1[root@zzjlogin ~]# route del -net 10.8.0.0/16 gw 192.168.161.2
2[root@zzjlogin ~]# route -n
3Kernel IP routing table
4Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
5192.168.6.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
6192.168.161.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
7169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
8169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
90.0.0.0         192.168.161.2   0.0.0.0         UG    0      0        0 eth0

添加管理网卡的路由条目

 1[root@zzjlogin ~]# route -F
 2Kernel IP routing table
 3Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
 4192.168.6.0     *               255.255.255.0   U     0      0        0 eth1
 5192.168.161.0   *               255.255.255.0   U     0      0        0 eth0
 6link-local      *               255.255.0.0     U     1002   0        0 eth0
 7link-local      *               255.255.0.0     U     1003   0        0 eth1
 8default         192.168.161.2   0.0.0.0         UG    0      0        0 eth0
 9[root@zzjlogin ~]# route add -net 10.8.0.0 netmask 255.255.0.0 dev eth1
10[root@zzjlogin ~]# route -F
11Kernel IP routing table
12Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
13192.168.6.0     *               255.255.255.0   U     0      0        0 eth1
14192.168.161.0   *               255.255.255.0   U     0      0        0 eth0
1510.8.0.0        *               255.255.0.0     U     0      0        0 eth1
16link-local      *               255.255.0.0     U     1002   0        0 eth0
17link-local      *               255.255.0.0     U     1003   0        0 eth1
18default         192.168.161.2   0.0.0.0         UG    0      0        0 eth0
19[root@zzjlogin ~]# route del -net 10.8.0.0 netmask 255.255.0.0 dev eth1
20[root@zzjlogin ~]# route
21Kernel IP routing table
22Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
23192.168.6.0     *               255.255.255.0   U     0      0        0 eth1
24192.168.161.0   *               255.255.255.0   U     0      0        0 eth0
25link-local      *               255.255.0.0     U     1002   0        0 eth0
26link-local      *               255.255.0.0     U     1003   0        0 eth1
27default         192.168.161.2   0.0.0.0         UG    0      0        0 eth0

屏蔽一条路由条目

 1[root@zzjlogin ~]# route
 2Kernel IP routing table
 3Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
 4192.168.6.0     *               255.255.255.0   U     0      0        0 eth1
 5192.168.161.0   *               255.255.255.0   U     0      0        0 eth0
 6link-local      *               255.255.0.0     U     1002   0        0 eth0
 7link-local      *               255.255.0.0     U     1003   0        0 eth1
 8default         192.168.161.2   0.0.0.0         UG    0      0        0 eth0
 9[root@zzjlogin ~]# route add -net 224.0.0.0 netmask 240.0.0.0 reject
10[root@zzjlogin ~]# route
11Kernel IP routing table
12Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
13192.168.6.0     *               255.255.255.0   U     0      0        0 eth1
14192.168.161.0   *               255.255.255.0   U     0      0        0 eth0
15link-local      *               255.255.0.0     U     1002   0        0 eth0
16link-local      *               255.255.0.0     U     1003   0        0 eth1
17224.0.0.0       -               240.0.0.0       !     0      -        0 -
18default         192.168.161.2   0.0.0.0         UG    0      0        0 eth0

108.6. 相关命令