1.概述

route用于查看路由表、增加路由、删除路由、修改路由表等操作。

2.路由类型

a.主机路由:表示指向单个IP地址或主机名的路由

b.网络路由:表示指向某个网路的路由

c.默认路由:表示不能再路由表中找到的目标主机或网路的路由

3.命令

a.显示数字地址(不解析主机名)

route -n,该命令经常使用,由于直接输入route主机会去接析主机名,有时候会耗用较长时间。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@smart Desktop]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 * 255.255.255.0 U 1 0 0 eth0
192.168.42.0 * 255.255.255.0 U 1 0 0 eth1
192.168.122.0 * 255.255.255.0 U 0 0 0 virbr0
default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
[root@smart Desktop]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0
192.168.42.0 0.0.0.0 255.255.255.0 U 1 0 0 eth1
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0

输出项说明:

输出项 含义
Destination 目标网段或者主机
Gateway 网关地址,”*” 表示目标是本主机所属的网络,不需要路由
Genmask 网络掩码
Flags 标记。一些可能的标记如下:
U-路由是活动的
H-目标是一个主机
G-路由指向网关
R- 恢复动态路由产生的表项
D-由路由的后台程序动态地安装
M-由路由的后台程序修改
!-拒绝路由
Metric 路由距离,到达指定网络所需的中转数
Ref 路由项引用次数
Use 此路由项被路由软件查找的次数
Iface 该路由表项对应的输出接口

b.查看内核路由表缓存

-F

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@smart Desktop]# route -C
Kernel IP routing cache
Source Destination Gateway Flags Metric Ref Use Iface
192.168.1.108 ns.szptt.net.cn 192.168.1.1 0 0 64 eth0
192.168.1.108 14.215.177.39 192.168.1.1 0 0 181 eth0
192.168.1.108 14.215.177.38 192.168.1.1 0 0 1 eth0
192.168.1.108 14.215.177.39 192.168.1.1 0 0 181 eth0
192.168.1.108 ns.szptt.net.cn 192.168.1.1 0 0 72 eth0
192.168.1.1 192.168.1.108 192.168.1.108 il 0 0 20 lo
192.168.1.108 a184-25-56-157. 192.168.1.1 0 1 0 eth0
10.9.0.1 192.168.1.108 192.168.1.108 l 0 0 5 lo
10.9.0.1 192.168.1.108 192.168.1.108 l 0 0 2 lo
a184-25-56-157. 192.168.1.108 192.168.1.108 l 0 1 0 lo
ns.szptt.net.cn 192.168.1.108 192.168.1.108 l 0 0 18 lo
192.168.1.108 14.215.177.38 192.168.1.1 0 0 1 eth0
[root@smart Desktop]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0
192.168.42.0 0.0.0.0 255.255.255.0 U 1 0 0 eth1
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0

4.路由配置

a.添加路由

1)添加主机路由

route add -host 192.168.1.100 dev eth0

route add -host 192.168.2.100 gw 192.168.1.1

2)添加网络路由

route add -net 172.168.1.0 netmask 255.255.255.0 eth0

route add -net 172.168.2.0/24 eth0

route add -net 172.168.3.0/24 dev eth0

route add -net 172.168.4.0/24 dev eth0 gw 192.168.1.1

route add -net 172.168.5.0/24 gw 192.168.1.1

3)添加默认路由

route add default gw 192.168.1.7

route add 0.0.0.0 gw 172.168.1.1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
[root@smart Desktop]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.168.1.1 255.255.255.255 UGH 0 0 0 eth1
192.168.2.100 0.0.0.0 255.255.255.255 UH 0 0 0 eth0
192.168.2.101 192.168.1.1 255.255.255.255 UGH 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0
172.168.1.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth0
172.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
172.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
172.168.3.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
172.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
172.168.5.0 192.168.1.7 255.255.255.0 UG 0 0 0 eth0
172.168.5.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth0
192.168.42.0 0.0.0.0 255.255.255.0 U 1 0 0 eth1
172.168.4.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth0
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
0.0.0.0 192.168.42.1 0.0.0.0 UG 0 0 0 eth1
0.0.0.0 192.168.42.7 0.0.0.0 UG 0 0 0 eth1
0.0.0.0 192.168.1.7 0.0.0.0 UG 0 0 0 eth0
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
[root@smart Desktop]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 172.168.1.1 255.255.255.255 UGH 0 0 0 eth1
192.168.2.100 * 255.255.255.255 UH 0 0 0 eth0
192.168.2.101 192.168.1.1 255.255.255.255 UGH 0 0 0 eth0
192.168.1.0 * 255.255.255.0 U 1 0 0 eth0
172.168.1.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth0
172.168.1.0 * 255.255.255.0 U 0 0 0 eth1
172.168.1.0 * 255.255.255.0 U 0 0 0 eth0
172.168.3.0 * 255.255.255.0 U 0 0 0 eth0
172.168.2.0 * 255.255.255.0 U 0 0 0 eth0
172.168.5.0 192.168.1.7 255.255.255.0 UG 0 0 0 eth0
172.168.5.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth0
192.168.42.0 * 255.255.255.0 U 1 0 0 eth1
172.168.4.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth0
192.168.122.0 * 255.255.255.0 U 0 0 0 virbr0
default 192.168.42.1 0.0.0.0 UG 0 0 0 eth1
default 192.168.42.7 0.0.0.0 UG 0 0 0 eth1
default 192.168.1.7 0.0.0.0 UG 0 0 0 eth0
default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0

b.删除路由

删除与添加相反,只需将add换为del即可,另外删除路由可以不添加网关和设备标识

1)删除主机路由

route del -host 192.168.1.100 dev eth0

route del -host 192.168.2.100 gw 192.168.1.1

route del -host 192.168.1.100

route del -host 192.168.2.100

2)添加网络路由

route del -net 172.168.1.0 netmask 255.255.255.0 eth0

route del -net 172.168.2.0/24 eth0

route del -net 172.168.3.0/24 dev eth0

route del -net 172.168.4.0/24 dev eth0 gw 192.168.1.1

route del -net 172.168.5.0/24 gw 192.168.1.1

3)添加默认路由

route del default gw 192.168.1.7

route del 0.0.0.0 gw 172.168.1.1