Linux系統的route命令用於顯示和操作IP路由表(show / manipulate the IP routing table)。要實現兩個不同的子網之間的通訊,需要一臺連線兩個網路的路由器,或者同時位於兩個網路的閘道器來實現。在Linux系統中,設定路由通常是為了解決以下問題:該Linux系統在一個區域網中,區域網中有一個閘道器,能夠讓機器訪問Internet,那麼就需要將這臺機器的IP地址設定為Linux機器的預設路由。要注意的是,直接在命令列下執行route命令來新增路由,不會永久儲存,當網路卡重啟或者機器重啟之後,該路由就失效了;可以在/etc/rc.local中新增route命令來保證該路由設定永久有效。
1.命令格式:
1 |
route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]] |
2.命令功能:
Route命令是用於操作基於核心ip路由表,它的主要作用是建立一個靜態路由讓指定一個主機或者一個網路通過一個網路介面,如eth0。當使用”add”或者”del”引數時,路由表被修改,如果沒有引數,則顯示路由表當前的內容。
3.命令引數:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
-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 命令的顯示功能。可以使用十進位制或十六進位制值進行介面索引。 |
4.使用例項:
例項1:顯示當前路由
命令:
1 2 3 |
route route -n |
輸出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[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:新增閘道器/設定閘道器
命令:
1 |
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0 |
輸出:
1 2 3 4 5 6 7 8 9 10 11 |
[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:遮蔽一條路由
命令:
1 |
route add -net 224.0.0.0 netmask 240.0.0.0 reject |
輸出:
1 2 3 4 5 6 7 8 9 10 |
[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:刪除路由記錄
命令:
1 2 3 |
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 |
輸出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
[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:刪除和新增設定預設閘道器
命令:
1 2 3 |
route del default gw 192.168.120.240 route add default gw 192.168.120.240 |
輸出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[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 ~]# |
說明:
本系列文章:
每天一個 Linux 命令(1):ls命令
每天一個 Linux 命令(2):cd命令
每天一個 Linux 命令(3):pwd命令
每天一個 Linux 命令(4):mkdir命令
每天一個 Linux 命令(5):rm 命令
每天一個 Linux 命令(6):rmdir 命令
每天一個 Linux 命令(7):mv命令
每天一個 Linux 命令(8):cp 命令
每天一個 Linux 命令(9):touch 命令
每天一個 Linux 命令(10):cat 命令
每天一個 Linux 命令(11):nl 命令
每天一個 Linux 命令(12):more 命令
每天一個 Linux 命令(13):less 命令
每天一個 Linux 命令(14):head 命令
每天一個 Linux 命令(15):tail 命令
每天一個 Linux 命令(16):which命令
每天一個 Linux 命令(17):whereis 命令
每天一個 Linux 命令(18):locate 命令
每天一個 Linux 命令(19):find 命令概覽
每天一個 Linux 命令(20):find命令之exec
每天一個 Linux 命令(21):find命令之xargs
每天一個 Linux 命令(22):find 命令的引數詳解
每天一個 Linux 命令(23):Linux 目錄結構
每天一個 Linux 命令(24):Linux 檔案型別與副檔名
每天一個 Linux 命令(25):Linux 檔案屬性詳解
每天一個 Linux 命令(26):用 SecureCRT 來上傳和下載檔案
每天一個 Linux 命令(27):linux chmod 命令
每天一個 Linux 命令(28):tar 命令
每天一個 Linux 命令(29): chgrp 命令
每天一個 Linux 命令(30): chown 命令
每天一個 Linux 命令(31): /etc/group 檔案詳解
每天一個 Linux 命令(32):gzip 命令
每天一個 Linux 命令(33):df 命令
每天一個 Linux 命令(34): du 命令
每天一個 Linux 命令(35): ln 命令
每天一個 Linux 命令(36): diff 命令
每天一個 Linux 命令(37): date 命令
每天一個 Linux 命令(38): cal 命令
每天一個 Linux 命令(39): grep 命令
每天一個 Linux 命令(40): wc 命令
每天一個 Linux 命令(41): ps 命令
每天一個 Linux 命令(44): top 命令
每天一個 Linux 命令(45): free 命令
每天一個 Linux 命令(46): vmstat 命令
每天一個 Linux 命令(47): iostat 命令
每天一個 Linux 命令(48): watch 命令
每天一個 Linux 命令(49): at 命令
每天一個 Linux 命令(50): crontab 命令
每天一個 Linux 命令(51): lsof 命令
每天一個 Linux 命令(52): ifconfig 命令