介紹
Linux系統(tǒng)中的route命令能夠用于IP路由表的顯示和操作。它的主要作用是創(chuàng)建一個(gè)靜態(tài)路由讓指定一個(gè)主機(jī)或者一個(gè)網(wǎng)絡(luò)通過(guò)一個(gè)網(wǎng)絡(luò)接口,如eth0。當(dāng)使用”add”或者”del”參數(shù)時(shí),路由表被修改,如果沒(méi)有參數(shù),則顯示路由表當(dāng)前的內(nèi)容。在一個(gè)網(wǎng)絡(luò)中,需要一個(gè)路由器來(lái)轉(zhuǎn)發(fā)不同廣播域之間的數(shù)據(jù),或是轉(zhuǎn)發(fā)lan和internet之間的數(shù)據(jù)。有時(shí)我們需要設(shè)定這個(gè)路由器作為linux系統(tǒng)的默認(rèn)路由,那么就可以通過(guò)route命令來(lái)操作。甚至我們也可以用我們的linux系統(tǒng)來(lái)充當(dāng)路由器。
要注意的是:直接在命令行下執(zhí)行route命令來(lái)添加路由,不會(huì)永久保存,當(dāng)網(wǎng)卡重啟或者機(jī)器重啟之后,該路由就失效了;可以在/etc/rc.local中添加route命令來(lái)保證該路由設(shè)置永久有效。當(dāng)然如果加上了-p參數(shù)的話那就會(huì)永久的生效了。
命令格式
1
|
route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway][metric Metric]] [ if Interface]] |
命令參數(shù)
-c 顯示更多信息
-n 不解析名字
-v 顯示詳細(xì)的處理信息
-F 顯示發(fā)送信息
-C 顯示路由緩存
-f 清除所有網(wǎng)關(guān)入口的路由表。
-p 與add 命令一起使用時(shí)使路由具有永久性。
add:添加一條新路由。
del:刪除一條路由。
-net:目標(biāo)地址是一個(gè)網(wǎng)絡(luò)。
-host:目標(biāo)地址是一個(gè)主機(jī)。
netmask:當(dāng)添加一個(gè)網(wǎng)絡(luò)路由時(shí),需要使用網(wǎng)絡(luò)掩碼。
gw:路由數(shù)據(jù)包通過(guò)網(wǎng)關(guān)。注意,你指定的網(wǎng)關(guān)必須能夠達(dá)到。
metric:設(shè)置路由跳數(shù)。
1、Command 指定您想運(yùn)行的命令 (Add/Change/Delete/Print)。
2、Destination 指定該路由的網(wǎng)絡(luò)目標(biāo)。
3、mask Netmask 指定與網(wǎng)絡(luò)目標(biāo)相關(guān)的網(wǎng)絡(luò)掩碼(也被稱作子網(wǎng)掩碼)。
4、Gateway 指定網(wǎng)絡(luò)目標(biāo)定義的地址集和子網(wǎng)掩碼可以到達(dá)的前進(jìn)或下一躍點(diǎn) IP 地址。
5、metric Metric 為路由指定一個(gè)整數(shù)成本值標(biāo)(從 1 至 9999),當(dāng)在路由表(與轉(zhuǎn)發(fā)的數(shù)據(jù)包目標(biāo)地址最匹配)的多個(gè)路由中進(jìn)行選擇時(shí)可以使用。
6、if Interface為可以訪問(wèn)目標(biāo)的接口指定接口索引。若要獲得一個(gè)接口列表和它們相應(yīng)的接口索引,使用 route print 命令的顯示功能??梢允褂檬M(jìn)制或十六進(jìn)
實(shí)例
1 顯示路由信息
1
|
[root@localhost~] # route |
1
2
3
4
5
|
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.40.0 * 255.255.252.0 U 0 0 0 eth0 169.254.0.0 * 255.255.0.0 U 0 0 0 eth0 default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0 |
Flags標(biāo)志說(shuō)明
- U Up表示此路由當(dāng)前為啟動(dòng)狀態(tài)
- H Host,表示此網(wǎng)關(guān)為一主機(jī)
- G Gateway,表示此網(wǎng)關(guān)為一路由器
- R Reinstate Route,使用動(dòng)態(tài)路由重新初始化的路由
- D Dynamically,此路由是動(dòng)態(tài)性地寫入–》什么時(shí)候才會(huì)有動(dòng)態(tài)的路由信息呢?
- M Modified,此路由是由路由守護(hù)程序或?qū)蚱鲃?dòng)態(tài)修改
2 添加一條指向某個(gè)網(wǎng)絡(luò)的路由
1
|
[root@localhost~] # route add -net 10.0.0.0 netmask 255.255.255.0 dev eth0 |
這里是指定這條路由的出口在哪里。-net 10.0.0.0 netmask 255.255.255.0 為指定目標(biāo)網(wǎng)絡(luò)的參數(shù),需要ip地址或地址范圍、子網(wǎng)掩碼用于確定網(wǎng)絡(luò)范圍。
1
|
[root@localhost~] # route |
1
2
3
4
5
6
|
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.0.0.0 * 255.255.255.0 U 0 0 0 eth0 192.168.40.0 * 255.255.252.0 U 0 0 0 eth0 169.254.0.0 * 255.255.0.0 U 0 0 0 eth0 default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0 |
route添加路由都是需要指定目標(biāo)網(wǎng)絡(luò),及路由出口這兩個(gè)參數(shù)。記住加上-p選項(xiàng)能永久添加。
3 添加到某一個(gè)ip的路由
1
2
|
[root@localhost~] # route add -host 192.168.40.1dev eth0 [root@localhost ~] # route |
可以發(fā)現(xiàn)添加的是主機(jī)的話,默認(rèn)是會(huì)幫我們添加一個(gè)全255的子網(wǎng)掩碼,表示子網(wǎng)范圍就只有一個(gè)而已,那就是這臺(tái)主機(jī)啦。
1
2
3
|
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.40.1 * 255.255.255.255 UH 0 0 0 eth0 |
4 屏蔽某一路由
當(dāng)我們不讓系統(tǒng)到達(dá)某個(gè)子網(wǎng)范圍或者某個(gè)主機(jī)是就可以手動(dòng)的來(lái)進(jìn)行屏蔽。
1
|
[root@localhost~] # route add -net 10.10.10.128 netmask 255.255.255.128 reject |
前面部分是一樣的,因?yàn)槲覀兌际鞘謩?dòng)來(lái)添加一個(gè)路由嘛。只是在命令的最后不一樣,我們指定的出口去而是reject(拒絕),也就是拒絕出口。達(dá)到屏蔽的效果。還有看下flags會(huì)顯示一個(gè)!
1
|
[root@localhost~] # route |
1
2
3
4
5
6
7
8
|
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.40.1 * 255.255.255.255 UH 0 0 0 eth0 10.10.10.128 - 255.255.255.128 ! 0 - 0 - 10.0.0.0 * 255.255.255.0 U 0 0 0 eth0 192.168.40.0 * 255.255.252.0 U 0 0 0 eth0 169.254.0.0 * 255.255.0.0 U 0 0 0 eth0 default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0 |
5 刪除路由
1
|
[root@localhost~] # route |
1
2
3
4
5
6
7
8
|
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.40.1 * 255.255.255.255UH 0 0 0 eth0 10.10.10.128 - 255.255.255.128 ! 0 - 0 - 10.0.0.0 * 255.255.255.0 U 0 0 0 eth0 192.168.40.0 * 255.255.252.0 U 0 0 0 eth0 169.254.0.0 * 255.255.0.0 U 0 0 0 eth0 default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0 |
1
|
[root@localhost~] # route del -net 10.10.10.128netmask 255.255.255.128 reject |
1
2
3
4
5
6
7
8
9
|
[root@localhost~] # route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.40.1 * 255.255.255.255UH 0 0 0 eth0 10.0.0.0 * 255.255.255.0 U 0 0 0 eth0 192.168.40.0 * 255.255.252.0 U 0 0 0 eth0 169.254.0.0 * 255.255.0.0 U 0 0 0 eth0 default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0 |
說(shuō)明:刪除路由時(shí),最好是看著路由表上的照樣打進(jìn)去,這樣比較不會(huì)刪錯(cuò)的。
添加刪除默認(rèn)網(wǎng)關(guān)
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@localhost ~] # route add default gw 192.168.40.2 [root@localhost~] # route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.40.1 * 255.255.255.255UH 0 0 0 eth0 10.0.0.0 * 255.255.255.0 U 0 0 0 eth0 192.168.40.0 * 255.255.252.0 U 0 0 0 eth0 169.254.0.0 * 255.255.0.0 U 0 0 0 eth0 default 192.168.40.2 0.0.0.0 UG 0 0 0 eth0 default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0 |
可以看到此處有兩個(gè)默認(rèn)網(wǎng)關(guān),那到底路由會(huì)走哪個(gè)呢?
1
|
[root@localhost~] # route del default gw192.168.40.2 |
1
2
3
4
5
6
7
8
9
|
[root@localhost~] # route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.40.1 * 255.255.255.255UH 0 0 0 eth0 10.0.0.0 * 255.255.255.0 U 0 0 0 eth0 192.168.40.0 * 255.255.252.0 U 0 0 0 eth0 169.254.0.0 * 255.255.0.0 U 0 0 0 eth0 default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0 |
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能有所幫助,如果有疑問(wèn)大家可以留言交流。