首頁  >  文章  >  運維  >  route指令詳解

route指令詳解

藏色散人
藏色散人原創
2020-03-03 09:25:2612613瀏覽

route指令詳解

route指令詳解

#Linux系統的route指令用於顯示和操作IP路由表(show / manipulate the IP routing table)。要實現兩個不同的子網路之間的通信,需要一台連接兩個網路的路由器,或同時位於兩個網路的網關來實現。在Linux系統中,設定路由通常是為了解決以下問題:

該Linux系統在一個區域網路中,區域網路中有一個網關,能夠讓機器存取Internet,那麼就需要將這台機器的IP位址設定為Linux機器的預設路由。要注意的是,直接在命令列下執行route命令來添加路由,不會永久保存,當網卡重啟或機器重啟之後,該路由就失效了;可以在/etc/rc.local中添加route命令來保證此路由設定永久有效。

推薦:《Linux教學

#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

#輸出:

[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 Dyn​​amically,此路由是動態地寫入

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 ~]#

 

以上是route指令詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn