国产欧美日韩第一页|日本一二三不卡视频|在线精品小视频,亚洲第一免费播放区,metcn人体亚洲一区,亚洲精品午夜视频

linux-route命令

2016-09-30 20:34:56 6104

Linux系統的route命令用于顯示和操作IP路由表(show / manipulate the IP routing table)。要實(shí)現兩個(gè)不同的子網(wǎng)之間的通信,需要一臺連接兩個(gè)網(wǎng)絡(luò )的路由器,或者同時(shí)位于兩個(gè)網(wǎng)絡(luò )的網(wǎng)關(guān)來(lái)實(shí)現。在Linux系統中,設置路由通常是為了解決以下問(wèn)題:該Linux系統在一個(gè)局域網(wǎng)中,局域網(wǎng)中有一個(gè)網(wǎng)關(guān),能夠讓機器訪(fǎng)?Internet,那么就需要將這臺機器的IP地址設置為L(cháng)inux機器的默認路由。要注意的是,直接在命令行下執行route命令來(lái)添加路由,不會(huì )永久保存,當網(wǎng)卡重啟或者機器重啟之后,該路由就失效了;可以在/etc/rc.local中添加route命令來(lái)保證該路由設置永久有效。

1.命令格式:

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

2.命令功能:

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

3.命?參數:

-c 顯示更多信息

-n 不解析名字

-v 顯示詳細的處理信息

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

-C 顯示路由緩存

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

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

 

add:添加一條新路由。

del:刪除一條路由。

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

-host:目標地址是一個(gè)主機。

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

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

metric:設置?由跳數。


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

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

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

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

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

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


4.使用實(shí)例:

實(shí)例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

說(shuō)明:

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

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

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

Flags標志說(shuō)明:

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

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

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

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

D Dynamically,此路由是動(dòng)態(tài)性地寫(xiě)入

M Modified,此路由是由路由守護程序或導向器動(dòng)態(tài)修改

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


備注:

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


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

命令:

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

說(shuō)明:

增加一條 到達244.0.0.0的路由


實(shí)例3:屏蔽一條路由

命令:

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


說(shuō)明:

增加一條屏蔽的路由,目的地址為 224.x.x.x 將被拒絕


實(shí)例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 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 ~]#


提交成功!非常感謝您的反饋,我們會(huì )繼續努力做到更好!

這條文檔是否有幫助解決問(wèn)題?

非常抱歉未能幫助到您。為了給您提供更好的服務(wù),我們很需要您進(jìn)一步的反饋信息:

在文檔使用中是否遇到以下問(wèn)題:
-->