濮阳杆衣贸易有限公司

主頁 > 知識庫 > Linux route命令詳解和使用示例(查看和操作IP路由表)

Linux route命令詳解和使用示例(查看和操作IP路由表)

熱門標簽:桂林市ai電銷機器人公司 新科火車站地圖標注點 制作地圖標注 地圖標注的牌子 外呼系統(tǒng)鏈接 外呼系統(tǒng)怎么弄 地址高德地圖標注 商店地圖標注外賣入駐 磁力導航地圖標注

在Linux系統(tǒng)中,設(shè)置路由通常是為了解決以下問題:該Linux系統(tǒng)在一個局域網(wǎng)中,局域網(wǎng)中有一個網(wǎng)關(guān),能夠讓機器訪問Internet,那么就需要將這臺機器的IP地址設(shè)置為Linux機器的默認路由。要注意的是,直接在命令行下執(zhí)行route命令來添加路由,不會永久保存,當網(wǎng)卡重啟或者機器重啟之后,該路由就失效了;可以在/etc/rc.local中添加route命令來保證該路由設(shè)置永久有效。

1.命令格式:

route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]] 

2.命令功能:

Route命令是用于操作基于內(nèi)核ip路由表,它的主要作用是創(chuàng)建一個靜態(tài)路由讓指定一個主機或者一個網(wǎng)絡(luò)通過一個網(wǎng)絡(luò)接口,如eth0。當使用"add"或者"del"參數(shù)時,路由表被修改,如果沒有參數(shù),則顯示路由表當前的內(nèi)容。

3.命令參數(shù):

-c 顯示更多信息

-n 不解析名字

-v 顯示詳細的處理信息

-F 顯示發(fā)送信息

-C 顯示路由緩存

-f 清除所有網(wǎng)關(guān)入口的路由表。 

-p 與 add 命令一起使用時使路由具有永久性。

 

add:添加一條新路由。

del:刪除一條路由。

-net:目標地址是一個網(wǎng)絡(luò)。

-host:目標地址是一個主機。

netmask:當添加一個網(wǎng)絡(luò)路由時,需要使用網(wǎng)絡(luò)掩碼。

gw:路由數(shù)據(jù)包通過網(wǎng)關(guān)。注意,你指定的網(wǎng)關(guān)必須能夠達到。

metric:設(shè)置路由跳數(shù)。

Command 指定您想運行的命令 (Add/Change/Delete/Print)。 

Destination 指定該路由的網(wǎng)絡(luò)目標。 

mask Netmask 指定與網(wǎng)絡(luò)目標相關(guān)的網(wǎng)絡(luò)掩碼(也被稱作子網(wǎng)掩碼)。 

Gateway 指定網(wǎng)絡(luò)目標定義的地址集和子網(wǎng)掩碼可以到達的前進或下一躍點 IP 地址。 

metric Metric 為路由指定一個整數(shù)成本值標(從 1 至 9999),當在路由表(與轉(zhuǎn)發(fā)的數(shù)據(jù)包目標地址最匹配)的多個路由中進行選擇時可以使用。 

if Interface 為可以訪問目標的接口指定接口索引。若要獲得一個接口列表和它們相應(yīng)的接口索引,使用 route print 命令的顯示功能??梢允褂檬M制或十六進制值進行接口索引。

4.使用實例:

實例1:顯示當前路由

命令:

復(fù)制代碼
代碼如下:

route
route -n

輸出:

 

復(fù)制代碼
代碼如下:

[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

說明:

第一行表示主機所在網(wǎng)絡(luò)的地址為192.168.120.0,若數(shù)據(jù)傳送目標是在本局域網(wǎng)內(nèi)通信,則可直接通過eth0轉(zhuǎn)發(fā)數(shù)據(jù)包;

第四行表示數(shù)據(jù)傳送目的是訪問Internet,則由接口eth0,將數(shù)據(jù)包發(fā)送到網(wǎng)關(guān)192.168.120.240

其中Flags為路由標志,標記當前網(wǎng)絡(luò)節(jié)點的狀態(tài)。

Flags標志說明:

U Up表示此路由當前為啟動狀態(tài)

H Host,表示此網(wǎng)關(guān)為一主機

G Gateway,表示此網(wǎng)關(guān)為一路由器

R Reinstate Route,使用動態(tài)路由重新初始化的路由

D Dynamically,此路由是動態(tài)性地寫入

M Modified,此路由是由路由守護程序或?qū)蚱鲃討B(tài)修改

! 表示此路由當前為關(guān)閉狀態(tài)

備注:

route -n (-n 表示不解析名字,列出速度會比route 快)

實例2:添加網(wǎng)關(guān)/設(shè)置網(wǎng)關(guān)

命令: route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0

輸出:


復(fù)制代碼
代碼如下:

[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

說明:

增加一條 到達244.0.0.0的路由

實例3:屏蔽一條路由

命令:route add -net 224.0.0.0 netmask 240.0.0.0 reject

輸出:

 

復(fù)制代碼
代碼如下:

[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:刪除路由記錄

命令:


復(fù)制代碼
代碼如下:

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

輸出:

 

復(fù)制代碼
代碼如下:

[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:刪除和添加設(shè)置默認網(wǎng)關(guān)

命令:


復(fù)制代碼
代碼如下:

route del default gw 192.168.120.240
route add default gw 192.168.120.240

輸出:


復(fù)制代碼
代碼如下:

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

標簽:江蘇 濱州 宿州 綏化 河源 常德 保山 亳州

巨人網(wǎng)絡(luò)通訊聲明:本文標題《Linux route命令詳解和使用示例(查看和操作IP路由表)》,本文關(guān)鍵詞  Linux,route,命令,詳解,和,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Linux route命令詳解和使用示例(查看和操作IP路由表)》相關(guān)的同類信息!
  • 本頁收集關(guān)于Linux route命令詳解和使用示例(查看和操作IP路由表)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    清丰县| 康马县| 霍林郭勒市| 岱山县| 息烽县| 岳普湖县| 贵州省| 柯坪县| 胶南市| 封开县| 商洛市| 塘沽区| 临潭县| 宁蒗| 怀集县| 额尔古纳市| 鹰潭市| 乌拉特后旗| 类乌齐县| 金寨县| 黔东| 焦作市| 巴楚县| 乡宁县| 德兴市| 东乡| 宿迁市| 轮台县| 鄂托克前旗| 石阡县| 宝兴县| 靖远县| 太仆寺旗| 视频| 巴青县| 浪卡子县| 万年县| 岳池县| 抚宁县| 淮滨县| 团风县|