linux ping 不通 route試一試

一、問題的背景:

linux設備連接不上服務器,而且無法ping通!

測試環境linux設備端口IP爲10.10.42.67

測試內網網關IP爲10.10.42.245

測試服務器IP爲192.168.1.245

內網設備==>內網網關==>測試服務器

1)配置完成後,linux設備一直連不上服務器,就先ping一下試試,發現linux設備ping不通內網其他機器和內網網關。

2)就把PC改爲和設備一樣的IP和MAC,接入內網測試環境,發現PC卻可以ping通。所以就想到了是不是linux設備的路由設置問題。使用route命令查看了路由列表,發現缺少了默認路由。

# route  
Destination     Gateway         Genmask Flags Metric Ref    Use Iface  
10.10.42.0     *               255.255.255.0   U     0      0        0 eth0  

3)隨後使用route設置了默認路由“route add default 10.10.42.245”,路由表裏如下,就可以成功連接服務器。

# route  
Destination     Gateway         Genmask       Flags Metric Ref    Use Iface  
default        10.10.42.254     0.0.0.0         UG    0      0     0  eth0  
10.10.42.0     *                255.255.255.0   U     0      0     0  eth0  

二、關於route命令

1. route命令的輸出項說明

Destination 目標網段或者主機
Gateway 網關地址,”*” 表示目標是本主機所屬的網絡,不需要路由
Genmask 網絡掩碼
Flags Flags爲路由標誌,標記當前網絡節點的狀態,如下所示:
  U — Up:表示此路由當前爲啓動狀態
  H — Host:表示此網關是一個主機
  G —Gateway: 表示此網關是一個路由器
 

R — Reinstate Route:

恢復動態路由產生的表項//使用動態路由重新初始化的路由

 

D — Dynamically:

此路由後臺程序動態地安裝//此路由是動態性地寫入

 

M — Modified:

此路由的後臺程序修改//

此路由是由路由守護程序或導向器動態修改

 

! — 拒絕路由//表示此路由當前爲關閉狀態

Metric 路由距離,到達指定網絡所需的中轉數(linux 內核中沒有使用)
Ref 路由項引用次數(linux 內核中沒有使用)
Use 此路由項被路由軟件查找的次數
Iface 該路由表項對應的輸出接口

    Linux系統的route命令用於顯示和操作IP路由表。要實現兩個不同的子網之間的通信,需要一臺連接兩個網絡的路由器,或者同時位於兩個網絡的網關來實現。在Linux系統中,設置路由通常是爲了解決以下問題:該Linux系統在一個局域網中,局域網中有一個網關,能夠讓機器訪問Internet,那麼就需要將這臺機器的IP地址設置爲Linux機器的默認路由。要注意的是,直接在命令行下執行route命令來添加路由,不會永久保存,當網卡重啓或者機器重啓之後,該路由就失效了;可以在/etc/rc.local中添加route命令來保證該路由設置永久有效。

1)命令格式

route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]] 

2)命令功能

    Route命令是用於操作基於內核ip路由表,它的主要作用是創建一個靜態路由讓指定一個主機或者一個網絡通過一個網絡接口,如eth0。當使用"add"或者"del"參數時,路由表被修改,如果沒有參數,則顯示路由表當前的內容。

3)命令參數

-c 顯示更多信息

-n 不解析名字,列出速度會比route 快

-v 顯示詳細的處理信息

-F 顯示發送信息

-C 顯示路由緩存

-f 清除所有網關入口的路由表。 

-p 與 add 命令一起使用時使路由具有永久性。

 

add:添加一條新路由。

del:刪除一條路由。

-net:目標地址是一個網絡。

-host:目標地址是一個主機。

netmask:當添加一個網絡路由時,需要使用網絡掩碼。

gw:路由數據包通過網關。注意,你指定的網關必須能夠達到。

metric:設置路由跳數。

Command 指定您想運行的命令 (Add/Change/Delete/Print)。 

# route  
Destination     Gateway         Genmask       Flags Metric Ref    Use Iface  
default        10.10.42.254     0.0.0.0         UG    0      0     0  eth0  
10.10.42.0     *                255.255.255.0   U     0      0     0  eth0  

