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 显示更多信息

#p#分页标题#e#-n 不解析名字

-v 显示详细的处理信息

-F 显示发送信息

-C 显示路由缓存

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

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

add:添加一条新路由。

del:删除一条路由。

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

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

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

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

#p#分页标题#e#metric:设置路由跳数。

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

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

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

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

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

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

4.使用实例:

实例1:显示当前路由

#p#分页标题#e#命令:

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#p#分页标题#e#
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为路由标志,标记当前网络节点的状态。

#p#分页标题#e#Flags标志说明:

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

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

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

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

D Dynamically,此路由是动态性地写入

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

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

备注:

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

实例2:添加网关/设置网关#p#分页标题#e#

命令:

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
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 #p#分页标题#e#

[root@localhost ~]#

说明:

增加一条 到达244.0.0.0的路由

实例3:屏蔽一条路由

命令:

route add -net 224.0.0.0 netmask 240.0.0.0 reject

输出:

[root@localhost ~]##p#分页标题#e# 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

说明:

增加一条屏蔽的路由,#p#分页标题#e#目的地址为 224.x.x.x 将被拒绝

实例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#p#分页标题#e#
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#p#分页标题#e#
[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:删除和添加设置默认网关

命令:

#p#分页标题#e#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#p#分页标题#e#
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 ~]#

转载于:http://www.itxuexiwang.com/a/liunxjishu/2016/0304/214.html?1457194241

每天一个linux命令(53):route命令的更多相关文章

  1. 每天一个linux命令:route命令

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

  2. 每天一个linux命令(40)--route命令

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

  3. linux 命令——53 route(转)

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

  4. linux常用命令:route 命令

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

  5. 每天一个linux命令(53)--ps命令

    要毁掉一天,从早上开始. Linux中的ps命令是 process status 的缩写.ps 命令用来列出系统中当前运行的那些进程.ps 命令列出的是当前那些进程的快照,就是执行ps 命令的那个时刻 ...

  6. 关于Linux路由表的route命令(转)

    查看 Linux 内核路由表 使用下面的 route 命令可以查看 Linux 内核路由表. # route Destination  Gateway      Genmask          Fl ...

  7. Linux路由表信息-route命令

    使用命令 :route route 命令    显示和设置Linux路由表 -A:设置地址类型: -C:打印将Linux核心的路由缓存: -v:详细信息模式: -n:不执行DNS反向查找,直接显示数字 ...

  8. 关于Linux路由表的route命令

    转自:http://www.cnblogs.com/gunl/archive/2010/09/14/1826234.html 查看 Linux 内核路由表 使用下面的 route 命令可以查看 Lin ...

  9. 【Linux常见命令】route命令

    route - show / manipulate the IP routing table route命令用于显示和操作IP路由表. route命令用来显示并设置Linux内核中的网络路由表,rou ...

  10. linux下的route命令

    语法: route [-CFvnee] route [add|del]  [-net|-host] [网络或主机] netmask [gw|dev] route  [-V] [--version] [ ...

随机推荐

  1. 简单跳转到微信分享,基于libWeiChatSDK 和简单的自定义UIActivityViewController

    一.自定义UIActivity: 如果想要自定义UIActivity必须知道UIActivityViewController.首先这个类主要是用于接受字符串,RUL类型和图片类型的数据用于分享和操作的 ...

  2. Java 获取 Unix时间戳

    unix时间戳是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒. 在大多数的UNIX系统中UNIX时间戳存储为32位,这样会引发2038年问题. 但是,因为需求是需要int类 ...

  3. ASP.NET页面之间传递值的几种方式

    目录 QueryString Session Cookie Application 一.QueryString QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中. ...

  4. 七个高效的文本编辑习惯(以Vim为例)

    七个高效的文本编辑习惯 如果你花很多时间输入纯文本.写程序或HTML,那么通过高效地使用一个好的编辑器,你可以节省大部分时间.本文将提供指导和提示,让你更迅速地做这些工作,并且少犯错误. 本文用开源文 ...

  5. UI Automation Test

    UI Automation test is based on the windows API. U can find the UI Automation MSDN file from http://m ...

  6. redis sentinel基本命令与参数

    1.redis基本命令1)获取sentinel的状态(1)info查看sentinel的状态(2)sentinel masters 获取sentinel中监控的所有master的节点(3)sentin ...

  7. flask_关注者,联系人和好友

    在这节我们实现的功能比较复杂,就是实现用户"关注"和"取消关注"的功能. 一个用户可以关注多个其他的用户,一个用户也可以被其他多个用户所关注,这样看的话,在数据 ...

  8. 【线段树】bzoj3995 [SDOI2015]道路修建

    线段树每个结点维护5个域: 整个区间的MST. 将两个左端点连通,两个右端点不连通,整个区间内选择2*(r-l+1)-2条边的最小生成森林,有两个连通块. 将两个右端点连通,两个左端点不连通,整个区间 ...

  9. WPF Touch操作滚动条,Window弹跳

    WPF,用ScrollViewer控件,触屏开发,当滑动到最后时会使整个窗体弹跳一下 原因是因为ScrollViewer触屏操作原生支持惯性,ScrollViewer中的内容滚动到边界是会自动触发Wi ...

  10. Core Audio(一)

    Core Audio APIs core audio apis是vista之后引入的,不使用与之前的windows版本:core audio apis提供访问endpoint devices,比如耳机 ...