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.  
  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. e192.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. default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
  9. [root@localhost ~]# route -n
  10. Kernel IP routing table
  11. Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
  12. 192.168.120.0   0.0.0.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. 0.0.0.0         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
  16.  

说明:

第一行表示主机所在网络的地址为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.  
  2.  
  3. [root@localhost ~]# route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
  4. [root@localhost ~]# route
  5. Kernel IP routing table
  6. Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
  7. 192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
  8. 192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
  9. 10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
  10. 224.0.0.0       *               240.0.0.0       U     0      0        0 eth0
  11. default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
  12.  

[root@localhost ~]#

说明:

增加一条 到达244.0.0.0的路由

实例3:屏蔽一条路由

命令:

route add -net 224.0.0.0 netmask 240.0.0.0 reject

输出:

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

说明:

增加一条屏蔽的路由,目的地址为 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.  
  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
  11. [root@localhost ~]# route del -net 224.0.0.0 netmask 240.0.0.0
  12. [root@localhost ~]# route
  13. Kernel IP routing table
  14. Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
  15. 192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
  16. 192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
  17. 10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
  18. 224.0.0.0       -               240.0.0.0       !     0      -        0 -
  19. default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
  20. [root@localhost ~]# route del -net 224.0.0.0 netmask 240.0.0.0 reject
  21. [root@localhost ~]# route
  22. Kernel IP routing table
  23. Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
  24. 192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
  25. 192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
  26. 10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
  27. default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
  28. [root@localhost ~]# 
  29.  

说明:

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

命令:

route del default gw 192.168.120.240

route add default gw 192.168.120.240

输出:

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

每天一个linux命令(45):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常用命令:route 命令

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

  4. 每天一个linux命令(45)--telnet命令

    每天一个Linux命令,今天是网络命令中的Telnet. Telnet 命令通常用来远程登录,Telnet 程序是基于 Telnet 协议的远程登录客户端程序.Telnet 协议是TCP/IP协议族中 ...

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

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

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

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

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

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

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

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

  9. linux下的route命令

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

  10. [网络配置相关]——ifconfig命令、ip命令、route命令

    ifconfig命令 1. 查看已被激活的网卡的详细信息 # ifconfig eth0 Link encap:Ethernet HWaddr 00:30:67:F2:10:CF inet addr: ...

随机推荐

  1. [转][ASP.NET MVC 小牛之路]12 - Section、Partial View 和 Child Action

    本文转自:http://www.cnblogs.com/willick/p/3410855.html 概括的讲,View中的内容可以分为静态和动态两部分.静态内容一般是html元素,而动态内容指的是在 ...

  2. 【Ext.Net学习笔记】03:Ext.Net DirectEvents用法详解、DirectMethods用法详解

    Ext.Net通过DirectEvents进行服务器端异步的事件处理.[Ext.Net学习笔记]02:Ext.Net用法概览.Ext.Net MessageBus用法.Ext.Net布局 中已经简单的 ...

  3. 边工作边刷题:70天一遍leetcode: day 89

    Word Break I/II 现在看都是小case题了,一遍过了.注意这题不是np complete,dp解的time complexity可以是O(n^2) or O(nm) (取决于inner ...

  4. bzoj-3170 3170: [Tjoi 2013]松鼠聚会(计算几何)

    题目链接: 3170: [Tjoi 2013]松鼠聚会 Time Limit: 10 Sec  Memory Limit: 128 MB Description 有N个小松鼠,它们的家用一个点x,y表 ...

  5. 2014 Super Training #10 D 花生的序列 --DP

    原题: FZU 2170 http://acm.fzu.edu.cn/problem.php?pid=2170 这题确实是当时没读懂题目,连样例都没想通,所以没做了,所以还是感觉这样散漫的做不好,有些 ...

  6. Jenkins学习七:Jenkins的授权和访问控制

    默认的Jenkins不包含任何的安全检查,任何人可以修改Jenkins设置,job和启动build等.显然地在大规模的公司需要多个部门一起协调工作的时候,没有任何安全检查会带来很多的问题. 在系统管理 ...

  7. WPF学习笔记:MVVM模式下,ViewModel如何关闭View?

    原文:http://blog.csdn.net/leftfist/article/details/32349731 矫枉过正,从一个极端走向另一个极端.MVVM模式,View只负责呈现,虽然也有后台代 ...

  8. 【转】【SEE】基于SSE指令集的程序设计简介

    SSE技术简介 Intel公司的单指令多数据流式扩展(SSE,Streaming SIMD Extensions)技术能够有效增强CPU浮点运算的能力.Visual Studio .NET 2003提 ...

  9. Contains Duplicate II

    Given an array of integers and an integer k, find out whether there there are two distinct indices i ...

  10. Linux 进程与线程四(加锁--解锁)

    线程共享进程的内存空间,打开的文件描述符,全局变量. 当有多个线程同事访问一块内存空间或者一个变量.一个文件描述符,如果不加控制,那么可能会出现意想不到的结果. 原子操作 对于我们的高级语言(C语言, ...