我使用過的Linux命令之route - 顯示和操做IP路由表

本文連接:http://codingstandards.iteye.com/blog/1125312 (轉載請註明出處)

用途說明

route命令用於顯示和操做IP路由表(show / manipulate the IP routing table)。要實現兩個不一樣的子網之間的通訊,須要一臺鏈接兩個網絡的路由器,或者同時位於兩個網絡的網關來實現。在Linux系統中,設置路由一般是爲了解決如下問題:該Linux系統在一個局域網中,局域網中有一個網關,可以讓機器訪問Internet,那麼就須要將這臺機器的IP地址設置爲Linux機器的默認路由。要注意的是,直接在命令行下執行route命令來添加路由,不會永久保存,當網卡重啓或者機器重啓以後,該路由就失效了;能夠在/etc/rc.local中添加route命令來保證該路由設置永久有效。本文中的例子中會驗證這一點。php

 

經常使用參數

格式:routehtml

格式:/sbin/routenode

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

在非root用戶使用時須要使用完整路徑執行route命令。web

 

格式:route -nbash

格式:/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),其中,this

參數{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命令系列總目錄

相關文章
相關標籤/搜索