转自:  http://codingstandards.iteye.com/blog/1125312

用途说明

route命令用于显示和操作IP路由表(show / manipulate the IP routing table)。要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现。在Linux系统中,设置路由通常是 为了解决以下问题:该Linux系统在一个局域网中,局域网中有一个网关,能够让机器访问Internet,那么就需要将这台机器的IP地址设置为 Linux机器的默认路由。要注意的是,直接在命令行下执行route命令来添加路由,不会永久保存,当网卡重启或者机器重启之后,该路由就失效了;可以 在/etc/rc.local中添加route命令来保证该路由设置永久有效。本文中的例子中会验证这一点。

常用参数

格式:route

格式:/sbin/route

用于打印路由表(display the current routing table)。

在非root用户使用时需要使用完整路径执行route命令。

格式:route -n

格式:/sbin/route -n

用于打印路由表,加上-n参数就是在输出的信息中不打印主机名而直接打印ip地址。像netstat命令也有此参数。

格式:route add default gw {IP-ADDRESS} {INTERFACE-NAME}

用于设置默认路由(adds a default route, which will be used if no other route matches),其中,

参数{IP-ADDRESS): 用于指定路由器(网关)的IP地址(Specify router IP address);
参数{INTERFACE-NAME}: 用于指定接口名称,如eth0(Specify interface name such as eth0)。使用/sbin/ifconfig -a可以显示所有接口信息。

man route 写道
route add default gw mango-gw
adds a default route (which will be used if no other route matches). All packets using this route will
be gatewayed through "mango-gw". The device which will actually be used for that route depends on how we
can reach "mango-gw" - the static route to "mango-gw" will have to be set up before.

格式:route add -net {NETWORK-ADDRESS} netmask {NETMASK} dev {INTERFACE-NAME}

添加到指定网络的路由规则,其中

参数{NETWORK-ADDRESS}: 用于指定网络地址

参数{NETMASK}: 用于指定子网掩码

参数{INTERFACE-NAME}: 用于指定接口名称,如eth0。

man route 写道
route add -net 192.56.76.0 netmask 255.255.255.0 dev eth0
adds a route to the network 192.56.76.x via "eth0". The Class C netmask modifier is not really necessary
here because 192.* is a Class C IP address. The word "dev" can be omitted here.

route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
This is an obscure one documented so people know how to do it. This sets all of the class D (multicast)
IP routes to go via "eth0". This is the correct normal configuration line with a multicasting kernel.

格式:route add -net {NETWORK-ADDRESS} netmask {NETMASK} reject

设置到指定网络为不可达,避免在连接到这个网络的地址时程序过长时间的等待,直接就知道该网络不可达。

man route 写道
route add -net 10.0.0.0 netmask 255.0.0.0 reject
This installs a rejecting route for the private network "10.x.x.x."

格式:route del -net {NETWORK-ADDRESS} netmask {NETMASK} dev {INTERFACE-NAME}

格式:route del -net {NETWORK-ADDRESS} netmask {NETMASK} reject

用于删除路由设置。参数指定的方式与route add相似。

route命令输出的路由表字段含义如下:

Destination 目标
              The destination network or destination host. 目标网络或目标主机。

Gateway 网关
              The gateway address or '*' if none set. 网关地址,如果没有就显示星号。

Genmask 网络掩码
              The  netmask  for  the  destination net; '255.255.255.255' for a
              host destination and '0.0.0.0' for the default route.

Flags  Possible flags include 标志,常用的是U和G。
              U (route is up) 路由启用

              H (target is a host) 目标是主机
              G (use gateway) 使用网关

              R (reinstate route for dynamic routing)
              D (dynamically installed by daemon or redirect)
              M (modified from routing daemon or redirect)
              A (installed by addrconf)
              C (cache entry)
              !  (reject route)

Metric 距离、跳数。暂无用。

The 'distance' to the target (usually counted in  hops).  It  is
              not  used  by  recent kernels, but may be needed by routing dae-
              mons.

Ref   不用管,恒为0。

Number of references to this route. (Not used in the Linux  ker-
              nel.)

Use    该路由被使用的次数,可以粗略估计通向指定网络地址的网络流量。

Count  of lookups for the route.  Depending on the use of -F and
              -C this will be either route cache misses (-F) or hits (-C).

Iface 接口,即eth0,eth0等网络接口名

Interface to which packets for this route will be sent.

使用示例

示例一 打印当前路由表(root用户)

