Linux route命令詳解:查看和操作IP路由表

來源:互聯網
上載者:User

標籤:

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

[email protected]:~# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface0.0.0.0         59.188.252.1    0.0.0.0         UG    100    0        0 br010.22.0.0       0.0.0.0         255.255.255.0   U     0      0        0 br059.188.252.0    0.0.0.0         255.255.255.0   U     0      0        0 br0172.22.0.0      0.0.0.0         255.255.255.0   U     0      0        0 br1192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

說明:

第一行表示主機所在網路的地址為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 


執行個體3:屏蔽一條路由
命令:
 route add -net 224.0.0.0 netmask 240.0.0.0 reject 

[[email protected] ~]# route add -net 224.0.0.0 netmask 240.0.0.0 reject[[email protected] ~]# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 0 0 0 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0224.0.0.0 - 240.0.0.0 ! 0 - 0 -224.0.0.0 * 240.0.0.0 U 0 0 0 eth0default 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.0route del -net 224.0.0.0 netmask 240.0.0.0 reject


執行個體5:刪除和添加設定預設閘道
命令:

route del default gw 192.168.120.240route add default gw 192.168.120.240

 

[[email protected] ~]# route del default gw 192.168.120.240[[email protected] ~]# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 0 0 0 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0[[email protected] ~]# route add default gw 192.168.120.240[[email protected] ~]# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.120.0 * 255.255.255.0 U 0 0 0 eth0192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth010.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0

 

Linux route命令詳解:查看和操作IP路由表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.