深入解析Linux 常用命令--route

本文深入解析了Linux系统中的路由管理,包括route命令的基本使用方法、显示数字地址、查看内核路由表缓存等内容,并详细介绍了如何添加和删除主机路由、网络路由及默认路由。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

深入解析Linux常用命令–route

1.概述

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

2.路由类型

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

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

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

3.命令

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

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

[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

[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

[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


2017/12/29

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值