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. //Vevb.com
- 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.
新聞熱點
疑難解答