详解CentOS中的route命令

发布时间:2022-04-22 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了详解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:删除一条路由。

     @R_126_2024@:目标地址是一个网络。

     -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标志说明

  1. U Up表示此路由当前为启动状态
  2. H Host,表示此网关为一主机
  3. G Gateway,表示此网关为一路由器
  4. R Reinstate Route,使用动态路由重新初始化的路由
  5. D Dynamically,此路由是动态性地写入–》什么时候才会有动态的路由信息呢?
  6. 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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能有所帮助,如果有疑问大家可以留言交流。

脚本宝典总结

以上是脚本宝典为你收集整理的详解CentOS中的route命令全部内容,希望文章能够帮你解决详解CentOS中的route命令所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:centos7route