Linux route
一、簡(jiǎn)介
Linux系統(tǒng)的route命令用于顯示和操作IP路由表(show / manipulate the IP routing table)。要實(shí)現(xiàn)兩個(gè)不同的子網(wǎng)之間的通信,需要一臺(tái)連接兩個(gè)網(wǎng)絡(luò)的路由器,或者同時(shí)位于兩個(gè)網(wǎng)絡(luò)的網(wǎng)關(guān)來(lái)實(shí)現(xiàn)。在Linux系統(tǒng)中,設(shè)置路由通常是為了解決以下問(wèn)題:該Linux系統(tǒng)在一個(gè)局域網(wǎng)中,局域網(wǎng)中有一個(gè)網(wǎng)關(guān),能夠讓機(jī)器訪問(wèn)Internet,那么就需要將這臺(tái)機(jī)器的IP地址設(shè)置為L(zhǎng)inux機(jī)器的默認(rèn)路由。要注意的是,直接在命令行下執(zhí)行route命令來(lái)添加路由,不會(huì)永久保存,當(dāng)網(wǎng)卡重啟或者機(jī)器重啟之后,該路由就失效了;可以在/etc/rc.local中添加route命令來(lái)保證該路由設(shè)置永久有效。
?
二、路由知識(shí)
1)靜態(tài)路由
靜態(tài)路由是手動(dòng)設(shè)置的路由。 如ip route 192.168.1.1 255.255.255.255 192.168.1.254,這條靜態(tài)路由的含義是把目的地為192.168.1.1的IP的數(shù)據(jù)全部轉(zhuǎn)發(fā)至192.168.1.2542)默認(rèn)路由
默認(rèn)路由是其他路由規(guī)則都沒(méi)有匹配到時(shí),才會(huì)選擇使用的路由。 如:ip route 0.0.0.0 0.0.0.0 192.168.1.1,這條默認(rèn)路由的含義是如果其他路由規(guī)則不匹配,就把數(shù)據(jù)轉(zhuǎn)發(fā)至192.168.1.13)靜態(tài)默認(rèn)路由
一般的默認(rèn)路由都是靜態(tài)的。4)動(dòng)態(tài)默認(rèn)路由
這個(gè)和靜態(tài)的默認(rèn)路由又有什么區(qū)別呢?比如有一個(gè)星形拓?fù)?#xff0c;R1為核心路由器,而剩下的R2,R3為分支,想要讓R1告訴分支路由器,當(dāng)找不到路徑到達(dá)一個(gè)遠(yuǎn)程網(wǎng)絡(luò)時(shí)候,全部通過(guò)核心路由器來(lái)轉(zhuǎn)發(fā)數(shù)據(jù)到遠(yuǎn)程網(wǎng)絡(luò)。但是當(dāng)有新的路由器加入到這個(gè)網(wǎng)絡(luò)拓?fù)涞臅r(shí)候,這個(gè)路由器也能通過(guò)核心路由器發(fā)現(xiàn)核心路由器上的默認(rèn)路由,這時(shí)候的默認(rèn)路由則是動(dòng)態(tài)的(例如:只要是能夠動(dòng)態(tài)得到核心路由器的網(wǎng)絡(luò)的所有的路由器都能有這么一條默認(rèn)路由,如EIGRP 用D*來(lái)表示)?
三、實(shí)例
1)查看當(dāng)前路由
route –n2)添加/屏蔽/刪除一條路由
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0 route add -net 224.0.0.0 netmask 240.0.0.0 reject route del -net 224.0.0.0 netmask 240.0.0.03) 添加/刪除設(shè)置默認(rèn)網(wǎng)關(guān)
route add default gw 192.168.0.1 route del default gw 192.168.0.1?
四、輸出詳解
route命令輸出的路由表字段含義如下:Destination 目標(biāo)The destination network or destination host. 目標(biāo)網(wǎng)絡(luò)或目標(biāo)主機(jī)。Gateway 網(wǎng)關(guān)The gateway address or '*' if none set. 網(wǎng)關(guān)地址,如果沒(méi)有就顯示星號(hào)。Genmask 網(wǎng)絡(luò)掩碼The netmask for the destination net; '255.255.255.255' for ahost destination and '0.0.0.0' for the default route.Flags:總共有多個(gè)旗標(biāo),代表的意義如下: o U (route is up):該路由是啟動(dòng)的; o H (target is a host):目標(biāo)是一部主機(jī) (IP) 而非網(wǎng)域; o G (use gateway):需要透過(guò)外部的主機(jī) (gateway) 來(lái)轉(zhuǎn)遞封包; o R (reinstate route for dynamic routing):使用動(dòng)態(tài)路由時(shí),恢復(fù)路由資訊的旗標(biāo); o D (dynamically installed by daemon or redirect):已經(jīng)由服務(wù)或轉(zhuǎn) port 功能設(shè)定為動(dòng)態(tài)路由 o M (modified from routing daemon or redirect):路由已經(jīng)被修改了; o ! (reject route):這個(gè)路由將不會(huì)被接受(用來(lái)抵擋不安全的網(wǎng)域!)o A (installed by addrconf)o C (cache entry)Metric 距離、跳數(shù)。暫無(wú)用。The 'distance' to the target (usually counted in hops). It isnot used by recent kernels, but may be needed by routing dae-mons.Ref 不用管,恒為0。Number of references to this route. (Not used in the Linux ker-nel.)Use 該路由被使用的次數(shù),可以粗略估計(jì)通向指定網(wǎng)絡(luò)地址的網(wǎng)絡(luò)流量。Count of lookups for the route. Depending on the use of -F and-C this will be either route cache misses (-F) or hits (-C).Iface 接口,即eth0,eth0等網(wǎng)絡(luò)接口名Interface to which packets for this route will be sent.?
五、常見(jiàn)錯(cuò)誤
1)route: netmask doesn't match route address
linux-rd4x:/ # route add -net 8.8.8.8 netmask 255.255.255.0 dev eth0 route: netmask doesn't match route address提示:
隨意添加一條路由信息時(shí),報(bào)錯(cuò),經(jīng)過(guò)嘗試,Destination IP必須和掩碼相對(duì)應(yīng), 即IP:8.8.8.8 & 255.255.255.0(邏輯與操作) 才是真正的DestinationIP,否則會(huì)報(bào)錯(cuò)。2)SIOCADDRT: 沒(méi)有那個(gè)進(jìn)程(No Such Process)
route add -net 192.168.1.0 netmask 255.255.255.0 SIOCADDRT: 沒(méi)有那個(gè)設(shè)備提示:
在添加一個(gè)新的網(wǎng)段時(shí),它的出口地址應(yīng)該是當(dāng)前ip routing table表中的當(dāng)前有的地址,否則會(huì)出現(xiàn)這個(gè)錯(cuò)誤:SIOCADDRT: 沒(méi)有那個(gè)進(jìn)程。?
參考:http://www.cnblogs.com/peida/archive/2013/03/05/2943698.html
轉(zhuǎn)載于:https://www.cnblogs.com/274914765qq/p/5246863.html
總結(jié)
以上是生活随笔為你收集整理的Linux route的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ASP.NET之.NET FrameWo
- 下一篇: 《Linux内核设计与实现》课程学习重点