Destination 指定該路由的網絡目標。 

mask Netmask 指定與網絡目標相關的網絡掩碼(也被稱作子網掩碼)。 

Gateway 指定網絡目標定義的地址集和子網掩碼可以到達的前進或下一躍點 IP 地址。 

metric Metric 爲路由指定一個整數成本值標(從 1 至 9999),當在路由表(與轉發的數據包目標地址最匹配)的多個路由中進行選擇時可以使用。 

if Interface 爲可以訪問目標的接口指定接口索引。若要獲得一個接口列表和它們相應的接口索引,使用 route print 命令的顯示功能。可以使用十進制或十六進制值進行接口索引。

 

2. 3種路由類型

1)主機路由

        主機路由是路由選擇表中指向單個IP地址或主機名的路由記錄。主機路由的Flags字段爲H。例如,在下面的示例中,本地主機通過IP地址192.168.1.1的路由器到達IP地址爲10.0.0.10的主機。

Destination    Gateway       Genmask Flags     Metric    Ref    Use    Iface
-----------    -------     -------            -----     ------    ---    ---    -----
10.0.0.10     192.168.1.1    255.255.255.255   UH       0    0      0    eth0

    2)網絡路由

        網絡路由是代表主機可以到達的網絡。網絡路由的Flags字段爲N。例如,在下面的示例中,本地主機將發送到網絡192.19.12的數據包轉發到IP地址爲192.168.1.1的路由器。

Destination    Gateway       Genmask Flags    Metric    Ref     Use    Iface
-----------    -------     -------         -----    -----   ---    ---    -----
192.19.12     192.168.1.1    255.255.255.0      UN      0       0     0    eth0

  3)默認路由(本文就是此種路由)

        當主機不能在路由表中查找到目標主機的IP地址或網絡路由時,數據包就被髮送到默認路由(默認網關)上。默認路由的Flags字段爲G。例如,在下面的示例中,默認路由是IP地址爲192.168.1.1的路由器。

Destination    Gateway       Genmask Flags    Metric    Ref     Use    Iface
-----------    -------     -------         -----    -----   ---    ---    -----
192.19.12     192.168.1.1    255.255.255.0      UN      0       0     0    eth0

3. 配置靜態路由

 

設置和查看路由表都可以用 route 命令,設置內核路由表的命令格式是:

# route  [add|del] [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]

其中:

  • add : 添加一條路由規則
  • del : 刪除一條路由規則
  • -net : 目的地址是一個網絡
  • -host : 目的地址是一個主機
  • target : 目的網絡或主機
  • netmask : 目的地址的網絡掩碼
  • gw : 路由數據包通過的網關
  • dev : 爲路由指定的網絡接口

1)添加到主機的路由

# route add -host 192.168.1.2 dev eth0 
# route add -host 10.20.30.148 gw 10.20.30.40     #添加到10.20.30.148的網管

2)添加到網絡的路由

# route add -net 10.20.30.40 netmask 255.255.255.248 eth0   #添加10.20.30.40的網絡
# route add -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41 #添加10.20.30.48的網絡
# route add -net 192.168.1.0/24 eth1

3)添加和刪除默認網關

[root@localhost ~]# route del default gw 192.168.120.240
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
[root@localhost ~]# route add default gw 192.168.120.240
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# 

4)添加網關/設置網關

說明:增加一條 到達244.0.0.0的路由

[root@localhost ~]# route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       *               240.0.0.0       U     0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0

5)屏蔽一條路由

用法說明:增加一條屏蔽的路由,目的地址爲 224.x.x.x 將被拒絕

[root@localhost ~]# route add -net 224.0.0.0 netmask 240.0.0.0 reject
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       -               240.0.0.0       !     0      -        0 -
224.0.0.0       *               240.0.0.0       U     0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0

 

6)刪除路由記錄

