Linuxroute命令详解和使⽤⽰例(查看和操作IP路由表)
Linux系统的route命令⽤于显⽰和操作IP路由表(show / manipulate the IP routing table)。要实现两个不同的⼦⽹之间的通信,需要⼀台连接两个⽹络的路由器,或者同时位于两个⽹络的⽹关来实现
在Linux系统中,设置路由通常是为了解决以下问题:该Linux系统在⼀个局域⽹中,局域⽹中有⼀个⽹关,能够让机器访问Internet,那么就需要将这台机器的IP地址设置为Linux机器的默认路由。要注意的是,直接在命令⾏下执⾏route命令来添加路由,不会永久保存,当⽹卡重启或者机器重启之后,该路由就失效了;可以在/etc/rc.local中添加route命令来保证该路由设置永久有效。
1.命令格式:
route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]]
2.命令功能:
Route命令是⽤于操作基于内核ip路由表,它的主要作⽤是创建⼀个静态路由让指定⼀个主机或者⼀个⽹络通过⼀个⽹络接⼝,如eth0。当使⽤"add"或者"del"参数时,路由表被修改,如果没有参数,则显⽰路由表当前的内容。
3.命令参数:
-c 显⽰更多信息
-n 不解析名字
-v 显⽰详细的处理信息
-F 显⽰发送信息
-C 显⽰路由缓存
-f 清除所有⽹关⼊⼝的路由表。
-p 与 add 命令⼀起使⽤时使路由具有永久性。
add:添加⼀条新路由。
del:删除⼀条路由。
-net:⽬标地址是⼀个⽹络。
-host:⽬标地址是⼀个主机。
netmask:当添加⼀个⽹络路由时,需要使⽤⽹络掩码。
gw:路由数据包通过⽹关。注意,你指定的⽹关必须能够达到。
metric:设置路由跳数。
Command 指定您想运⾏的命令 (Add/Change/Delete/Print)。
Destination 指定该路由的⽹络⽬标。
mask Netmask 指定与⽹络⽬标相关的⽹络掩码(也被称作⼦⽹掩码)。
Gateway 指定⽹络⽬标定义的地址集和⼦⽹掩码可以到达的前进或下⼀跃点 IP 地址。
metric Metric 为路由指定⼀个整数成本值标(从 1 ⾄ 9999),当在路由表(与转发的数据包⽬标地址最匹配)的多个路由中进⾏选择时可以使⽤。
if Interface 为可以访问⽬标的接⼝指定接⼝索引。若要获得⼀个接⼝列表和它们相应的接⼝索引,使⽤ route print 命令的显⽰功能。可以使⽤⼗进制或⼗六进制值进⾏接⼝索引。
4.使⽤实例:
实例1:显⽰当前路由
命令:
复制代码
代码如下:
route
route -n
输出:
复制代码
代码如下:
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
e192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.120.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
0.0.0.0 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
说明:
第⼀⾏表⽰主机所在⽹络的地址为192.168.120.0,若数据传送⽬标是在本局域⽹内通信,则可直接通过eth0转发数据包;第四⾏表⽰数据传送⽬的是访问Internet,则由接⼝eth0,将数据包发送到⽹关192.168.120.240
其中Flags为路由标志,标记当前⽹络节点的状态。
Flags标志说明:
U Up表⽰此路由当前为启动状态
H Host,表⽰此⽹关为⼀主机
G Gateway,表⽰此⽹关为⼀路由器
R Reinstate Route,使⽤动态路由重新初始化的路由
D Dynamically,此路由是动态性地写⼊
M Modified,此路由是由路由守护程序或导向器动态修改
! 表⽰此路由当前为关闭状态
备注:
route -n (-n 表⽰不解析名字,列出速度会⽐route 快)
实例2:添加⽹关/设置⽹关
命令: route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
输出:
复制代码
代码如下:
[root@localhost ~]# route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
[root@localhost ~]# route
route add 添加路由
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
224.0.0.0 * 240.0.0.0 U 0 0 0 eth0
default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
说明:
增加⼀条到达244.0.0.0的路由
实例3:屏蔽⼀条路由
命令:route add -net 224.0.0.0 netmask 240.0.0.0 reject
输出:
复制代码
代码如下:
[root@localhost ~]# route add -net 224.0.0.0 netmask 240.0.0.0 reject [root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
224.0.0.0 - 240.0.0.0 ! 0 - 0 -
224.0.0.0 * 240.0.0.0 U 0 0 0 eth0
default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
说明:
增加⼀条屏蔽的路由,⽬的地址为 将被拒绝
实例4:删除路由记录
命令:
复制代码
代码如下:
route del -net 224.0.0.0 netmask 240.0.0.0
route del -net 224.0.0.0 netmask 240.0.0.0 reject
输出:
复制代码
代码如下:
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
224.0.0.0 - 240.0.0.0 ! 0 - 0 -
224.0.0.0 * 240.0.0.0 U 0 0 0 eth0
default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
[root@localhost ~]# route del -net 224.0.0.0 netmask 240.0.0.0
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
224.0.0.0 - 240.0.0.0 ! 0 - 0 -
default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
[root@localhost ~]# route del -net 224.0.0.0 netmask 240.0.0.0 reject [root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
[root@localhost ~]#
说明:
实例5:删除和添加设置默认⽹关
命令:
复制代码
代码如下:
route del default gw 192.168.120.240
route add default gw 192.168.120.240
输出:
复制代码
代码如下:
[root@localhost ~]# route del default gw 192.168.120.240
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
[root@localhost ~]# route add default gw 192.168.120.240
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
[root@localhost ~]#