歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> CentOS裡route命令詳解

CentOS裡route命令詳解

日期:2017/3/3 12:17:36   编辑:Linux技術
功能簡述:linux系統中的route命令能夠用於IP路由表的顯示和操作。它的主要作用是創建一個靜態路由讓指定一個主機或者一個網絡通過一個網絡接口,如eth0。當使用"add"或者"del"參數時,路由表被修改,如果沒有參數,則顯示路由表當前的內容。在一個網絡中,需要一個路由器來轉發不同廣播域之間的數據,或是轉發lan和internet之間的數據。有時我們需要設定這個路由器作為linux系統的默認路由,那麼就可以通過route命令來操作。甚至我們也可以用我們的linux系統來充當路由器。
要注意的是,直接在命令行下執行route命令來添加路由,不會永久保存,當網卡重啟或者機器重啟之後,該路由就失效了;可以在/etc/rc.local中添加route命令來保證該路由設置永久有效。當然如果加上了-p參數的話那就會永久的生效了。
命令格式:route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway][metric Metric]] [if Interface]]
命令參數:
-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 命令的顯示功能。可以使用十進制或十六進
(1)顯示路由信息
[root@www ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.20.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.40.1 0.0.0.0 UG 0 0 0 eth0
Flags標志說明:
U Up表示此路由當前為啟動狀態
H Host,表示此網關為一主機
G Gateway,表示此網關為一路由器
R Reinstate Route,使用動態路由重新初始化的路由
D Dynamically,此路由是動態性地寫入–》什麼時候才會有動態的路由信息呢?
M Modified,此路由是由路由守護程序或導向器動態修改
(2)添加一條指向某個網絡的路由
[root@ www ~]# route add -net 10.0.0.0 netmask 255.255.255.0 dev eth0—》這裡是指定這條路由的出口在哪裡。綠色部分:為指定目標網絡的參數,需要ip地址或地址范圍、子網掩碼用於確定網絡范圍。
[root@ www ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.0.0 * 255.255.255.0 U 0 0 0 eth0
192.168.20.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.40.1 0.0.0.0 UG 0 0 0 eth0
自我小結:route添加路由都是需要指定目標網絡,及路由出口這兩個參數。記住加上-p選項能永久添加。
(3)再來一條添加到某一個ip的路由
[root@ www ~]# route add -host 192.168.40.1dev eth0
[root@ www ~]# route ----》可以發現添加的是主機的話,默認是會幫我們添加一個全255的子網掩碼,表示子網范圍就只有一個而已,那就是這台主機啦。
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.40.1 * 255.255.255.255 UH 0 0 0 eth0
(4)屏蔽某一路由
當我們不讓系統到達某個子網范圍或者某個主機是就可以手動的來進行屏蔽。
[root@ www ~]# route add -net 10.10.32.0 netmask 255.255.255.0 reject—》前面部分是一樣的,因為我們都是手動來添加一個路由嘛。只是在命令的最後不一樣,我們指定的出口去而是reject(拒絕),也就是拒絕出口。達到屏蔽的效果。還有看下flags會顯示一個!
[root@ www ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.20.1 * 255.255.255.255 UH 0 0 0 eth0
10.10.32.0 - 255.255.255.0 ! 0 - 0 -
10.0.0.0 * 255.255.255.0 U 0 0 0 eth0
192.168.20.0 * 255.255.252.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
default 192.168.20.1 0.0.0.0 UG 0 0 0 eth0
(5)刪除路由
[root@ www ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.20.1 * 255.255.255.255UH 0 0 0 eth0
10.10.32.0 - 255.255.255.0 ! 0 - 0 -
10.0.0.0 * 255.255.255.0 U 0 0 0 eth0
192.168.20.0 * 255.255.252.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
default 192.168.20.1 0.0.0.0 UG 0 0 0 eth0
[root@ www ~]# route del -net 10.10.32.0 netmask 255.255.255.0 reject
[root@ www ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.20.1 * 255.255.255.255UH 0 0 0 eth0
10.0.0.0 * 255.255.255.0 U 0 0 0 eth0
192.168.20.0 * 255.255.252.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
default 192.168.20.1 0.0.0.0 UG 0 0 0 eth0
說明:看到沒有,路由已經被我刪除掉了,哈哈哈哈。刪除路由時,最好是看著路由表上的照樣打進去,這樣比較不會刪錯的。
(6)添加刪除默認網關
[root@ www ~]# route add default gw 192.168.20.2
[root@ www ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.20.1 * 255.255.255.255 UH 0 0 0 eth0
10.0.0.0 * 255.255.255.0 U 0 0 0 eth0
192.168.20.0 * 255.255.252.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
default 192.168.20.2 0.0.0.0 UG 0 0 0 eth0
default 192.168.20.1 0.0.0.0 UG 0 0 0 eth0
--》可以看到此處有兩個默認網關,那到底路由會走哪個呢?
[root@ www ~]# route del default gw192.168.20.2--- 》在這裡我們還是先刪除掉吧。
[root@ www ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.20.1 * 255.255.255.255UH 0 0 0 eth0
10.0.0.0 * 255.255.255.0 U 0 0 0 eth0
192.168.20.0 * 255.255.252.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
default 192.168.20.1 0.0.0.0 UG 0 0 0 eth0
轉自http://www.centoscn.com/CentOS/help/2014/0113/2351.html
Copyright © Linux教程網 All Rights Reserved