[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       -               240.0.0.0       !     0      -        0 -
224.0.0.0       *               240.0.0.0       U     0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# route del -net 224.0.0.0 netmask 240.0.0.0
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       -               240.0.0.0       !     0      -        0 -
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# route del -net 224.0.0.0 netmask 240.0.0.0 reject
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# 

 

 

附錄示例

顯示現在所有路由    
    
  #route -n    
    
  root@Ubuntu:~# route    
    
  Kernel IP routing table    
    
  Destination Gateway Genmask Flags Metric Ref Use Iface    
    
  10.147.9.0 * 255.255.255.0 U 1 0 0 eth0    
    
  192.168.1.0 * 255.255.255.0 U 2 0 0 wlan0    
    
  192.168.122.0 * 255.255.255.0 U 0 0 0 virbr0    
    
  link-local * 255.255.0.0 U 1000 0 0 eth0    
    
  192.168.0.0 192.168.1.1 255.255.0.0 UG 0 0 0 wlan0    
    
  default 10.147.9.1 0.0.0.0 UG 0 0 0 eth0    
    
  root@Ubuntu:~#    
    
  結果是自上而下, 就是說, 哪條在前面, 哪條就有優先, 前面都沒有, 就用最後一條default    
    
  舉例, 添加一條路由(發往192.168.62這個網段的全部要經過網關192.168.1.1)    
    
  route add -net 192.168.62.0 netmask 255.255.255.0 gw 192.168.1.1    
    
  刪除一條路由    
    
  route del -net 192.168.122.0 netmask 255.255.255.0    
    
  刪除的時候不用寫網關    
    
  linux下添加路由的方法:    
    
  一:使用 route 命令添加    
    
  使用route 命令添加的路由,機器重啓或者網卡重啓後路由就失效了,方法:    
    
  //添加到主機的路由    
    
  # route add –host 192.168.168.110 dev eth0    
    
  # route add –host 192.168.168.119 gw 192.168.168.1    
    
  //添加到網絡的路由    
    
  # route add –net IP netmask MASK eth0    
    
  # route add –net IP netmask MASK gw IP    
    
  # route add –net IP/24 eth1    
    
  //添加默認網關    
    
  # route add default gw IP    
    
  //刪除路由    
    
  # route del –host 192.168.168.110 dev eth0    
    
  二:在linux下設置永久路由的方法:    
    
  1.在/etc/rc.local裏添加    
    
  方法:    
    
  route add -net 192.168.3.0/24 dev eth0    
    
  route add -net 192.168.2.0/24 gw 192.168.3.254    
    
  2.在/etc/sysconfig/network裏添加到末尾    
    
  方法:GATEWAY=gw-ip 或者 GATEWAY=gw-dev    
    
  3./etc/sysconfig/static-router :    
    
  any net x.x.x.x/24 gw y.y.y.y    
    
    
------------------------------------------------------------------------------------------    
--  Route命令的正確用法    
使用 Route 命令行工具查看並編輯計算機的 IP 路由表。Route 命令和語法如下所示:    
route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]]    
-f 清除所有網關入口的路由表。      
-p 與 add 命令一起使用時使路由具有永久性。     
Command 指定您想運行的命令 (Add/Change/Delete/Print)。     
Destination 指定該路由的網絡目標。      
mask Netmask 指定與網絡目標相關的網絡掩碼(也被稱作子網掩碼)。      
Gateway 指定網絡目標定義的地址集和子網掩碼可以到達的前進或下一躍點 IP 地址。      
metric Metric 爲路由指定一個整數成本值標(從 1 至 ArrayArrayArrayArray),當在路由表(與轉發的數據包目標地址最匹配)的多個路由中進行選擇時可以使用。      
if Interface 爲可以訪問目標的接口指定接口索引。若要獲得一個接口列表和它們相應的接口索引,使用 route print 命令的顯示功能。可以使用十進制或十六進制值進行接口索引。     
/?  在命令提示符處顯示幫助。      
示例    
若要顯示 IP 路由表的全部內容,請鍵入:    
route print    
若要顯示以 10. 起始的 IP 路由表中的路由,請鍵入:    
route print 10.*    
若要添加帶有 1Array2.168.12.1 默認網關地址的默認路由,請鍵入:    
route add 0.0.0.0 mask 0.0.0.0 1Array2.168.12.1    
若要向帶有 255.255.0.0 子網掩碼和 10.27.0.1 下一躍點地址的 10.41.0.0 目標中添加一個路由,請鍵入:    
route add 10.41.0.0 mask 255.255.0.0 10.27.0.1    
若要向帶有 255.255.0.0 子網掩碼和 10.27.0.1 下一躍點地址的 10.41.0.0 目標中添加一個永久路由,請鍵入:    
route -p add 10.41.0.0 mask 255.255.0.0 10.27.0.1    
若要向帶有 255.255.0.0 子網掩碼、10.27.0.1 下一躍點地址且其成本值標爲 7 的 10.41.0.0 目標中添加一個路由,請鍵入:    
route add 10.41.0.0 mask 255.255.0.0 10.27.0.1 metric 7    
若要向帶有 255.255.0.0 子網掩碼、10.27.0.1 下一躍點地址且使用 0x3 接口索引的 10.41.0.0 目標中添加一個路由,請鍵入:    
route add 10.41.0.0 mask 255.255.0.0 10.27.0.1 if 0x3    
若要刪除到帶有 255.255.0.0 子網掩碼的 10.41.0.0 目標的路由,請鍵入:    
route delete 10.41.0.0 mask 255.255.0.0    
若要刪除以 10. 起始的 IP 路由表中的所有路由,請鍵入:    
route delete 10.*    
若要將帶有 10.41.0.0 目標和 255.255.0.0 子網掩碼的下一躍點地址從 10.27.0.1 修改爲 10.27.0.25,請鍵入:    
route change 10.41.0.0 mask 255.255.0.0 10.27.0.25    
    
