linux route命令详解
考試題一:linux下如何添加路由(百度面試題)
?
以上是原題,老男孩老師翻譯成如下3道題。
?
a.如何用命令行方式給linux機(jī)器添加一個(gè)默認(rèn)網(wǎng)關(guān),假設(shè)網(wǎng)關(guān)地址為10.0.0.254?
b. 192.168.1.0網(wǎng)段,?192.168.1.1網(wǎng)關(guān)的某一服務(wù)器想連入172.16.1.0/24段,該如何添加路由(奇虎360)
c.如果添加一個(gè)主機(jī)路由?
請分別解答。
解答:route?-net 172.16.1.0/24?gw 192.168.1.1
route 命令使用方法:
?
a.缺省網(wǎng)關(guān)路由
?
????默認(rèn)網(wǎng)關(guān)就是數(shù)據(jù)包不匹配任何設(shè)定的路由規(guī)則,最后流經(jīng)的地址關(guān)口!網(wǎng)關(guān)按字面意思就是網(wǎng)絡(luò)的關(guān)口,就相當(dāng)于我們家里房子的門一樣,如果外出就要經(jīng)過房門,數(shù)據(jù)包也是一樣。
?
本題的答案:
?
route del default gw 10.0.0.254
?
解答實(shí)踐:
?
[root@oldboy ~]# route -n?#==>查看路由表,netstat -rn也可以。
?
Kernel IP routing table
?
Destination?????Gateway?????????Genmask?????????Flags Metric Ref????Use Iface
?
10.0.0.0????????0.0.0.0?????????255.255.255.0???U?????0??????0???????0 eth0
?
169.254.0.0?????0.0.0.0?????????255.255.0.0?????U?????0??????0???????0 eth0
?
0.0.0.0?????????10.0.0.254??????0.0.0.0?????????UG????0?????0????????0 eth0
?
#==>這里就是系統(tǒng)的默認(rèn)網(wǎng)關(guān)信息,表示去任何地方(0.0.0.0),都發(fā)給10.0.0.254,因?yàn)槭悄J(rèn)網(wǎng)關(guān),所以,放在了最后一條。路由也是有順序的,如果不符合任何一條規(guī)則就交給默認(rèn)網(wǎng)關(guān)處理。
?
[root@oldboy ~]# route del default gw 10.0.0.254?#==>這個(gè)命令是刪除默認(rèn)的網(wǎng)關(guān)。
?
[root@oldboy ~]# route -n
?
Kernel IP routing table
?
Destination?????Gateway?????????Genmask?????????Flags Metric Ref????Use Iface
?
10.0.0.0????????0.0.0.0?????????255.255.255.0???U?????0??????0???????0 eth0
?
169.254.0.0?????0.0.0.0?????????255.255.0.0?????U?????0??????0???????0 eth0
?
[root@oldboy ~]# route add default gw 10.0.0.254????#==>這個(gè)命令是添加默認(rèn)的網(wǎng)關(guān),也是本題的答案。
?
[root@oldboy ~]# netstat -rn
?
Kernel IP routing table
?
Destination?????Gateway?????????Genmask?????????Flags???MSS Window??irtt Iface
?
10.0.0.0????????0.0.0.0?????????255.255.255.0???U?????????0 0??????????0 eth0
?
169.254.0.0?????0.0.0.0?????????255.255.0.0?????U?????????0 0??????????0 eth0
?
0.0.0.0?????????10.0.0.254??????0.0.0.0?????????UG????0?????0????????0 eth0?#==>又回來了
?
[root@oldboy ~]# route -n
?
Kernel IP routing table
?
Destination?????Gateway?????????Genmask?????????Flags Metric Ref????Use Iface
?
10.0.0.0????????0.0.0.0?????????255.255.255.0???U?????0??????0???????0 eth0
?
169.254.0.0?????0.0.0.0?????????255.255.0.0?????U?????0??????0???????0 eth0
?
0.0.0.0?????????10.0.0.254??????0.0.0.0?????????UG????0?????0????????0 eth0?#這里就是添加的默認(rèn)網(wǎng)關(guān)記錄。
?
特別強(qiáng)調(diào):實(shí)際上route add default gw 10.0.0.254 就相當(dāng)于route add -net 0.0.0.0 netmask 0.0.0.0 gw 10.0.0.254
?
b.網(wǎng)絡(luò)路由:即去往某一網(wǎng)絡(luò)或網(wǎng)段的路由
?
????一般多網(wǎng)段之間互相通信,希望建立一條優(yōu)先路由,而不是通過默認(rèn)網(wǎng)關(guān)時(shí)就可以配置網(wǎng)絡(luò)路由。還是拿房子比喻,你現(xiàn)在不是要出門,而是臥室,衛(wèi)生間,去臥室就要經(jīng)過臥室的門,去衛(wèi)生間也要經(jīng)過衛(wèi)生間的門,這里的臥室和衛(wèi)生間的門就可以認(rèn)為是去往某一網(wǎng)段的路由,而不是默認(rèn)路由(即房子的門。)
?
????實(shí)際工作中會有需求,兩個(gè)不同的內(nèi)部網(wǎng)絡(luò)之間互訪,而不是出網(wǎng)訪問,就是上面例子的情況。
?
????本題的答案:
?
route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1
?
解答實(shí)踐:
?
[root@oldboy ~]# route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1
?
SIOCADDRT:?網(wǎng)絡(luò)不可達(dá)?#==>當(dāng)連不通地址192.168.1.1時(shí),無法添加路由。
?
[root@oldboy ~]# ifconfig eth0:0 192.168.1.1/24 up?#==>添加一個(gè)IP別名用于臨時(shí)測試,如果永久生效最好加雙網(wǎng)卡或?qū)懭氲脚渲梦募?/p>
?
[root@oldboy ~]# ifconfig eth0:0?#==>查看添加的IP別名(網(wǎng)絡(luò)里把這種多IP的方式稱為子接口)
?
eth0:0????Link encap:Ethernet??HWaddr 00:0C:29:65:A4:FD?
?
??????????inet addr:192.168.1.1??Bcast:192.168.1.255??Mask:255.255.255.0
?
??????????UP BROADCAST RUNNING MULTICAST??MTU:1500??Metric:1
?
再來添加去192.168.1.0的數(shù)據(jù)包,交給192.168.1.1處理。
?
[root@oldboy ~]# route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1
?
[root@oldboy ~]# netstat -rn???#==>和route -n很像。
?
Kernel IP routing table
?
Destination?????Gateway?????????Genmask?????????Flags???MSS Window??irtt Iface
?
10.0.0.0????????0.0.0.0?????????255.255.255.0???U?????????0 0??????????0 eth0
?
192.168.1.0?????192.168.1.1?????255.255.255.0???UG????????0 0??????????0 eth0?#==>這就是網(wǎng)絡(luò)路由
?
192.168.1.0?????0.0.0.0?????????255.255.255.0???U?????????0 0?????????0 eth0
?
169.254.0.0?????0.0.0.0?????????255.255.0.0?????U?????????0 0??????????0 eth0
?
0.0.0.0?????????10.0.0.254??????0.0.0.0?????????UG????????0 0??????????0 eth0
?
拓展:其他寫法
?
[root@oldboy ~]# route add -net 192.168.1.0 netmask 255.255.255.0 dev eth0??#==>指定設(shè)備而不是地址。
?
[root@oldboy ~]# route -n
?
Kernel IP routing table
?
Destination?????Gateway?????????Genmask?????????Flags Metric Ref????Use Iface
?
10.0.0.0????????0.0.0.0?????????255.255.255.0???U?????0??????0???????0 eth0
?
192.168.1.0?????0.0.0.0?????????255.255.255.0???U?????0??????0????????0 eth0
?
192.168.1.0?????192.168.1.1?????255.255.255.0???UG????0??????0????????0 eth0
?
192.168.1.0?????0.0.0.0?????????255.255.255.0???U?????0??????0????????0 eth0
?
169.254.0.0?????0.0.0.0?????????255.255.0.0?????U?????0??????0???????0 eth0
?
0.0.0.0?????????10.0.0.254??????0.0.0.0?????????UG????0?????0????????0 eth0
?
[root@oldboy ~]# route del -net 192.168.1.0/24 dev eth0???
?
[root@oldboy ~]# route add -net 192.168.1.0/24 dev eth0???
?
[root@oldboy ~]# route -n
?
Kernel IP routing table
?
Destination?????Gateway?????????Genmask?????????Flags Metric Ref????Use Iface
?
10.0.0.0????????0.0.0.0?????????255.255.255.0???U?????0??????0???????0 eth0
?
192.168.1.0?????0.0.0.0?????????255.255.255.0???U?????0??????0????????0 eth0
?
192.168.1.0?????0.0.0.0?????????255.255.255.0???U?????0??????0????????0 eth0
?
169.254.0.0?????0.0.0.0?????????255.255.0.0?????U?????0??????0???????0 eth0
?
0.0.0.0?????????10.0.0.254??????0.0.0.0?????????UG????0?????0????????0 eth0
?
總結(jié):
?
route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1
?
route add -net 192.168.1.0 netmask 255.255.255.0 dev eth0
?
route add -net 192.168.1.0/24 dev eth0?
?
route del -net 192.168.1.0/24 dev eth0??
?
特別強(qiáng)調(diào):以上配置在重啟網(wǎng)絡(luò)時(shí)都會失效,那么如何讓它永久生效呢?
?
如果要是永久生效,有如下幾種方法:
方法一:
?
vi /etc/sysconfig/network-scripts/route-eth0??#默認(rèn)不存在此文件
?
加入如下內(nèi)容:
?
192.168.1.0/24 via 192.168.1.1
?
提示:寫到配置里,重啟網(wǎng)絡(luò)服務(wù)和重啟系統(tǒng)都會生效!
?
?
?
?
方法二:
?
vi /etc/sysconfig/static-routes??#默認(rèn)不存在此文件
?
加入如下內(nèi)容:
?
any net 192.168.1.0/24 gw 192.168.1.1
?
提示:寫到配置里,重啟網(wǎng)絡(luò)服務(wù)和重啟系統(tǒng)都會生效!
?
?
?
?
方法三:
?
vi /etc/rc.local
?
加入如下內(nèi)容:
?
route add -net 192.168.1.0/24 gw 192.168.1.1
?
PS:?方法一推薦生產(chǎn)環(huán)境使用
?
提示:方法三寫到/etc/rc.local里只在開機(jī)時(shí)加載,當(dāng)手工重啟網(wǎng)絡(luò)后會失效,但是重啟系統(tǒng)后會生效!
?
?
如果是配置默認(rèn)路由網(wǎng)關(guān)可以再網(wǎng)卡配置里:
[root@oldboy ~]# grep GATEWAY /etc/sysconfig/network-scripts/ifcfg-eth0
GATEWAY=10.0.0.254
?
c.主機(jī)路由:就是去往某個(gè)主機(jī)地址如何配置路由
?
/sbin/route add -host 192.168.2.13 dev eth2
?
/sbin/route add -host 202.81.11.91 dev lo
?
例如:keepalived或heartbeat高可用服務(wù)器對之間的使用單獨(dú)網(wǎng)卡接心跳線通信就會用到以上主機(jī)路由。
route命令拓展:
?
刪除一條默認(rèn)路由:
?
route del default gw 10.0.0.254
?
刪除一條靜態(tài)路由:
?
route del?–net?目標(biāo)網(wǎng)絡(luò)?netmask
?
如:route del -net 192.168.1.0/24?或route?del?-net 192.168.1.0 netmask 255.225.255.0
?
刪除一條主機(jī)路由:
?
route?del?-host 192.168.1.10 dev eth0
?
有關(guān)route命令更詳細(xì)的內(nèi)容需要大家執(zhí)行man route查看幫助,并仔細(xì)總結(jié)。
?
有關(guān)此題,我們談下多網(wǎng)段生產(chǎn)環(huán)境網(wǎng)段劃分及路由的解決方案(1000臺機(jī)器劃分網(wǎng)段方案)。我們能感受到route命令不同功能應(yīng)用案例。
總結(jié)
以上是生活随笔為你收集整理的linux route命令详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LINUX ulimit命令
- 下一篇: 试试Linux下的ip命令,ifconf