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