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

命令:

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

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

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 ~]# 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

[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 ~]# 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

说明:

增加一条屏蔽的路由,目的地址为 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

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

[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:删除和添加设置默认网关

命令:

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

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 ~]# 

Linux指令--route的更多相关文章

  1. linux 指令备忘

    linux 指令备忘 1.ls [选项] [目录名 | 列出相关目录下的所有目录和文件 -a 列出包括.a开头的隐藏文件的所有文件 -A 通-a,但不列出"."和"..& ...

  2. linux下route命令--说的比较清楚!

    linux下route命令     route命令感觉很不容易.一般开机后在命令行中使用route命令,会得到下面的信息   Kernel IP routing table   Destination ...

  3. Linux 指令大全

    作为一个小前端,以前有我们的运维大神在的时候,要给服务器做什么配置的时候就找他(那时幸福到哭),如今他走了,公司也没招人(想把这个钱省下来,让我发现了,毕竟我能当小运维用,虽然很这方面很渣渣,哈哈,偷 ...

  4. Linux 指令。

    从16年11月21号开始吧,加班变得特别频繁,基本上一周加5天,周六也会加,下班也很晚,一般都是10点9点,回家之后很疲惫,已经很久没有给自己充过电了,自己的学习计划和健身计划也打乱了,对工作的压力也 ...

  5. 04 Linux 指令语法结构与帮助命令

    一.Linux指令语法结构 [tyang3@localhost Desktop]$ command [-options] [arguments] 指令           选项           参 ...

  6. linux指令大全(完整篇)(转)

       http://blog.chinaunix.net/uid-9681606-id-1998590.html  linux指令大全(完整篇)(转) 2009-03-17 01:21:46 分类:  ...

  7. Linux指令范例速查手册

    linux命令繁多,命令就是AK的子弹,对上口径,百发百中! 无意发现一本介绍Linux命令的手册--->[Linux指令范例速查手册] 下载: https://pan.baidu.com/s/ ...

  8. Linux 指令篇:磁盘管理--tree

    Linux 指令篇:磁盘管理--tree 功能说明:以树状图列出目录的内容. 语 法:tree [-aACdDfFgilnNpqstux][-I <范本样式>][-P <范本样式&g ...

  9. Linux指令od和hexdump

    Linux指令:od (octal dump) 示例用法:od -c hello Linux指令:od od命令用户通常使用od命令查看特殊格式的文件内容.通过指定该命令的不同选项可以以十进制.八进制 ...

随机推荐

  1. Hyperledger Fabric 本地运行的简单示例

    环境: Ubuntu 16.04 go 1.7.4 版本: Fabric v1.0.0-alpha 本文主要目的就是让大家体验以下Fabric网络环境搭建的具体过程,不基于集成化脚本手动搭建. 一.编 ...

  2. BIOS 品牌快捷键

    主板品牌 启动按键 笔记本品牌 启动按键 台式机品牌 启动按键 华硕主板 F8 联想笔记本 F12 联想台式机 F12 技嘉主板 F12 宏基笔记本 F12 惠普台式机 F12 微星主板 F11 华硕 ...

  3. SQL Server-聚焦WHERE Column=@Param OR @Param IS NULL有问题?

    前言 上一篇我们讲完SQL动态查询,本节我们继续来讲解SQL动态查询中存在的问题. SQL动态查询条件筛选过滤 当我们创建存储过程调用存储过程时,若筛选条件有值则过滤,没有值则返回所行记录,类似如下查 ...

  4. CSS(五)圣杯,双飞翼布局

    双飞翼布局 <style> *{ margin:; padding:; } .main{ width: 100%; height: 200px; background: pink; flo ...

  5. sync_binlog innodb_flush_log_at_trx_commit 浅析

    一 参数意义 innodb_flush_log_at_trx_commit 如果innodb_flush_log_at_trx_commit设置为0,log buffer将每秒一次地写入log fil ...

  6. Excel生成guid、uuid

    1.Excel生成guid,uuid  格式:600d65bc-948a-1260-2217-fd8dfeebb1cd =LOWER(CONCATENATE(DEC2HEX(RANDBETWEEN(, ...

  7. TLD算法概述--学习理解之(一)

    liuyihai@126.com http://www.cnblogs.com/liuyihai/ TLD(Tracking-Learning-Detection)是英国萨里大学的一个捷克籍博士生Zd ...

  8. http常见状态码含义

    200:请求成功 301:请求的资源已永久移动到新位置 302:请求的资源临时移动到新位置 304:请求内容无改变 401:未授权 403:禁止访问 404:文件未找到 500:服务器内部错误 501 ...

  9. JavaScript练习2

    今天做了一些JS数组的练习题 一.往数组中插入一个数字 var attr = [1,2,3,4,5,6]; var c = 7; for(var i=0;i<attr.length;i++) { ...

  10. Java与算法之(9) - 直接插入排序

    直接插入排序是最简单的排序算法,也比较符合人的思维习惯.想像一下玩扑克牌抓牌的过程.第一张抓到5,放在手里:第二张抓到3,习惯性的会把它放在5的前面:第三张抓到7,放在5的后面:第四张抓到4,那么我们 ...