您的位置首页  散文随感

全程干货route add(route add mask 命令详解)

Linux系统的route命令用于显示和操作IP路由表(show / manipulate the IP routing table)。要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现。

全程干货route add(route add mask 命令详解)

 

(点击上方公众号,可快速关注)来源:peidahttp://www.cnblogs.com/peida/archive/2013/03/05/2943698.html如有好文章投稿,请点击 → 这里了解详情

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:显示当前路由

命令:routeroute -n输出:[root@localhost ~]# routeKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.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 eth0

10.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 Iface

192.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 eth0

10.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标志说明: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 ~]# routeKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.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 eth0

10.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 eth0

default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0[root@localhost ~]#  说明:增加一条 到达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 ~]# routeKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.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 eth0

10.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 Iface

192.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 eth0

10.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 table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface192.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 eth010.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 ~]# routeKernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface192.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 eth010.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.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 Iface

192.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 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 ~]# routeKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.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 eth0

10.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 ~]# 本系列文章:每天一个 Linux 命令(1):ls命令每天一个 Linux 命令(2):cd命令每天一个 Linux 命令(3):pwd命令每天一个 Linux 命令(4):mkdir命令

每天一个 Linux 命令(5):rm 命令每天一个 Linux 命令(6):rmdir 命令每天一个 Linux 命令(7):mv命令每天一个 Linux 命令(8):cp 命令每天一个 Linux 命令(9):touch 命令

每天一个 Linux 命令(10):cat 命令每天一个 Linux 命令(11):nl命令每天一个 Linux 命令(12):more命令每天一个 Linux 命令(13):less 命令每天一个 Linux 命令(14):head 命令

每天一个 Linux 命令(15):tail 命令每天一个 Linux 命令(16):which命令每天一个 Linux 命令(17):whereis 命令每天一个 Linux 命令(18):locate 命令

每天一个 Linux 命令(19):find 命令概览每天一个 Linux 命令(20):find命令之exec每天一个 Linux 命令(21):find命令之xargs每天一个 Linux 命令(22):find 命令的参数详解

每天一个 Linux 命令(23):Linux 目录结构每天一个 Linux 命令(24):Linux 文件类型与扩展名每天一个 Linux 命令(25):Linux 文件属性详解每天一个 Linux 命令(26):用 SecureCRT 来上传和下载文件

每天一个 Linux 命令(27):chmod 命令每天一个 Linux 命令(28):tar 命令每天一个 Linux 命令(29):chgrp命令每天一个 Linux 命令(30):chown命令每天一个 Linux 命令(31):/etc/group文件详解

每天一个 Linux 命令(32):gzip命令每天一个 Linux 命令(33):df 命令每天一个 Linux 命令(34):du 命令每天一个 Linux 命令(35):ln 命令每天一个 Linux 命令(36):diff 命令

每天一个 Linux 命令(37):date命令每天一个 Linux 命令(38):cal 命令每天一个 Linux 命令(39):grep 命令每天一个 Linux 命令(40):wc命令每天一个 Linux 命令(41):ps命令

每天一个 Linux 命令(42):kill命令每天一个 Linux 命令(43):killall 命令每天一个 Linux 命令(44):top命令每天一个 Linux 命令(45):free 命令每天一个 Linux 命令(46):vmstat命令

每天一个 Linux 命令(47):iostat命令每天一个 Linux 命令(48):watch命令每天一个 Linux 命令(49):at命令每天一个 Linux 命令(50):crontab命令每天一个 Linux 命令(51):lsof命令

每天一个 Linux 命令(52):ifconfig命令看完本文有收获?请分享给更多人关注「Linux 爱好者」,提升Linux技能

免责声明:本站所有信息均搜集自互联网,并不代表本站观点,本站不对其真实合法性负责。如有信息侵犯了您的权益,请告知,本站将立刻处理。联系QQ:1640731186