[root@jfht ~]# route

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
211.103.28.0    *               255.255.255.224 U     0      0        0 eth0
192.168.1.0     *               255.255.255.0   U     0      0        0 eth1
169.254.0.0     *               255.255.0.0     U     0      0        0 eth1
default         211.103.28.1    0.0.0.0         UG    0      0        0 eth0
[root@jfht ~]# /sbin/route

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
211.103.28.0    *               255.255.255.224 U     0      0        0 eth0
192.168.1.0     *               255.255.255.0   U     0      0        0 eth1
169.254.0.0     *               255.255.0.0     U     0      0        0 eth1
default         211.103.28.1    0.0.0.0         UG    0      0        0 eth0
[root@jfht ~]# route -n

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
211.103.28.0    0.0.0.0         255.255.255.224 U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth1
0.0.0.0         211.103.28.1    0.0.0.0         UG    0      0        0 eth0
[root@jfht ~]# /sbin/route -n

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
211.103.28.0    0.0.0.0         255.255.255.224 U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth1
0.0.0.0         211.103.28.1    0.0.0.0         UG    0      0        0 eth0
[root@jfht ~]#

示例二 打印当前路由表(非root用户)

[web@hnweb1 ~]$ route

-bash: route: command not found
[web@hnweb1 ~]$ /sbin/route

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.66.10.0      *               255.255.255.128 U     0      0        0 eth0
192.130.12.0    10.66.10.1      255.255.255.0   UG    0      0        0 eth0
10.0.0.0        *               255.255.255.0   U     0      0        0 eth1
10.66.0.0       10.66.10.1      255.255.0.0     UG    0      0        0 eth0
134.161.0.0     10.66.10.1      255.255.0.0     UG    0      0        0 eth0
10.20.0.0       10.66.10.1      255.255.0.0     UG    0      0        0 eth0
169.254.0.0     *               255.255.0.0     U     0      0        0 eth1
172.224.0.0     10.66.10.1      255.255.0.0     UG    0      0        0 eth0
default         10.66.10.22     0.0.0.0         UG    0      0        0 eth0
[web@hnweb1 ~]$ route -n

-bash: route: command not found
[web@hnweb1 ~]$ /sbin/route -n

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.66.10.0      0.0.0.0         255.255.255.128 U     0      0        0 eth0
192.130.12.0    10.66.10.1      255.255.255.0   UG    0      0        0 eth0
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth1
10.66.0.0       10.66.10.1      255.255.0.0     UG    0      0        0 eth0
134.161.0.0     10.66.10.1      255.255.0.0     UG    0      0        0 eth0
10.20.0.0       10.66.10.1      255.255.0.0     UG    0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth1
172.224.0.0     10.66.10.1      255.255.0.0     UG    0      0        0 eth0
0.0.0.0         10.66.10.22     0.0.0.0         UG    0      0        0 eth0
[web@hnweb1 ~]$

示例三 设置到某网络的路由的例子

下面的例子
来自一个实际的服务器配置。
route命令写在了/etc/rc.local中,这样就设置了一条永久路由。

[root@jf07 root]# grep route /etc/rc.local

route add -net 10.0.0.0/8 gw 10.33.149.1 dev eth1


[root@jf07 root]# route

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.32.181.182   10.33.149.1     255.255.255.255 UGH   0      0        0 eth1
10.33.136.135   10.33.149.1     255.255.255.255 UGH   0      0        0 eth1
10.32.208.13    10.33.149.1     255.255.255.255 UGH   0      0        0 eth1
10.33.149.0     *               255.255.255.128 U     0      0        0 eth1
192.168.1.0     *               255.255.255.0   U     0      0        0 eth0
169.254.0.0     *               255.255.0.0     U     0      0        0 eth1
10.0.0.0        10.33.149.1     255.0.0.0       UG    0      0        0 eth1

default         192.168.1.254   0.0.0.0         UG    0      0        0 eth0
[root@jf07 root]#

示例四 添加拒绝路由的测试

[root@jfht ~]# route

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
211.103.28.0    *               255.255.255.224 U     0      0        0 eth0
192.168.1.0     *               255.255.255.0   U     0      0        0 eth1
169.254.0.0     *               255.255.0.0     U     0      0        0 eth1
default         211.103.28.1    0.0.0.0         UG    0      0        0 eth0
[root@jfht ~]# ping 10.33.11.12

PING 10.33.11.12 (10.33.11.12) 56(84) bytes of data.
Ctrl+C

