(轉)linux route命令詳解

linux route命令詳解

原文:https://www.cnblogs.com/lpfuture/p/5857738.html   &&   http://blog.csdn.net/wangyan_hbu/article/details/51013602html

爲了讓設備能訪問另外一個子網,須要在設備裏增長路由到子網絡,通常來講都是爲了能訪問別的子網才設置路由的,好比說,你的主機處於192.168.10.0/24,而你想訪問192.168.20.0/24網的主機,固然你知道一個網關IP,例如192.168.10.1(必須和你主機處於同一子網),那麼,你能夠這樣配置路由。linux

添加路由面試

route add -net 192.168.20.0 netmask 255.255.255.0 gw 192.168.10.1安全

查看路由狀態服務器

route -n網絡

刪除路由post

route del -net 192.168.20.0netmask 255.255.255.0測試

route命令用於顯示和操做IP路由表。要實現兩個不一樣的子網之間的通訊,須要一臺鏈接兩個網絡的路由器,或者同時位於兩個網絡的網關來實現。在Linux系統中,設置路由一般是 爲了解決如下問題:該Linux系統在一個局域網中,局域網中有一個網關,可以讓機器訪問Internet,那麼就須要將這臺機器的IP地址設置爲 Linux機器的默認路由。要注意的是,直接在命令行下執行route命令來添加路由,不會永久保存,當網卡重啓或者機器重啓以後,該路由就失效了;要想永久保存,有以下方法:this

 

1.在/etc/rc.local裏添加
2.在/etc/sysconfig/network裏添加到末尾
3./etc/sysconfig/static-router :
any net x.x.x.x/24 gw y.y.y.yurl


格式:route 
格式:/sbin/route 
用於打印路由表(display the current routing table)。

在非root用戶使用時須要使用完整路徑執行route命令。

命令參數

[root@linux ~]# route [-nee]
[root@linux ~]# route add [-net|-host] [網域或主機] netmask [mask] [gw|dev]
[root@linux ~]# route del [-net|-host] [網域或主機] netmask [mask] [gw|dev]
觀察的參數:
   -n  :不要使用通信協定或主機名稱,直接使用 IP 或 port number;
   -ee :使用更詳細的資訊來顯示
增長 (add) 與刪除 (del) 路由的相關參數:
   -net    :表示後面接的路由爲一個網域;
   -host   :表示後面接的爲鏈接到單部主機的路由;
   netmask :與網域有關,能夠設定 netmask 決定網域的大小;
   gw      :gateway 的簡寫,後續接的是 IP 的數值喔,與 dev 不一樣;
   dev     :若是隻是要指定由那一塊網路卡連線出去,則使用這個設定,後面接 eth0 等

格式:route -n
格式:/sbin/route -n
用於打印路由表,加上-n參數就是在輸出的信息中不打印主機名而直接打印ip地址。

格式:route add default gw {IP-ADDRESS} {INTERFACE-NAME}
用於設置默認路由,其中,
參數{IP-ADDRESS): 用於指定路由器(網關)的IP地址;
參數{INTERFACE-NAME}: 用於指定接口名稱,如eth0。使用/sbin/ifconfig -a能夠顯示全部接口信息。
例:route add default gw mango

格式:route add -net {NETWORK-ADDRESS} netmask {NETMASK} dev {INTERFACE-NAME}
添加到指定網絡的路由規則,其中
參數{NETWORK-ADDRESS}: 用於指定網絡地址
參數{NETMASK}: 用於指定子網掩碼
參數{INTERFACE-NAME}: 用於指定接口名稱,如eth0。
例1:route add -net 192.56.76.0 netmask 255.255.255.0 dev eth0
例2:route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0

格式:route add -net {NETWORK-ADDRESS} netmask {NETMASK} reject
設置到指定網絡爲不可達,避免在鏈接到這個網絡的地址時程序過長時間的等待,直接就知道該網絡不可達。
例:route add -net 10.0.0.0 netmask 255.0.0.0 reject

格式:route del -net {NETWORK-ADDRESS} netmask {NETMASK} dev {INTERFACE-NAME}
格式:route del -net {NETWORK-ADDRESS} netmask {NETMASK} reject
用於刪除路由設置。參數指定的方式與route add類似。

 

輸出詳解

 

route命令輸出的路由表字段含義以下:
    Destination 目標
          The destination network or destination host. 目標網絡或目標主機。

    Gateway 網關
          The gateway address or '*' if none set. 網關地址,若是沒有就顯示星號。

    Genmask 網絡掩碼
          The  netmask  for  the  destination net; '255.255.255.255' for a
          host destination and '0.0.0.0' for the default route.

     Flags:總共有多個旗標,表明的意義以下:                        

         o U (route is up):該路由是啓動的;                       

         o H (target is a host):目標是一部主機 (IP) 而非網域;                       

         o G (use gateway):須要透過外部的主機 (gateway) 來轉遞封包;                       

         o R (reinstate route for dynamic routing):使用動態路由時,恢復路由資訊的旗標;                       

         o D (dynamically installed by daemon or redirect):已經由服務或轉 port 功能設定爲動態路由                       

         o M (modified from routing daemon or redirect):路由已經被修改了;                       

         o !  (reject route):這個路由將不會被接受(用來抵擋不安全的網域!)

         o A (installed by addrconf)

         o C (cache entry)
    Metric 距離、跳數。暫無用。
          The 'distance' to the target (usually counted in  hops).  It  is
          not  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    該路由被使用的次數,能夠粗略估計通向指定網絡地址的網絡流量。
          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等網絡接口名
          Interface to which packets for this route will be sent.

