科技常识:Linux route命令详解和使用示例(查看和操作IP路由表)

2021-06-09 18:59:05
导读 今天小编跟大家讲解下有关科技常识:Linux route命令详解和使用示例(查看和操作IP路由表),相信小伙伴们对这个话题应该也很关注吧,小编

今天小编跟大家讲解下有关科技常识:Linux route命令详解和使用示例(查看和操作IP路由表),相信小伙伴们对这个话题应该也很关注吧,小编也收集到了有关科技常识:Linux route命令详解和使用示例(查看和操作IP路由表)的相关资料,希望小伙伴会喜欢也能够帮助大家。

在Linux系统中,设置路由通常是为了解决以下问题:该Linux系统在一个局域网中,局域网中有一个网关,能够让机器访问Internet,那么就需要将这台机器的IP地址设置为Linux机器的默认路由。要注意的是,直接在命令行下执行route命令来添加路由,不会永久保存,当网卡重启或者机器重启之后,该路由就失效了;可以在/etc/rc.local中添加route命令来保证该路由设置永久有效。

1.命令格式:

route[-f][-p][Command[Destination][maskNetmask][Gateway][metricMetric]][ifInterface]]

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指定该路由的网络目标。

maskNetmask指定与网络目标相关的网络掩码(也被称作子网掩码)。

Gateway指定网络目标定义的地址集和子网掩码可以到达的前进或下一跃点IP地址。

metricMetric为路由指定一个整数成本值标(从1至9999),当在路由表(与转发的数据包目标地址最匹配)的多个路由中进行选择时可以使用。

ifInterface为可以访问目标的接口指定接口索引。若要获得一个接口列表和它们相应的接口索引,使用routeprint命令的显示功能。可以使用十进制或十六进制值进行接口索引。

4.使用实例:

实例1:显示当前路由

命令:复制代码代码如下:routeroute -n

输出:

复制代码代码如下:[root@localhost ~]# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 0 0 0 eth0e192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0[root@localhost ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth00.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标志说明:

UUp表示此路由当前为启动状态

HHost,表示此网关为一主机

GGateway,表示此网关为一路由器

RReinstateRoute,使用动态路由重新初始化的路由

DDynamically,此路由是动态性地写入

MModified,此路由是由路由守护程序或导向器动态修改

!表示此路由当前为关闭状态

备注:

route-n(-n表示不解析名字,列出速度会比route快)

实例2:添加网关/设置网关

命令: routeadd-net224.0.0.0netmask240.0.0.0deveth0

输出:

复制代码代码如下:[root@localhost ~]# route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0[root@localhost ~]# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 0 0 0 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0224.0.0.0 * 240.0.0.0 U 0 0 0 eth0default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0

说明:

增加一条到达244.0.0.0的路由

实例3:屏蔽一条路由

命令:routeadd-net224.0.0.0netmask240.0.0.0reject

输出:

复制代码代码如下:[root@localhost ~]# route add -net 224.0.0.0 netmask 240.0.0.0 reject[root@localhost ~]# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 0 0 0 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0224.0.0.0 - 240.0.0.0 ! 0 - 0 -224.0.0.0 * 240.0.0.0 U 0 0 0 eth0default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0

说明:

增加一条屏蔽的路由,目的地址为224.x.x.x将被拒绝

实例4:删除路由记录

命令:

复制代码代码如下:route del -net 224.0.0.0 netmask 240.0.0.0route del -net 224.0.0.0 netmask 240.0.0.0 reject

输出:

复制代码代码如下:[root@localhost ~]# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 0 0 0 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0224.0.0.0 - 240.0.0.0 ! 0 - 0 -224.0.0.0 * 240.0.0.0 U 0 0 0 eth0default 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 ~]# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 0 0 0 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0224.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 ~]# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 0 0 0 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0[root@localhost ~]#

说明:

实例5:删除和添加设置默认网关

命令:

复制代码代码如下:route del default gw 192.168.120.240route add default gw 192.168.120.240

输出:

复制代码代码如下:[root@localhost ~]# route del default gw 192.168.120.240[root@localhost ~]# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 0 0 0 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.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 ~]# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 0 0 0 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0[root@localhost ~]#

来源:爱蒂网

免责声明:本文由用户上传,如有侵权请联系删除!

猜你喜欢

最新文章