--- 10.33.11.12 ping statistics ---
21 packets transmitted, 0 received, 100% packet loss, time 19999ms

[root@jfht ~]# route add -net 10.0.0.0 netmask 255.0.0.0 reject

[root@jfht ~]# route

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
211.103.28.0    *               255.255.255.224 U     0      0        0 eth0
192.168.1.0     *               255.255.255.0   U     0      0        0 eth1
169.254.0.0     *               255.255.0.0     U     0      0        0 eth1
10.0.0.0        -               255.0.0.0       !     0      -        0 -
default         211.103.28.1    0.0.0.0         UG    0      0        0 eth0
[root@jfht ~]# ping 10.33.11.12
                               
connect: Network is unreachable
[root@jfht ~]#

示例五 设置路由之后重启机器的测试

[root@node34 root]# route

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.227.0   *               255.255.255.0   U     0      0        0 eth0
169.254.0.0     *               255.255.0.0     U     0      0        0 eth0
default         192.168.227.2   0.0.0.0         UG    0      0        0 eth0
[root@node34 root]# route add -net 10.0.0.0 netmask 255.0.0.0 reject

[root@node34 root]# route

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.227.0   *               255.255.255.0   U     0      0        0 eth0
169.254.0.0     *               255.255.0.0     U     0      0        0 eth0
10.0.0.0        -               255.0.0.0       !     0      -        0 -
default         192.168.227.2   0.0.0.0         UG    0      0        0 eth0
[root@node34 root]# reboot

Broadcast message from root (pts/0) (Thu Jul  7 05:31:26 2011):

The system is going down for reboot NOW!
[root@node34 root]#

Last login: Thu Jul  7 05:30:50 2011 from 192.168.227.1
[root@node34 root]# route

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.227.0   *               255.255.255.0   U     0      0        0 eth0
169.254.0.0     *               255.255.0.0     U     0      0        0 eth0
default         192.168.227.2   0.0.0.0         UG    0      0        0 eth0
[root@node34 root]#

上面的测试表明route设置的路由在机器重启之后就消失了。

示例六 将route命令添加到/etc/rc.local来设置永久路由的测试

先用vi在/etc/rc.local后面添加route命令。

[root@node34 root]# tail /etc/rc.local

#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

# 2011.07.15 add permanent route test
route add -net 10.0.0.0 netmask 255.0.0.0 reject

[root@node34 root]# route

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.227.0   *               255.255.255.0   U     0      0        0 eth0
169.254.0.0     *               255.255.0.0     U     0      0        0 eth0
default         192.168.227.2   0.0.0.0         UG    0      0        0 eth0
[root@node34 root]# reboot

Broadcast message from root (pts/0) (Fri Jul 15 14:43:44 2011):

The system is going down for reboot NOW!
[root@node34 root]#

Last login: Fri Jul 15 14:40:22 2011 from 192.168.227.1
[root@node34 root]# route

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.227.0   *               255.255.255.0   U     0      0        0 eth0
169.254.0.0     *               255.255.0.0     U     0      0        0 eth0
10.0.0.0        -               255.0.0.0       !     0      -        0 -
default         192.168.227.2   0.0.0.0         UG    0      0        0 eth0
[root@node34 root]#

示例七 删除路由的测试

删除路由的时候只需将route add改成route del,其他参数类似。如果报“无效的参数”或“没有那个进程”,可能是因为提供的参数不够。

[root@node34 root]# route

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.227.0   *               255.255.255.0   U     0      0        0 eth0
169.254.0.0     *               255.255.0.0     U     0      0        0 eth0
10.0.0.0        -               255.0.0.0       !     0      -        0 -
default         192.168.227.2   0.0.0.0         UG    0      0        0 eth0
[root@node34 root]#
[root@node34 root]# route del -net 10.0.0.0

SIOCDELRT: 无效的参数
[root@node34 root]# route del -net 10.0.0.0 netmask 255.0.0.0

SIOCDELRT: 没有那个进程
[root@node34 root]#
[root@node34 root]# route del -net 10.0.0.0 netmask 255.0.0.0 reject

[root@node34 root]#

[root@node34 root]# route

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.227.0   *               255.255.255.0   U     0      0        0 eth0
169.254.0.0     *               255.255.0.0     U     0      0        0 eth0
default         192.168.227.2   0.0.0.0         UG    0      0        0 eth0
[root@node34 root]#

问题思考

相关资料

