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]]
2.命令功能
route命令是用於操作基於內核ip路由表,它的主要作用是創建一個靜態路由讓指定一個主機或者一個網路通過一個網路介面,如eth0。當使用”add“或者”del“參數時,路由表被修改,如果沒有參數,則顯示路由表當前的內容。
3.命令參數
-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:顯示當前路由
命令:
route
route -n
輸出:
[zaixian@localhost ~]$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default gateway 0.0.0.0 UG 100 0 0 ens33
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
[zaixian@localhost ~]$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 ens33
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
[zaixian@localhost ~]$
說明:
第一行表示主機所在網路的地址為192.168.0.197,若數據傳送目標是在本局域網內通信,則可直接通過ens33轉發數據包;
第四行表示數據傳送目的是訪問 Internet,則由介面ens33,將數據包發送到網關192.168.0.1
其中Flags為路由標誌,標記當前網路節點的狀態。
Flags標誌說明:
UUp表示此路由當前為啟動狀態HHost,表示此網關為一主機GGateway,表示此網關為一路由器RReinstate Route,使用動態路由重新初始化的路由DDynamically,此路由是動態性地寫入MModified,此路由是由路由守護程式或導向器動態修改!表示此路由當前為關閉狀態
備註: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.0.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.0.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.0.240 0.0.0.0 UG 0 0 0 eth0
說明:增加一條 到達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.0.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.0.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.0.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.0.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.0.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.0.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.0.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.0.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.0.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.0.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.0.1 255.0.0.0 UG 0 0 0 eth0
224.0.0.0 - 240.0.0.0 ! 0 - 0 -
default 192.168.0.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.0.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.0.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.0.1 255.0.0.0 UG 0 0 0 eth0
default 192.168.0.240 0.0.0.0 UG 0 0 0 eth0
[root@localhost ~]#
實例5:刪除和添加設置默認網關
命令:
route del default gw 192.168.0.240
route add default gw 192.168.0.240
輸出:
[root@localhost ~]# route del default gw 192.168.1.240
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.1.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.1.1 255.0.0.0 UG 0 0 0 eth0
[root@localhost ~]# route add default gw 192.168.1.240
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
192.168.1.0 192.168.1.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.1.1 255.0.0.0 UG 0 0 0 eth0
default 192.168.1.240 0.0.0.0 UG 0 0 0 eth0
[root@localhost ~]#
上一篇:
ifconfig命令
下一篇:
ping命令