範例一

單純的觀察路由狀態


[root@linux ~]# route -n
Kernel IP routing table
Destination      Gateway         Genmask         Flags  Metric   Ref    Use   Iface
192.168.10.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         192.168.10.30   0.0.0.0                   UG      0           0        0      eth0
[root@linux ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags   Metric   Ref    Use   Iface
192.168.10.0     *               255.255.255.0          U          0           0        0      eth0
169.254.0.0        *               255.255.0.0              U          0           0        0      eth0
default         server.cluster     0.0.0.0                   UG       0           0        0      eth0

此外,觀察一下上面的路由排列順序喔,依序是由小網域(192.168.10.0/24 是 Class C),逐漸到大網域(169.254.0.0/16 Class B) 最後則是預設路由 (0.0.0.0/0.0.0.0)。而後當咱們要判斷某個網路封包應該如何傳送的時候,該封包會經由這個路由的過程來判斷喔!舉例來講,我上頭僅有三個路由,若我有一個傳往 192.168.10.20 的封包要傳遞,那首先會找 192.168.10.0/24 這個網域的路由,找到了!因此直接由 eth0 傳送出去;若是是傳送到 Yahoo 的主機呢? Yahoo 的主機 IP 是 202.43.195.52,我經過判斷 

1)不是 192.168.10.0/24,
      2)不是 169.254.0.0/16 結果到達 

3)0/0  時, OK !傳出去了,透過  eth0  將封包傳給  192.168.10.30 那部  gateway  主機啊!因此說,路由是有順序的。所以當你重複設定多個一樣的路由時,例如在你的主機上的兩張網路卡設定爲相同網域的 IP 時,會出現什麼狀況?會出現以下的狀況: 
Kernel  IP  routing  table 
Destination          Gateway                  Genmask                  Flags  Metric  Ref        Use  Iface
192.168.10.0        0.0.0.0                  255.255.255.0                U          0            0        0     eth0
192.168.10.0        0.0.0.0                  255.255.255.0                U          0            0        0     eth1
也就是說,因爲路由是依照順序來排列與傳送的,因此不論封包是由那個介面  (eth0, eth1)  所接收,都會由上述的 eth0 傳送出去,因此,在一部主機上面設定兩個相同網域的 IP  自己沒有什麼意義!有點畫蛇添足就是了。除非是相似虛擬主機  (Xen, VMware 等軟體 )  所架設的多主機時,纔會有這個必要~

範例二

[root@linux ~]# route del -net 169.254.0.0 netmask 255.255.0.0 dev eth0
# 上面這個動做能夠刪除掉 169.254.0.0/16 這個網域!
# 請注意,在刪除的時候,須要將路由表上面出現的資訊都寫入
# 包括  netmask , dev 等等參數喔!注意注意
[root@linux ~]# route add -net 192.168.100.0 netmask 255.255.255.0 dev eth0
# 透過 route add 來增長一個路由!請注意,這個路由必需要可以與你互通。


# 舉例來講,若是我下達底下的指令就會顯示錯誤:
 route add -net 192.168.200.0 netmask 255.255.255.0 gw 192.168.200.254
# 由於個人環境內僅有 192.168.10.100 這個 IP ,因此不能與 192.168.200.254這個網段直接使用 MAC 互通!這樣說,能夠理解喔!?
[root@linux ~]# route add default gw 192.168.10.30
# 增長預設路由的方法!請注意,只要有一個預設路由就夠了喔!
# 在這個地方若是您隨便設定後,記得使用底下的指令從新設定你的網路
  /etc/init.d/network restart
     若是是要進行路由的刪除與增長,那就得要參考上面的例子了,其實,使用 man route 裏面的資料就很豐富了!仔細查閱一下囉!你只要記得,當出現『SIOCADDRT: Network is unreachable』這個錯誤時,確定是因爲 gw 後面接的 IP 沒法直接與您的網域溝通 (Gateway 並不在你的網域內)。

WINDOWS下的route命令 

簡單的的操做以下,

查看路由狀態:routeprint

只查看ipv4(ipv6)路由狀態:route print-4(-6)

添加路由:route add 目的網絡 mask 子網掩碼 網關——重啓機器或網卡失效

route add 192.168.20.0 mask 255.255.255.0192.168.10.1

添加永久:route -p add 目的網絡 mask子網掩碼網關

route -p add 192.168.20.0 mask 255.255.255.0192.168.10.1

刪除路由:route delete 目的網絡 mask 子網掩碼

route delete 192.168.20.0 mask255.255.255.0

 

考試題一: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命令不一樣功能應用案例。

相關文章
相關標籤/搜索