linux route命令詳解


linux route命令詳解

 

相關鏈接:https://blog.51cto.com/laodou/2066476

轉自:https://www.cnblogs.com/lpfuture/p/5857738.html

 

考試題一:linux下如何添加路由(百度面試題)

 

以上是原題,老男孩老師翻譯成如下3道題。

 

a.如何用命令行方式給linux機器添加一個默認網關,假設網關地址為10.0.0.254?

b. 192.168.1.0網段, 192.168.1.1網關的某一服務器想連入172.16.1.0/24段,該如何添加路由(奇虎360)

c.如果添加一個主機路由?

請分別解答。

解答:route -net 172.16.1.0/24 gw 192.168.1.1

route 命令使用方法:

 

a.缺省網關路由

 

    默認網關就是數據包不匹配任何設定的路由規則,最后流經的地址關口!網關按字面意思就是網絡的關口,就相當於我們家里房子的門一樣,如果外出就要經過房門,數據包也是一樣。

 

本題的答案:

 

route del default gw 10.0.0.254

 

解答實踐:

 

[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

 

#==>這里就是系統的默認網關信息,表示去任何地方(0.0.0.0),都發給10.0.0.254,因為是默認網關,所以,放在了最后一條。路由也是有順序的,如果不符合任何一條規則就交給默認網關處理。

 

[root@oldboy ~]# route del default gw 10.0.0.254 #==>這個命令是刪除默認的網關。

 

[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    #==>這個命令是添加默認的網關,也是本題的答案。

 

[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 #這里就是添加的默認網關記錄。

 

特別強調:實際上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.網絡路由:即去往某一網絡或網段的路由

 

    一般多網段之間互相通信,希望建立一條優先路由,而不是通過默認網關時就可以配置網絡路由。還是拿房子比喻,你現在不是要出門,而是卧室,衛生間,去卧室就要經過卧室的門,去衛生間也要經過衛生間的門,這里的卧室和衛生間的門就可以認為是去往某一網段的路由,而不是默認路由(即房子的門。)

 

    實際工作中會有需求,兩個不同的內部網絡之間互訪,而不是出網訪問,就是上面例子的情況。

 

    本題的答案:

 

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: 網絡不可達 #==>當連不通地址192.168.1.1時,無法添加路由。

 

[root@oldboy ~]# ifconfig eth0:0 192.168.1.1/24 up #==>添加一個IP別名用於臨時測試,如果永久生效最好加雙網卡或寫入到配置文件。

 

[root@oldboy ~]# ifconfig eth0:0 #==>查看添加的IP別名(網絡里把這種多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的數據包,交給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 #==>這就是網絡路由

 

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  #==>指定設備而不是地址。

 

[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

 

總結:

 

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  

 

特別強調:以上配置在重啟網絡時都會失效,那么如何讓它永久生效呢?

 

如果要是永久生效,有如下幾種方法:

方法一:

 

vi /etc/sysconfig/network-scripts/route-eth0  #默認不存在此文件

 

加入如下內容:

 

192.168.1.0/24 via 192.168.1.1

 

提示:寫到配置里,重啟網絡服務和重啟系統都會生效!

 

 

 

 

方法二:

 

vi /etc/sysconfig/static-routes  #默認不存在此文件

 

加入如下內容:

 

any net 192.168.1.0/24 gw 192.168.1.1

 

提示:寫到配置里,重啟網絡服務和重啟系統都會生效!

 

 

 

 

方法三:

 

vi /etc/rc.local

 

加入如下內容:

 

route add -net 192.168.1.0/24 gw 192.168.1.1

 

PS: 方法一推薦生產環境使用

 

提示:方法三寫到/etc/rc.local里只在開機時加載,當手工重啟網絡后會失效,但是重啟系統后會生效!

 

 

如果是配置默認路由網關可以再網卡配置里:

[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高可用服務器對之間的使用單獨網卡接心跳線通信就會用到以上主機路由。

route命令拓展:

 

刪除一條默認路由:

 

route del default gw 10.0.0.254

 

刪除一條靜態路由:

 

route del –net 目標網絡 netmask

 

如:route del -net 192.168.1.0/24 或route del -net 192.168.1.0 netmask 255.225.255.0

 

刪除一條主機路由:

 

route del -host 192.168.1.10 dev eth0

 

有關route命令更詳細的內容需要大家執行man route查看幫助,並仔細總結。

 

有關此題,我們談下多網段生產環境網段划分及路由的解決方案(1000台機器划分網段方案)。我們能感受到route命令不同功能應用案例。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM