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

输出:

  1.  
    [root@localhost ~]# route
  2.  
    Kernel IP routing table
  3.  
    Destination Gateway Genmask Flags Metric Ref Use Iface
  4.  
    192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
  5.  
    e192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
  6.  
    10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
  7.  
    default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
  8.  
    [root@localhost ~]# route -n
  9.  
    Kernel IP routing table
  10.  
    Destination Gateway Genmask Flags Metric Ref Use Iface
  11.  
    192.168.120.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
  12.  
    192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
  13.  
    10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
  14.  
    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

输出:

  1.  
    [root@localhost ~]# route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
  2.  
    [root@localhost ~]# route
  3.  
    Kernel IP routing table
  4.  
    Destination Gateway Genmask Flags Metric Ref Use Iface
  5.  
    192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
  6.  
    192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
  7.  
    10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
  8.  
    224.0.0.0 * 240.0.0.0 U 0 0 0 eth0
  9.  
    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

输出:

  1.  
    [root@localhost ~]# route add -net 224.0.0.0 netmask 240.0.0.0 reject
  2.  
    [root@localhost ~]# route
  3.  
    Kernel IP routing table
  4.  
    Destination Gateway Genmask Flags Metric Ref Use Iface
  5.  
    192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
  6.  
    192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
  7.  
    10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
  8.  
    224.0.0.0 - 240.0.0.0 ! 0 - 0 -
  9.  
    224.0.0.0 * 240.0.0.0 U 0 0 0 eth0
  10.  
    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

输出:

  1.  
    [root@localhost ~]# route
  2.  
    Kernel IP routing table
  3.  
    Destination Gateway Genmask Flags Metric Ref Use Iface
  4.  
    192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
  5.  
    192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
  6.  
    10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
  7.  
    224.0.0.0 - 240.0.0.0 ! 0 - 0 -
  8.  
    224.0.0.0 * 240.0.0.0 U 0 0 0 eth0
  9.  
    default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
  10.  
    [root@localhost ~]# route del -net 224.0.0.0 netmask 240.0.0.0
  11.  
    [root@localhost ~]# route
  12.  
    Kernel IP routing table
  13.  
    Destination Gateway Genmask Flags Metric Ref Use Iface
  14.  
    192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
  15.  
    192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
  16.  
    10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
  17.  
    224.0.0.0 - 240.0.0.0 ! 0 - 0 -
  18.  
    default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
  19.  
    [root@localhost ~]# route del -net 224.0.0.0 netmask 240.0.0.0 reject
  20.  
    [root@localhost ~]# route
  21.  
    Kernel IP routing table
  22.  
    Destination Gateway Genmask Flags Metric Ref Use Iface
  23.  
    192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
  24.  
    192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
  25.  
    10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
  26.  
    default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
  27.  
    [root@localhost ~]#

说明:

实例5:删除和添加设置默认网关

命令:

route del default gw 192.168.120.240

route add default gw 192.168.120.240

输出:

  1.  
    [root@localhost ~]# route del default gw 192.168.120.240
  2.  
    [root@localhost ~]# route
  3.  
    Kernel IP routing table
  4.  
    Destination Gateway Genmask Flags Metric Ref Use Iface
  5.  
    192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
  6.  
    192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
  7.  
    10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
  8.  
    [root@localhost ~]# route add default gw 192.168.120.240
  9.  
    [root@localhost ~]# route
  10.  
    Kernel IP routing table
  11.  
    Destination Gateway Genmask Flags Metric Ref Use Iface
  12.  
    192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
  13.  
    192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
  14.  
    10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
  15.  
    default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
  16.  
    [root@localhost ~]#

