东莞列举网 > 商务服务 > 网站/软件服务 > CentOS中route命令的解析
东莞
[切换城市]

CentOS中route命令的解析

更新时间:2019-06-12 09:28:29 浏览次数:79次
区域: 东莞 > 莞城
类别:IDC服务
地址:莞城区可园路福禧大厦
很多人对CentOS中的route命令都不是非常理解的,接下来是小编为大家介绍CentOS中route命令的解析,有需要的朋友们下面来跟着小编一起学习学习吧。

  介绍

  Linux系统中的route命令能够用于IP路由表的显示和操作。它的主要作用是创建一个静态路由让指定一个主机或者一个网络通过一个网络接口,如eth0。当使用”add”或者”del”参数时,路由表被修改,如果没有参数,则显示路由表当前的内容。在一个网络中,需要一个路由器来转发不同广播域之间的数据,或是转发lan和internet之间的数据。有时我们需要设定这个路由器作为linux系统的默认路由,那么就可以通过route命令来操作。甚至我们也可以用我们的linux系统来充当路由器。

  要注意的是:直接在命令行下执行route命令来添加路由,不会保存,当网卡重启或者机器重启之后,该路由就失效了;可以在/etc/rc.local中添加route命令来保证该路由设置有效。当然如果加上了-p参数的话那就会的生效了。

  命令格式

  route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway][metric Metric]] [if Interface]]

  命令参数

  ???? -c 显示更多信息

  ???? -n 不解析名字

  ???? -v 显示详细的处理信息

  ???? -F 显示发送信息

  ???? -C 显示路由缓存

  ???? -f 所有网关入口的路由表。

  ???? -p 与add 命令一起使用时使路由具有性。

  ???? add:添加一条新路由。

  ???? del:删除一条路由。

  ???? -net:目标地址是一个网络。

  ???? -host:目标地址是一个主机。

  ???? netmask:当添加一个网络路由时,需要使用网络掩码。

  ???? gw:路由数据网关。注意,你指定的网关必须能够达到。

  ???? metric:设置路由跳数。

  ????????? 1、Command 指定您想运行的命令 (Add/Change/Delete/Print)。

  ????????? 2、Destination 指定该路由的网络目标。

  ????????? 3、mask Netmask 指定与网络目标相关的网络掩码(也被称作子网掩码)。

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

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

  ????????? 6、if Interface为可以访问目标的接口指定接口索引。若要获得一个接口列表和它们相应的接口索引,使用 route print 命令的显示功能。可以使用十进制或十六进

  实例

  1 显示路由信息

  [root@localhost~]# route

  Kernel IP routing table

  Destination Gateway Genmask Flags Metric Ref Use Iface

  192.168.40.0 * 255.255.252.0 U 0 0 0 eth0

  169.254.0.0 * 255.255.0.0 U 0 0 0 eth0

  default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0

  Flags标志说明

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

  H Host,表示此网关为一主机

  G Gateway,表示此网关为一路由器

  R Reinstate Route,使用动态路由重新初始化的路由

  D Dynamically,此路由是动态性地写入–》什么时候才会有动态的路由信息呢?

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

  2 添加一条指向某个网络的路由

  [root@localhost~]# route add -net 10.0.0.0 netmask 255.255.255.0 dev eth0

  这里是指定这条路由的出口在哪里。-net 10.0.0.0 netmask 255.255.255.0 为指定目标网络的参数,需要ip地址或地址范围、子网掩码用于确定网络范围。

  [root@localhost~]# route

  Kernel IP routing table

  Destination Gateway Genmask Flags Metric Ref Use Iface

  10.0.0.0 * 255.255.255.0 U 0 0 0 eth0

  192.168.40.0 * 255.255.252.0 U 0 0 0 eth0

  169.254.0.0 * 255.255.0.0 U 0 0 0 eth0

  default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0

  route添加路由都是需要指定目标网络,及路由出口这两个参数。记住加上-p选项能添加。

  3 添加到某一个ip的路由

  [root@localhost~]# route add -host 192.168.40.1dev eth0

  [root@localhost ~]# route

  可以发现添加的是主机的话,默认是会帮我们添加一个全255的子网掩码,表示子网范围就只有一个而已,那就是这台主机啦。

  Kernel IP routing table

  Destination Gateway Genmask Flags Metric Ref Use Iface

  192.168.40.1 * 255.255.255.255 UH 0 0 0 eth0

  4 屏蔽某一路由

  当我们不让系统到达某个子网范围或者某个主机是就可以手动的来进行屏蔽。

  [root@localhost~]# route add -net 10.10.10.128 netmask 255.255.255.128 reject

  前面部分是一样的,因为我们都是手动来添加一个路由嘛。只是在命令的后不一样,我们指定的出口去而是reject(拒绝),也就是拒绝出口。达到屏蔽的效果。还有看下flags会显示一个!

  [root@localhost~]# route

  Kernel IP routing table

  Destination Gateway Genmask Flags Metric Ref Use Iface

  192.168.40.1 * 255.255.255.255 UH 0 0 0 eth0

  10.10.10.128 - 255.255.255.128 ! 0 - 0 -

  10.0.0.0 * 255.255.255.0 U 0 0 0 eth0

  192.168.40.0 * 255.255.252.0 U 0 0 0 eth0

  169.254.0.0 * 255.255.0.0 U 0 0 0 eth0

  default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0

  5 删除路由

  [root@localhost~]# route

  Kernel IP routing table

  Destination Gateway Genmask Flags Metric Ref Use Iface

  192.168.40.1 * 255.255.255.255UH 0 0 0 eth0

  10.10.10.128 - 255.255.255.128 ! 0 - 0 -

  10.0.0.0 * 255.255.255.0 U 0 0 0 eth0

  192.168.40.0 * 255.255.252.0 U 0 0 0 eth0

  169.254.0.0 * 255.255.0.0 U 0 0 0 eth0

  default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0

  [root@localhost~]# route del -net 10.10.10.128netmask 255.255.255.128 reject

  [root@localhost~]# route

  Kernel IP routing table

  Destination Gateway Genmask Flags Metric Ref Use Iface

  192.168.40.1 * 255.255.255.255UH 0 0 0 eth0

  10.0.0.0 * 255.255.255.0 U 0 0 0 eth0

  192.168.40.0 * 255.255.252.0 U 0 0 0 eth0

  169.254.0.0 * 255.255.0.0 U 0 0 0 eth0

  default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0

  说明:删除路由时,是看着路由表上的照样打进去,这样比较不会删错的。

  添加删除默认网关

  [root@localhost ~]# route add default gw 192.168.40.2

  [root@localhost~]# route

  Kernel IP routing table

  Destination Gateway Genmask Flags Metric Ref Use Iface

  192.168.40.1 * 255.255.255.255UH 0 0 0 eth0

  10.0.0.0 * 255.255.255.0 U 0 0 0 eth0

  192.168.40.0 * 255.255.252.0 U 0 0 0 eth0

  169.254.0.0 * 255.255.0.0 U 0 0 0 eth0

  default 192.168.40.2 0.0.0.0 UG 0 0 0 eth0

  default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0

  可以看到此处有两个默认网关,那到底路由会走哪个呢?

  [root@localhost~]# route del default gw192.168.40.2

  [root@localhost~]# route

  Kernel IP routing table

  Destination Gateway Genmask Flags Metric Ref Use Iface

  192.168.40.1 * 255.255.255.255UH 0 0 0 eth0

  10.0.0.0 * 255.255.255.0 U 0 0 0 eth0

  192.168.40.0 * 255.255.252.0 U 0 0 0 eth0

  169.254.0.0 * 255.255.0.0 U 0 0 0 eth0

  default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0

更多咨询Q Q:549844935或594380336
www.***
东莞网站/软件服务相关信息
注册时间:2019年05月14日
UID:599849
---------- 认证信息 ----------
手机已认证
查看用户主页