每(mei)天一個linux命令(53):route命令
Linux系統的route命(ming)(ming)(ming)令(ling)用于(yu)顯(xian)示和操作IP路由(you)表(show / manipulate the IP routing table)。要(yao)實現(xian)(xian)兩(liang)(liang)個(ge)(ge)不同(tong)(tong)的(de)(de)子網(wang)(wang)之間的(de)(de)通信,需(xu)要(yao)一(yi)臺連接(jie)兩(liang)(liang)個(ge)(ge)網(wang)(wang)絡的(de)(de)路由(you)器(qi),或者同(tong)(tong)時(shi)位于(yu)兩(liang)(liang)個(ge)(ge)網(wang)(wang)絡的(de)(de)網(wang)(wang)關(guan)來(lai)實現(xian)(xian)。在(zai)Linux系統(tong)中,設(she)置(zhi)路由(you)通常(chang)是為(wei)了(le)解決以下(xia)問(wen)題:該(gai)(gai)Linux系統(tong)在(zai)一(yi)個(ge)(ge)局域網(wang)(wang)中,局域網(wang)(wang)中有一(yi)個(ge)(ge)網(wang)(wang)關(guan),能夠讓(rang)機(ji)器(qi)訪問(wen)Internet,那么就(jiu)需(xu)要(yao)將這臺機(ji)器(qi)的(de)(de)IP地址設(she)置(zhi)為(wei)Linux機(ji)器(qi)的(de)(de)默認路由(you)。要(yao)注意的(de)(de)是,直接(jie)在(zai)命(ming)(ming)(ming)令(ling)行下(xia)執行route命(ming)(ming)(ming)令(ling)來(lai)添加路由(you),不會永久保存,當(dang)網(wang)(wang)卡重啟或者機(ji)器(qi)重啟之后,該(gai)(gai)路由(you)就(jiu)失效(xiao)了(le);可以在(zai)/etc/rc.local中添加route命(ming)(ming)(ming)令(ling)來(lai)保證該(gai)(gai)路由(you)設(she)置(zhi)永久有效(xiao)。
1.命令格式:
route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]]
2.命(ming)令功(gong)能:
Route命令是用于(yu)操作基于內(nei)核ip路由表(biao),它的主(zhu)要作用(yong)是創建一(yi)個(ge)(ge)靜(jing)態路由讓指定(ding)一(yi)個(ge)(ge)主(zhu)機或者一(yi)個(ge)(ge)網(wang)絡(luo)通過(guo)一(yi)個(ge)(ge)網(wang)絡(luo)接口(kou),如eth0。當使用(yong)"add"或者"del"參數(shu)時(shi),路由表(biao)被修改,如果沒有參數(shu),則顯示路由表(biao)當前的內(nei)容。
3.命(ming)令參數:
-c 顯示(shi)更多信息
-n 不解析名字
-v 顯示詳(xiang)細的(de)處理信息
-F 顯示發送信息
-C 顯示路由緩存
-f 清除所有(you)網(wang)關入(ru)口的(de)路由表。
-p 與 add 命令一起(qi)使(shi)用(yong)時使(shi)路由(you)具(ju)有永(yong)久(jiu)性。
add:添(tian)加一(yi)條新路由(you)。
del:刪除一條(tiao)路(lu)由(you)。
-net:目標(biao)地址是一個網絡。
-host:目標地址是一個主機(ji)。
netmask:當添加一(yi)個網絡(luo)路(lu)由(you)時(shi),需要使用網絡(luo)掩碼。
gw:路由(you)數據(ju)包(bao)通過網關。注(zhu)意,你指定的網關必須能夠達到。
metric:設置(zhi)路(lu)由跳數。
Command 指定您想運行的(de)命令(ling) (Add/Change/Delete/Print)。
Destination 指定(ding)該路(lu)由的(de)網絡目標。
mask Netmask 指定與(yu)網(wang)絡目標(biao)相關的網(wang)絡掩碼(也被稱作子網(wang)掩碼)。
Gateway 指定網(wang)絡(luo)目標定義的地址集和子網(wang)掩碼可以到(dao)達的前進或下一躍點(dian) IP 地址。
metric Metric 為路由(you)指定一個整數(shu)成本(ben)值標(從(cong) 1 至 9999),當在路由(you)表(與轉發(fa)的(de)數(shu)據包目(mu)標地址最匹(pi)配(pei))的(de)多(duo)個路由(you)中進行選(xuan)擇(ze)時可(ke)以使用。
if Interface 為(wei)可(ke)以(yi)訪問目標(biao)的接口(kou)指(zhi)定接口(kou)索(suo)引(yin)。若要獲(huo)得一個接口(kou)列表和(he)它們相(xiang)應的接口(kou)索(suo)引(yin),使用 route print 命(ming)令的顯示功能。可(ke)以(yi)使用十(shi)進制或(huo)十(shi)六(liu)進制值進行接口(kou)索(suo)引(yin)。
4.使用實例:
實(shi)例1:顯示(shi)當前(qian)路(lu)由
命令:
route
route -n
輸出:
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
說明:
第一行表示(shi)主(zhu)機所(suo)在(zai)網絡的(de)地址為192.168.120.0,若數(shu)據傳送目標(biao)是在本局域網內通信,則可直接通過eth0轉發數(shu)據包;
第四行表示(shi)數據傳送目的是訪問(wen)Internet,則由接口eth0,將(jiang)數據包(bao)發(fa)送到網(wang)關192.168.120.240
其(qi)中Flags為路由標志(zhi),標記當(dang)前網絡節點的狀態。
Flags標志(zhi)說明:
U Up表示此路由(you)當前為(wei)啟動狀態
H Host,表示此網關為一主機
G Gateway,表(biao)示此網(wang)關為一路由器(qi)
R Reinstate Route,使用動態路由重(zhong)新(xin)初始化的(de)路由
D Dynamically,此路由(you)是(shi)動態(tai)性地寫入
M Modified,此路(lu)由是由路(lu)由守護程序或導向(xiang)器動態修改(gai)
! 表(biao)示此路由當前為關(guan)閉(bi)狀態(tai)
備(bei)注:
route -n (-n 表(biao)示不解析名字,列出(chu)速度會(hui)比route 快)
實例(li)2:添加網關/設置網關
命令:
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 ~]#
說明:
增(zeng)加一條(tiao) 到達244.0.0.0的路由
實例3:屏蔽一條路由
命令:
route add -net 224.0.0.0 netmask 240.0.0.0 reject
輸(shu)出(chu):
[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
說明:
增加一條(tiao)屏(ping)蔽的路(lu)由,目的地址為 224.x.x.x 將(jiang)被(bei)拒絕
實(shi)例4:刪除(chu)路由記(ji)錄
命(ming)令:
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
輸出:
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:刪除和添(tian)加(jia)設置(zhi)默(mo)認網關(guan)
命令:
route del default gw 192.168.120.240
route add default gw 192.168.120.240
輸出(chu):
[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 ~]#
說明:
關注 熵減黑客 ,一起學習成長
