天天一個linux命令(53):route命令

原文地址;http://www.cnblogs.com/peida/archive/2013/03/05/2943698.htmlhtml

 

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]] spa

2.命令功能:命令行

Route命令是用於操做基於內核ip路由表,它的主要做用是建立一個靜態路由讓指定一個主機或者一個網絡經過一個網絡接口,如eth0。當使用"add"或者"del"參數時,路由表被修改,若是沒有參數,則顯示路由表當前的內容。code

3.命令參數:htm

-c 顯示更多信息blog

-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

輸出:

 

複製代碼
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
e192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
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

輸出:

複製代碼
[root@localhost ~]# route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       *               240.0.0.0       U     0      0        0 eth0
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

輸出:

 

複製代碼
[root@localhost ~]# route add -net 224.0.0.0 netmask 240.0.0.0 reject
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       -               240.0.0.0       !     0      -        0 -
224.0.0.0       *               240.0.0.0       U     0      0        0 eth0
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

輸出:

 

複製代碼
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       -               240.0.0.0       !     0      -        0 -
224.0.0.0       *               240.0.0.0       U     0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# route del -net 224.0.0.0 netmask 240.0.0.0
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       -               240.0.0.0       !     0      -        0 -
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# route del -net 224.0.0.0 netmask 240.0.0.0 reject
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# 
複製代碼

 

說明:

 

實例5:刪除和添加設置默認網關

命令:

route del default gw 192.168.120.240

route add default gw 192.168.120.240

輸出:

 

複製代碼
[root@localhost ~]# route del default gw 192.168.120.240
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
[root@localhost ~]# route add default gw 192.168.120.240
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# 
複製代碼

 

說明:

相關文章
相關標籤/搜索