linux中route命令的使用詳解

來源:互聯網
上載者:User

route命令用於顯示和操作IP路由表。要實現兩個不同的子網之間的通訊,需要一台串連兩個網路的路由器,或者同時位於兩個網路的網關來實現。在Linux系統中,設定路由通常是 為瞭解決以下問題:該Linux系統在一個區域網路中,區域網路中有一個網關,能夠讓機器訪問Internet,那麼就需要將這台機器的IP地址設定為 Linux機器的預設路由。

作用

route命令用於查看和配置linux核心路由表,也就是用來查看和配置linux的靜態路由表。

描述

route命令操作基於linux核心路由表,它的主要作用是建立一個靜態路由讓指定的一個主機或者一個網路通過一個網路介面,如eth0。當使用”add”或者”del”參數時,路由表被修改,如果沒有參數,則顯示路由表當前的內容。

命令格式

route命令格式如下:

route [-nee]
route add [-net|-host] [網路或主機] netmask [mask] [gw|dev]
route del [-net|-host] [網路或主機] netmask [mask] [gw|dev]

要注意的是,直接在命令列下執行route命令來添加路由,不會永久儲存,當網卡重啟或者機器重啟之後,該路由就失效了;要想永久儲存,有如下方法:

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

格式: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 裡面的資料就很豐富了!仔細查閱一下?櫻∧闃灰?塹茫?背魷幀?IOCADDRT: Network is unreachable』這個錯誤時,肯定是由於 gw 後面接的 IP 無法直接與您的網域溝通 (Gateway 並不在你的網域內)。
我們通過幾個例子來說明如何使用route命令:

routeadd-net127.0.0.0

這個命令將向路由表中添加一個指定地址或者網路的路由。注意此時網路為A類地址,掩碼被設定為255.0.0.0,這個新添加的條目被串連到lo裝置上。

routeadd-netxxx.xxx.xxx.xxxnetmask255.255.255.0deveth0

這個命令為IP地址為xxx.xxx.xxx.xxx的主機增加一個路由,它的網路遮罩被設定為255.255.255.0。

routedel-netxxx.xxx.xxx.xxx

此命令將刪除xxx.xxx.xxx.xxx這個網路的路由。

使用route命令還可以很方便地對整個網路的路由資訊進行管理,其輸出結果是網路的路由表。如下所示:

[root@lee/root]#route
KernelIProutingtable
DestinationGatewayGenmaskFlagsMetricRefUseIface
10.10.8.224*255.255.255.255UH000eth0
10.10.8.0*255.255.255.0U000eth0
127.0.0.0*255.0.0.0U000lo
defaultdgc8.njupt.edu0.0.0.0UG000eth0
defaultdgc8.njupt.edu0.0.0.0UG100eth0
[root@lee/root]#

輸出結果中各個欄位的含義是:

·Destination表示路由的目標IP地址。

·Gateway表示網關使用的主機名稱或者是IP地址。上面輸出的"*"表示沒有網關。

·Genmask表示路由的網路遮罩。

·Flags是表示路由的標誌。可用的標誌及其意義是:U表示路由在啟動,H表示target是一台主機,G表示使用網關,R表示對動態路由進行複位設定;D表示動態安裝路由,M表示修改路由,!表示拒絕路由。

·Metric表示路由的單位開銷量。

·Ref表示依賴本路由現狀的其它路由數目。

·Use表示路由表條目被使用的數目。

·Iface表示路由所發送的包的目的網路。

通過查看這些輸出資訊,我們就可以方便地管理網路的路由表了。

路由表欄位含義

[root@www ~]# route -n

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
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     1002   0        0 eth0
0.0.0.0         192.168.1.254   0.0.0.0         UG    0      0        0 eth0

[root@www ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     *               255.255.255.0   U     0      0        0 eth0
link-local      *               255.255.0.0     U     1002   0        0 eth0
default         192.168.1.254   0.0.0.0         UG    0      0        0 eth0
 Destination 目標

The destination network or destination host. 目標網路或目標主機(有-n參數時,只顯示IP地址,否則顯示主機名稱,如上面的default)。

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. 即,”255.255.255.255″表示一個主機。”0.0.0.0″表示網關。

 Flags  Possible flags include 標誌,常用的是U和G。

              U (route is up) 路由啟用
              H (target is a host) 目標是主機
              G (use gateway) 使用網關
              R (reinstate route for dynamic routing)
              D (dynamically installed by daemon or redirect)
              M (modified from routing daemon or redirect)
              A (installed by addrconf)
              C (cache entry)
              !  (reject route)
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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.