文章目錄
- 樣本一 列印當前路由表(root使用者)
- 樣本二 列印當前路由表(非root使用者)
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命令。TP-Link 620G在Fedora上安裝網卡驅動指南
格式: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 ~]$