【1】nixCraft Linux setup default gateway with route command
http://www.cyberciti.biz/faq/linux-setup-default-gateway-with-route-command/

【2】360doc Linux route命令
http://www.360doc.com/content/11/0418/14/2054285_110501874.shtml

【3】Linux公社 Linux下route add route del 用法
http://www.linuxidc.com/Linux/2010-11/30032.htm

【4】鳥哥的 Linux 私房菜 第五章、 Linux 常用網路指令

http://linux.vbird.org/linux_server/0140networkcommand.php#route

Linux命令之route - 显示和操作IP路由表的更多相关文章

  1. Linux route命令详解和使用示例(查看和操作IP路由表)

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

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

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

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

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

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

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

  5. Linux命令:route命令

    route显示或修改IP路由表 route -n:显示路由信息,使用数字格式显示,不反解地址到主机名 #route -n Kernel IP routing table Destination Gat ...

  6. Linux命令之hostname - 显示或设置主机名

    我使用过的Linux命令之hostname - 显示或设置主机名 本文链接:http://codingstandards.iteye.com/blog/804648   (转载请注明出处) 用途说明 ...

  7. linux命令(5)文件操作:ls命令、显示文件总个数

    一:ls命令是最常用的linux命令了:下面是ls --help里面的用法 在提示符下输入ls --help ,屏幕会显示该命令的使用格式及参数信息: 先介绍一下ls命令的主要参数: -a 列出目录下 ...

  8. Linux命令之type - 显示命令的类型

    用途说明 type命令用来显示指定命令的类型.一个命令的类型可以是如下之一 alias 别名 keyword 关键字,Shell保留字 function 函数,Shell函数 builtin 内建命令 ...

  9. linux命令之磁盘和文件系统操作

    1.   fdisk:磁盘分区命令 语法:fdisk [选项][参数] 命令说明:fdisk是linux系统里常用的一种磁盘管理工具,可以创建和管理系统分区 常用命令选项: -l:列出指定的并退出,没 ...

随机推荐

  1. asp.net Ajax刷新和无刷新的区别

    无刷新按钮btnShua 刷新按钮btnWu label控件和calendar控件在updatePanel中显示 两个button按钮在div中 <%@ Page Language=" ...

  2. 事件:卸载事件(onunload)

    这是几点应卸载事件的说明 ①目前试了Firefox.Google Chrome.IE三个浏览器,该事件只对IE起作用. ②onunload事件对于刷新页面和超链接跳转其他页面情况有效,对于关闭页面无效 ...

  3. VisualRust + VisualGDB编辑调试Rust

    Rust到1.6了,到了一个相对成熟的阶段,可以试用做一些项目了.但是写的代码越多,就会发现一个好的IDE相当于效率的一半.这里分享我 在Visual Studio的使用Rust的经验. 首先需要下载 ...

  4. angularjs(二)模板终常用的指令的使用方法

    通过使用模板,我们可以把model和controller中的数据组装起来呈现给浏览器,还可以通过数据绑定,实时更新视图,让我们的页面变成动态的.ng的模板真是让我爱不释手.学习ng道路还很漫长,从模板 ...

  5. Appium学习路—脚本篇(启动app)

    启动之前的准备   1.脚本执行前,需要先启动appium的server端, 启动server方法: 打开appium客户端,点击右上角的Launch     2.iOS的测试只能在mac本上做   ...

  6. WPF自适应窗体实现小结

    WPF自适应窗体实现小结 这几天,因工作需要,要对一个小软件进行UI调整.主要内容就是让其能够实现自适应窗体(包括文字和图标),做成像WIN7下的Media Center一样的UI.自适应窗体,顾名思 ...

  7. Java垃圾回收机制 入门

    对于Java虚拟机的了解,我认为是一个Java程序员已经入门的重要标志,而JVM中的垃圾回收机制(Garbage Collection,简称GC)又是JVM中的重点,所以hans在这里用篇文章时间和大 ...

  8. Light OJ 1029- Civil and Evil Engineer (图论-最小生成树)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1029 题目大意:一个发电站,给n座房子供电, 任意房子之间有电线直接或者间接相 ...

  9. oracle 脱敏和加密

     脱敏:在此只是对数据如姓名,身份证号码等进行简单粗暴的脱敏,即改变数据 针对于number类型的数据:update table_name set conlunm_name =  substr (co ...

  10. 网页链接qq

    <a href="mqqwpa://im/chat?chat_type=wpa&uin=12345678&version=1&src_type=web& ...