运维route语法的更多相关文章

  1. 运维ldd语法--》ldconfig

    Linux:ldd命令详解   ldd 用于打印程序或者库文件所依赖的共享库列表. 语法 ldd(选项)(参数) 选项 --version:打印指令版本号: -v:详细信息模式,打印所有相关信息: - ...

  2. 运维ip语法,DNS配置方法

    修改配置文件: /etc/resolv.conf nameserver DNS_IP_1 nameserver DNS_IP_2 nameserver 指定本机解析: /etc/hosts 主机IP ...

  3. 运维chroot语法

    chroot命令 chroot命令用来在指定的根目录下运行指令.chroot,即 change root directory (更改 root 目录).在 linux 系统中,系统默认的目录结构都是以 ...

  4. 运维ps语法---》ps、pstree、top、htop、nice、renice、kill、ulimit、w 和 who 和 whoami、pgrep、fg 和 bg、ipcs

    Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...

  5. 运维rpm语法

    Linux软件包分类 rpm 常用命令1.安装一个包 # rpm -ivh 2.升级一个包 # rpm -Uvh 3.卸载一个包 # rpm -e 4.安装参数 --force 即使覆盖属于其它包的文 ...

  6. 运维seq语法

    seq-print a sequence of numbers 用于产生从某个数到另外一个数之间的所有整数 语法:seq 开始列  指定步长  结束列 参数: -f :指定输出格式,允许使用print ...

  7. 运维grep语法

    grep的语法和用法 grep命令的格式: grep   [options]   PATTERN  [FILE] 其中:1,pattern是用正则表达式书写的模式.2,FILE是要查找的文件,可以是用 ...

  8. 运维nslookup语法

    nslookup 查询域名DNS信息的工具 补充说明 nslookup命令 是常用域名查询工具,就是查DNS信息用的命令. nslookup4有两种工作模式,即“交互模式”和“非交互模式”.在“交互模 ...

  9. 运维dig语法

    dig命令是常用的域名查询工具,可以用来测试域名系统工作是否正常 语法 1 dig(选项)(参数) 选项 1 @<服务器地址>:指定进行域名解析的域名服务器: 2 -b<ip地址&g ...

随机推荐

  1. Oracle使用

    Oracle数据库首先必须在服务器端安装,安装完成后在DBCA中创建数据库,然后在Net Configuration Assistant中配置监听程序和本地Net服务名.然后安装本地Oracle客户端 ...

  2. 超简DbHelper

    using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Dynamic; n ...

  3. 从零开始学Python 三(网络爬虫)

    本章由网络爬虫的编写来学习python.首先写几行代码抓取百度首页,提提精神,代码如下: import urllib.request file=urllib.request.urlopen(" ...

  4. 【安全测试】如何利用短信验证码BUG浪费公司的钱

    一.背景 公司新产品体验,发现不少交互.UI.功能设计上的小问题.于是花了点时间随意挑了几个功能深入的玩了一下,顺手提了BUG.接口层,看了一下接口文档,简单测了一下接口,BUG其实还挺严重的,后面详 ...

  5. oracle分析性能问题实例

    摘录于SAP有关分析ORACLE数据性能事件的文档. 1.A check for the distribution of relevant Oracle server time revealed: 有 ...

  6. editorconfig使用

    //是否是顶级配置文件,设置为true的时候才会停止搜索.editorconfig文件 root = true [*] //缩进方式tab" | "space indent_sty ...

  7. 阿里推荐的线程使用方法 ThreadPoolExecutor

    阿里推荐原因:使用线程池可以减少创建和销毁线程上所花的时间以及系统资源的开销,然后之所以不用Executors自定义线程池,用ThreadPoolExecutor是为了规范线程池的使用,还有让其他人更 ...

  8. Html+css学习笔记一 创建一个网页

    第一个网页 新建一个记事本,把名字改成first.html <html> <head> <title>MyFristHtml</title> </ ...

  9. 带你领略Linux系统发展及版本更迭

    Linux的出现是在1991年,Linus Torvalds的学生开发的,最初的Linux是类似Unix操作系统,可用于386,486或奔腾处理器的计算机上.Linus Torvalds是一个伟人,他 ...

  10. matlab 将数字矩阵转换成图像

    MATLAB中,常常使用imshow()函数来显示图像,而此时的图像矩阵可能经过了某种运算.在matlab中,为了保证精度,经过了运算的图像矩阵I其数据类型会从unit8型变成double型.如果直接 ...