-------------------------------------------------------------------------    
  首先,先了解傳統的網絡配置命令:    
  1. 使用ifconfig命令配置並查看網絡接口情況    
  示例1: 配置eth0的IP,同時激活設備:    
  # ifconfig eth0 192.168.4.1 netmask 255.255.255.0 up    
  示例2: 配置eth0別名設備 eth0:1 的IP,並添加路由    
  # ifconfig eth0:1 192.168.4.2    
  # route add –host 192.168.4.2 dev eth0:1    
  示例3:激活(禁用)設備    
  # ifconfig eth0:1 up(down)    
  示例4:查看所有(指定)網絡接口配置    
  # ifconfig (eth0)    
  2. 使用route 命令配置路由表    
  示例1:添加到主機路由    
  # route add –host 192.168.4.2 dev eth0:1    
  # route add –host 192.168.4.1 gw 192.168.4.250    
  示例2:添加到網絡的路由    
  # route add –net IP netmask MASK eth0    
  # route add –net IP netmask MASK gw IP    
  # route add –net IP/24 eth1    
  示例3:添加默認網關    
  # route add default gw IP    
  示例4:刪除路由    
  # route del –host 192.168.4.1 dev eth0:1    
  示例5:查看路由信息    
  # route 或 route -n (-n 表示不解析名字,列出速度會比route 快)    
  3.ARP 管理命令    
  示例1:查看ARP緩存    
  # arp    
  示例2: 添加    
  # arp –s IP MAC    
  示例3: 刪除    
  # arp –d IP    
  4. ip是iproute2軟件包裏面的一個強大的網絡配置工具,它能夠替代一些傳統的網絡管理工具。例如:ifconfig、route等,    
  上面的示例完全可以用下面的ip命令實現,而且ip命令可以實現更多的功能.下面介紹一些示例:    
  4.0 ip命令的語法    
  ip命令的用法如下:    
  ip [OPTIONS] OBJECT [COMMAND [ARGUMENTS]]    
  4.1 ip link set--改變設備的屬性. 縮寫:set、s    
  示例1:up/down 起動/關閉設備。    
  # ip link set dev eth0 up    
  這個等於傳統的 # ifconfig eth0 up(down)    
  示例2:改變設備傳輸隊列的長度。    
  參數:txqueuelen NUMBER或者txqlen NUMBER    
  # ip link set dev eth0 txqueuelen 100    
  示例3:改變網絡設備MTU(最大傳輸單元)的值。    
  # ip link set dev eth0 mtu 1500    
  示例4: 修改網絡設備的MAC地址。    
  參數: address LLADDRESS    
  # ip link set dev eth0 address 00:01:4f:00:15:f1    
  4.2 ip link show--顯示設備屬性. 縮寫:show、list、lst、sh、ls、l    
  -s選項出現兩次或者更多次,ip會輸出更爲詳細的錯誤信息統計。    
  示例:    
  # ip -s -s link ls eth0    
  eth0: mtu 1500 qdisc cbq qlen 100    
  link/ether 00:a0:cc:66:18:78 brd ff:ff:ff:ff:ff:ff    
  RX: bytes packets errors dropped overrun mcast    
  2449949362 2786187 0 0 0 0    
  RX errors: length crc fifo missed    
  0 0 0 0 0    
  TX: bytes packets errors dropped carrier collsns    
  178558497 1783946 332 0 332 35172    
  TX errors: aborted fifo window heartbeat    
  0 0 0 332    
  這個命令等於傳統的 ifconfig eth0    
  5.1 ip address add--添加一個新的協議地址. 縮寫:add、a    
  示例1:爲每個地址設置一個字符串作爲標籤。爲了和Linux-2.0的網絡別名兼容,這個字符串必須以設備名開頭,接着一個冒號,    
  # ip addr add local 192.168.4.1/28 brd + label eth0:1 dev eth0    
  示例2: 在以太網接口eth0上增加一個地址192.168.20.0,掩碼長度爲24位(155.155.155.0),標準廣播地址,標籤爲eth0:Alias:    
  # ip addr add 192.168.4.2/24 brd + dev eth1 label eth1:1    
  這個命令等於傳統的: ifconfig eth1:1 192.168.4.2    
  5.2 ip address delete--刪除一個協議地址. 縮寫:delete、del、d    
  # ip addr del 192.168.4.1/24 brd + dev eth0 label eth0:Alias1    
  5.3 ip address show--顯示協議地址. 縮寫:show、list、lst、sh、ls、l    
  # ip addr ls eth0    
  5.4.ip address flush--清除協議地址. 縮寫:flush、f    
  示例1 : 刪除屬於私網10.0.0.0/8的所有地址:    
  # ip -s -s a f to 10/8    
  示例2 : 取消所有以太網卡的IP地址    
  # ip -4 addr flush label "eth0"    
  6. ip neighbour--neighbour/arp表管理命令    
  縮寫 neighbour、neighbor、neigh、n    
  命令 add、change、replace、delete、fulsh、show(或者list)    
  6.1 ip neighbour add -- 添加一個新的鄰接條目    
  ip neighbour change--修改一個現有的條目    
  ip neighbour replace--替換一個已有的條目    
  縮寫:add、a;change、chg;replace、repl    
  示例1: 在設備eth0上,爲地址10.0.0.3添加一個permanent ARP條目:    
  # ip neigh add 10.0.0.3 lladdr 0:0:0:0:0:1 dev eth0 nud perm    
  示例2:把狀態改爲reachable    
  # ip neigh chg 10.0.0.3 dev eth0 nud reachable    
  6.2.ip neighbour delete--刪除一個鄰接條目    
  示例1:刪除設備eth0上的一個ARP條目10.0.0.3    
  # ip neigh del 10.0.0.3 dev eth0    
  6.3.ip neighbour show--顯示網絡鄰居的信息. 縮寫:show、list、sh、ls    
  示例1: # ip -s n ls 193.233.7.254    
  193.233.7.254. dev eth0 lladdr 00:00:0c:76:3f:85 ref 5 used 12/13/20 nud reachable    
  6.4.ip neighbour flush--清除鄰接條目. 縮寫:flush、f    
  示例1: (-s 可以顯示詳細信息)    
  # ip -s -s n f 193.233.7.254    
  7. 路由表管理    
  7.1.縮寫 route、ro、r    
  7.2.路由表    
  從Linux-2.2開始,內核把路由歸納到許多路由表中,這些表都進行了編號,編號數字的範圍是1到255。另外,    
  爲了方便,還可以在/etc/iproute2/rt_tables中爲路由表命名。    
  默認情況下,所有的路由都會被插入到表main(編號254)中。在進行路由查詢時,內核只使用路由表main。    
  7.3.ip route add -- 添加新路由    
  ip route change -- 修改路由    
  ip route replace -- 替換已有的路由    
  縮寫:add、a;change、chg;replace、repl    
  示例1: 設置到網絡10.0.0/24的路由經過網關193.233.7.65    
  # ip route add 10.0.0/24 via 193.233.7.65    
  示例2: 修改到網絡10.0.0/24的直接路由,使其經過設備dummy    
  # ip route chg 10.0.0/24 dev dummy    
  示例3: 實現鏈路負載平衡.加入缺省多路徑路由,讓ppp0和ppp1分擔負載(注意:scope值並非必需,它只不過是告訴內核,    
  這個路由要經過網關而不是直連的。實際上,如果你知道遠程端點的地址,使用via參數來設置就更好了)。    
  # ip route add default scope global nexthop dev ppp0 nexthop dev ppp1    
  # ip route replace default scope global nexthop dev ppp0 nexthop dev ppp1    
  示例4: 設置NAT路由。在轉發來自192.203.80.144的數據包之前,先進行網絡地址轉換,把這個地址轉換爲193.233.7.83    
  # ip route add nat 192.203.80.142 via 193.233.7.83    
  示例5: 實現數據包級負載平衡,允許把數據包隨機從多個路由發出。weight 可以設置權重.    
  # ip route replace default equalize nexthop via 211.139.218.145 dev eth0 weight 1 nexthop via 211.139.218.145 dev eth1 weight 1    
  7.4.ip route delete-- 刪除路由    
  縮寫:delete、del、d    
  示例1:刪除上一節命令加入的多路徑路由    
  # ip route del default scope global nexthop dev ppp0 nexthop dev ppp1    
  7.5.ip route show -- 列出路由    
  縮寫:show、list、sh、ls、l    
  示例1: 計算使用gated/bgp協議的路由個數    
  # ip route ls proto gated/bgp |wc    
  1413 9891 79010    
  示例2: 計算路由緩存裏面的條數,由於被緩存路由的屬性可能大於一行,以此需要使用-o選項    
  # ip -o route ls cloned |wc    
  159 2543 18707    
  示例3: 列出路由表TABLEID裏面的路由。缺省設置是table main。TABLEID或者是一個真正的路由表ID或者是/etc/iproute2/rt_tables文件定義的字符串,    
  或者是以下的特殊值:    
  all -- 列出所有表的路由;    
  cache -- 列出路由緩存的內容。    
  ip ro ls 193.233.7.82 tab cache    
  示例4: 列出某個路由表的內容    
  # ip route ls table fddi153    
  示例5: 列出默認路由表的內容    
  # ip route ls    
  這個命令等於傳統的: route    
  7.6.ip route flush -- 擦除路由表    
  示例1: 刪除路由表main中的所有網關路由(示例:在路由監控程序掛掉之後):    
  # ip -4 ro flush scope global type unicast    
  示例2:清除所有被克隆出來的IPv6路由:    
  # ip -6 -s -s ro flush cache    
  示例3: 在gated程序掛掉之後,清除所有的BGP路由:    
  # ip -s ro f proto gated/bgp    
  示例4: 清除所有ipv4路由cache    
  # ip route flush cache    
  *** IPv4 routing cache is flushed.    
  7.7 ip route get -- 獲得單個路由 .縮寫:get、g    
  使用這個命令可以獲得到達目的地址的一個路由以及它的確切內容。    
  ip route get命令和ip route show命令執行的操作是不同的。ip route show命令只是顯示現有的路由,而ip route get命令在必要時會派生出新的路由。    
  示例1: 搜索到193.233.7.82的路由    
  # ip route get 193.233.7.82    
  193.233.7.82 dev eth0 src 193.233.7.65 realms inr.ac cache mtu 1500 rtt 300    
  示例2: 搜索目的地址是193.233.7.82,來自193.233.7.82,從eth0設備到達的路由(這條命令會產生一條非常有意思的路由,這是一條到193.233.7.82的迴環路由)    
  # ip r g 193.233.7.82 from 193.233.7.82 iif eth0    
  193.233.7.82 from 193.233.7.82 dev eth0 src 193.233.7.65 realms inr.ac/inr.ac    
  cache     
 mtu 1500 rtt 300 iif eth0    
  8. ip route -- 路由策略數據庫管理命令    
  命令    
  add、delete、show(或者list)    
  注意:策略路由(policy routing)不等於路由策略(rouing policy)。    
  在某些情況下,我們不只是需要通過數據包的目的地址決定路由,可能還需要通過其他一些域:源地址、IP協議、傳輸層端口甚至數據包的負載。    
  這就叫做:策略路由(policy routing)。    
  8.1. ip rule add -- 插入新的規則    
  ip rule delete -- 刪除規則    
  縮寫:add、a;delete、del、d    
  示例1: 通過路由表inr.ruhep路由來自源地址爲192.203.80/24的數據包    
  ip ru add from 192.203.80/24 table inr.ruhep prio 220    
  示例2:把源地址爲193.233.7.83的數據報的源地址轉換爲192.203.80.144,並通過表1進行路由    
  ip ru add from 193.233.7.83 nat 192.203.80.144 table 1 prio 320    
  示例3:刪除無用的缺省規則    
  ip ru del prio 32767    
  8.2. ip rule show -- 列出路由規則    
  縮寫:show、list、sh、ls、l    
  示例1: # ip ru ls    
  0: from all lookup local    
  32762: from 192.168.4.89 lookup fddi153    
  32764: from 192.168.4.88 lookup fddi153    
  32766: from all lookup main    
  32767: from all lookup 253    
  9. ip maddress -- 多播地址管理    
  縮寫:show、list、sh、ls、l    
  9.1.ip maddress show -- 列出多播地址    
  示例1: # ip maddr ls dummy    
  9.2. ip maddress add -- 加入多播地址    
  ip maddress delete -- 刪除多播地址    
  縮寫:add、a;delete、del、d    
  使用這兩個命令,我們可以添加/刪除在網絡接口上監聽的鏈路層多播地址。這個命令只能管理鏈路層地址。    
  示例1: 增加 # ip maddr add 33:33:00:00:00:01 dev dummy    
  示例2: 查看 # ip -O maddr ls dummy    
  2: dummy    
  link 33:33:00:00:00:01 users 2 static    
  link 01:00:5e:00:00:01    
  示例3: 刪除 # ip maddr del 33:33:00:00:00:01 dev dummy    
  10.ip mroute -- 多播路由緩存管理    
  10.1. ip mroute show -- 列出多播路由緩存條目    
  縮寫:show、list、sh、ls、l    
  示例1:查看 # ip mroute ls    
  (193.232.127.6, 224.0.1.39) Iif: unresolved    
  (193.232.244.34, 224.0.1.40) Iif: unresolved    
  (193.233.7.65, 224.66.66.66) Iif: eth0 Oifs: pimreg    
  示例2:查看 # ip -s mr ls 224.66/16    
  (193.233.7.65, 224.66.66.66) Iif: eth0 Oifs: pimreg    
  9383 packets, 300256 bytes    
  11. ip tunnel -- 通道配置    
  縮寫    
  tunnel、tunl    
  11.1.ip tunnel add -- 添加新的通道    
  ip tunnel change -- 修改現有的通道    
  ip tunnel delete -- 刪除一個通道    
  縮寫:add、a;change、chg;delete、del、d    
  示例1:建立一個點對點通道,最大TTL是32    
  # ip tunnel add Cisco mode sit remote 192.31.7.104 local 192.203.80.1 ttl 32    
  11.2.ip tunnel show -- 列出現有的通道    
  縮寫:show、list、sh、ls、l    
  示例1: # ip -s tunl ls Cisco    
  12. ip monitor和rtmon -- 狀態監視    
  ip命令可以用於連續地監視設備、地址和路由的狀態。這個命令選項的格式有點不同,命令選項的名字叫做monitor,接着是操作對象:    
  ip monitor [ file FILE ] [ all | OBJECT-LIST ]    
  示例1: # rtmon file /var/log/rtmon.log    
  示例2: # ip monitor file /var/log/rtmon.log r 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章