1. 程式人生 > >linux route命令的使用詳解

linux route命令的使用詳解

使用下面的 route 命令可以檢視 Linux 核心路由表。

  1. # route
  2. Destination     Gateway         Genmask Flags Metric Ref    Use Iface  
  3. 192.168.0.0     *               255.255.255.0   U     0      0        0 eth0  
  4. 169.254.0.0     *               255.255.0.0     U     0      0        0 eth0  
  5. default         192.168.0.1     0.0.0.0         UG    0      0        0 eth0 

route 命令的輸出項說明

輸出項 說明
Destination 目標網段或者主機
Gateway 閘道器地址,”*” 表示目標是本主機所屬的網路,不需要路由
Genmask 網路掩碼
Flags 標記。一些可能的標記如下:
U — 路由是活動的
H — 目標是一個主機
G — 路由指向閘道器
R — 恢復動態路由產生的表項
D — 由路由的後臺程式動態地安裝
M — 由路由的後臺程式修改
! — 拒絕路由
Metric 路由距離,到達指定網路所需的中轉數(linux 核心中沒有使用)
Ref 路由項引用次數(linux 核心中沒有使用)
Use 此路由項被路由軟體查詢的次數
Iface 該路由表項對應的輸出介面

主機路由

主機路由是路由選擇表中指向單個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

網路路由

網路路由是代表主機可以到達的網路。網路路由的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

預設路由

當主機不能在路由表中查詢到目標主機的IP地址或網路路由時,資料包就被髮送到預設路由(預設閘道器)上。預設路由的Flags欄位為G。例如,在下面的示例中,預設路由是IP地址為192.168.1.1的路由器。

Destination    Gateway       Genmask Flags     Metric    Ref    Use    Iface
-----------    -------     ------- -----      ------    ---    ---    -----
default       192.168.1.1     0.0.0.0    UG       0        0     0    eth0

設定和檢視路由表都可以用 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 
  2. # route add -host 10.20.30.148 gw 10.20.30.40     #新增到10.20.30.148的網管

新增到網路的路由

  1. # route add -net 10.20.30.40 netmask 255.255.255.248 eth0   #新增10.20.30.40的網路
  2. # route add -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41 #新增10.20.30.48的網路
  3. # route add -net 192.168.1.0/24 eth1

新增預設路由

  1. # route add default gw 192.168.1.1

刪除路由

  1. # route del -host 192.168.1.2 dev eth0:0
  2. # route del -host 10.20.30.148 gw 10.20.30.40
  3. # route del -net 10.20.30.40 netmask 255.255.255.248 eth0
  4. # route del -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41
  5. # route del -net 192.168.1.0/24 eth1
  6. # route del default gw 192.168.1.1

設定包轉發

在 CentOS 中預設的核心配置已經包含了路由功能,但預設並沒有在系統啟動時啟用此功能。開啟 Linux 的路由功能可以通過調整核心的網路引數來實現。要配置和調整核心引數可以使用 sysctl 命令。例如:要開啟 Linux 核心的資料包轉發功能可以使用如下的命令。

  1. # sysctl -w net.ipv4.ip_forward=1

這樣設定之後,當前系統就能實現包轉發,但下次啟動計算機時將失效。為了使在下次啟動計算機時仍然有效,需要將下面的行寫入配置檔案/etc/sysctl.conf。

  1. # vi /etc/sysctl.conf
  2. net.ipv4.ip_forward = 1  

使用者還可以使用如下的命令檢視當前系統是否支援包轉發。

  1. # sysctl net.ipv4.ip_forward

route 命令:

Linux系統的route命令用於顯示和操作IP路由表(show / manipulate the IP routing table)。要實現兩個不同的子網之間的通訊,需要一臺連線兩個網路的路由器,或者同時位於兩個網路的閘道器來實現。在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 不解析名字

-v 顯示詳細的處理資訊

-F 顯示傳送資訊

-C 顯示路由快取

-f 清除所有閘道器入口的路由表。 

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

add:新增一條新路由。

del:刪除一條路由。

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

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

netmask:當新增一個網路路由時,需要使用網路掩碼。

gw:路由資料包通過閘道器。注意,你指定的閘道器必須能夠達到。

metric:設定路由跳數。

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

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

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

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

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

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

4.使用例項:

例項1:顯示當前路由

命令:

route

route -n

輸出:

  1. [[email protected] ~]# route  
  2. Kernel IP routing table  
  3. Destination     Gateway         Genmask         Flags Metric Ref    Use Iface  
  4. 192.168.120.0   *               255.255.255.0   U     0      0        0 eth0  
  5. e192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0  
  6. 10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0  
  7. default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0  
  8. [[email protected] ~]# route -n  
  9. Kernel IP routing table  
  10. Destination     Gateway         Genmask         Flags Metric Ref    Use Iface  
  11. 192.168.120.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0  
  12. 192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0  
  13. 10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0  
  14. 0.0.0.0         192.168.120.240 0.0.0.0         UG    0      0        0 eth0  

說明:

第一行表示主機所在網路的地址為192.168.120.0,若資料傳送目標是在本區域網內通訊,則可直接通過eth0轉發資料包;

行表示資料傳送目的是訪問Internet,則由介面eth0,將資料包傳送到閘道器192.168.120.240

其中Flags為路由標誌,標記當前網路節點的狀態。

Flags標誌說明

U Up表示此路由當前為啟動狀態

H Host,表示此閘道器為一主機

G Gateway,表示此閘道器為一路由器

R Reinstate Route,使用動態路由重新初始化的路由

D Dynamically,此路由是動態性地寫入

M Modified,此路由是由路由守護程式或導向器動態修改

! 表示此路由當前為關閉狀態

備註:

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

例項2:新增閘道器/設定閘道器

命令:

route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0

輸出:

  1. [[email protected] ~]# route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0  
  2. [[email protected] ~]# route  
  3. Kernel IP routing table  
  4. Destination     Gateway         Genmask         Flags Metric Ref    Use Iface  
  5. 192.168.120.0   *               255.255.255.0   U     0      0        0 eth0  
  6. 192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0  
  7. 10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0  
  8. 224.0.0.0       *               240.0.0.0       U     0      0        0 eth0  
  9. default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0  

[[email protected] ~]# 

說明:

增加一條 到達244.0.0.0的路由

例項3:遮蔽一條路由

命令:

route add -net 224.0.0.0 netmask 240.0.0.0 reject

輸出:

  1. [[email protected] ~]# route add -net 224.0.0.0 netmask 240.0.0.0 reject  
  2. [[email protected] ~]# route  
  3. Kernel IP routing table  
  4. Destination     Gateway         Genmask         Flags Metric Ref    Use Iface  
  5. 192.168.120.0   *               255.255.255.0   U     0      0        0 eth0  
  6. 192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0  
  7. 10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0  
  8. 224.0.0.0       -               240.0.0.0       !     0      -        0 -  
  9. 224.0.0.0       *               240.0.0.0       U     0      0        0 eth0  
  10. default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0  

說明:

增加一條遮蔽的路由目的地址為 224.x.x.x 將被拒絕

例項4:刪除路由記錄

命令:

route del -net 224.0.0.0 netmask 240.0.0.0

route del -net 224.0.0.0 netmask 240.0.0.0 reject

輸出:

  1. [[email protected] ~]# route  
  2. Kernel IP routing table  
  3. Destination     Gateway         Genmask         Flags Metric Ref    Use Iface  
  4. 192.168.120.0   *               255.255.255.0   U     0      0        0 eth0  
  5. 192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0  
  6. 10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0  
  7. 224.0.0.0       -               240.0.0.0       !     0      -        0 -  
  8. 224.0.0.0       *               240.0.0.0       U     0      0        0 eth0  
  9. default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0  
  10. [[email protected] ~]# route del -net 224.0.0.0 netmask 240.0.0.0  
  11. [[email protected] ~]# route  
  12. Kernel IP routing table  
  13. Destination     Gateway         Genmask         Flags Metric Ref    Use Iface  
  14. 192.168.120.0   *               255.255.255.0   U     0      0        0 eth0  
  15. 192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0  
  16. 10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0  
  17. 224.0.0.0       -               240.0.0.0       !     0      -        0 -  
  18. default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0  
  19. [[email protected] ~]# route del -net 224.0.0.0 netmask 240.0.0.0 reject  
  20. [[email protected] ~]# route  
  21. Kernel IP routing table  
  22. Destination     Gateway         Genmask         Flags Metric Ref    Use Iface  
  23. 192.168.120.0   *               255.255.255.0   U     0      0        0 eth0  
  24. 192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0  
  25. 10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0  
  26. default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0  
  27. [[email protected] ~]#   

說明:

例項5:刪除和新增設定預設閘道器

命令:

route del default gw 192.168.120.240

route add default gw 192.168.120.240

輸出:

  1. [[email protected] ~]# route del default gw 192.168.120.240  
  2. [[email protected] ~]# route  
  3. Kernel IP routing table  
  4. Destination     Gateway         Genmask         Flags Metric Ref    Use Iface  
  5. 192.168.120.0   *               255.255.255.0   U     0      0        0 eth0  
  6. 192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0  
  7. 10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0  
  8. [[email protected] ~]# route add default gw 192.168.120.240  
  9. [[email protected] ~]# route  
  10. Kernel IP routing table  
  11. Destination     Gateway         Genmask         Flags Metric Ref    Use Iface  
  12. 192.168.120.0   *               255.255.255.0   U     0      0        0 eth0  
  13. 192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0  
  14. 10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0  
  15. default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0  
  16. [[email protected] ~]#   


  1. 顯示現在所有路由    
  2.   #route -n    
  3.   [email protected]:~# route    
  4.   Kernel IP routing table    
  5.   Destination Gateway Genmask Flags Metric Ref Use Iface    
  6.   10.147.9.0 * 255.255.255.0 U 1 0 0 eth0    
  7.   192.168.1.0 * 255.255.255.0 U 2 0 0 wlan0    
  8.   192.168.122.0 * 255.255.255.0 U 0 0 0 virbr0    
  9.   link-local * 255.255.0.0 U 1000 0 0 eth0    
  10.   192.168.0.0 192.168.1.1 255.255.0.0 UG 0 0 0 wlan0    
  11.   default 10.147.9.1 0.0.0.0 UG 0 0 0 eth0    
  12.   [email protected]:~#    
  13.   結果是自上而下, 就是說, 哪條在前面, 哪條就有優先, 前面都沒有, 就用最後一條default
  14.   舉例, 新增一條路由(發往192.168.62這個網段的全部要經過閘道器192.168.1.1)    
  15.   route add -net 192.168.62.0 netmask 255.255.255.0 gw 192.168.1.1    
  16.   刪除一條路由    
  17.   route del -net 192.168.122.0 netmask 255.255.255.0    
  18.   刪除的時候不用寫閘道器    
  19.   linux下新增路由的方法:    
  20.   一:使用 route 命令新增    
  21.   使用route 命令新增的路由,機器重啟或者網絡卡重啟後路由就失效了,方法:    
  22.   //新增到主機的路由  
  23.   # route add –host 192.168.168.110 dev eth0    
  24.   # route add –host 192.168.168.119 gw 192.168.168.1    
  25.   //新增到網路的路由  
  26.   # route add –net IP netmask MASK eth0    
  27.   # route add –net IP netmask MASK gw IP    
  28.   # route add –net IP/24 eth1    
  29.   //新增預設閘道器  
  30.   # route add default gw IP    
  31.   //刪除路由  
  32.   # route del –host 192.168.168.110 dev eth0    
  33.   二:在linux下設定永久路由的方法:    
  34.   1.在/etc/rc.local裡新增    
  35.   方法:    
  36.   route add -net 192.168.3.0/24 dev eth0    
  37.   route add -net 192.168.2.0/24 gw 192.168.3.254    
  38.   2.在/etc/sysconfig/network裡新增到末尾    
  39.   方法:GATEWAY=gw-ip 或者 GATEWAY=gw-dev    
  40.   3./etc/sysconfig/static-router :    
  41.   any net x.x.x.x/24 gw y.y.y.y    
  42. ------------------------------------------------------------------------------------------    
  43. --  Route命令的正確用法    
  44. 使用 Route 命令列工具檢視並編輯計算機的 IP 路由表。Route 命令和語法如下所示:    
  45. route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]]    
  46. -f 清除所有閘道器入口的路由表。      
  47. -p 與 add 命令一起使用時使路由具有永久性。     
  48. Command 指定您想執行的命令 (Add/Change/Delete/Print)。     
  49. Destination 指定該路由的網路目標。      
  50. mask Netmask 指定與網路目標相關的網路掩碼(也被稱作子網掩碼)。      
  51. Gateway 指定網路目標定義的地址集和子網掩碼可以到達的前進或下一躍點 IP 地址。      
  52. metric Metric 為路由指定一個整數成本值標(從 1 至 ArrayArrayArrayArray),當在路由表(與轉發的資料包目標地址最匹配)的多個路由中進行選擇時可以使用。      
  53. if Interface 為可以訪問目標的介面指定介面索引。若要獲得一個介面列表和它們相應的介面索引,使用 route print 命令的顯示功能。可以使用十進位制或十六進位制值進行介面索引。     
  54. /?  在命令提示符處顯示幫助。      
  55. 示例    
  56. 若要顯示 IP 路由表的全部內容,請鍵入:    
  57. route print    
  58. 若要顯示以 10. 起始的 IP 路由表中的路由,請鍵入:    
  59. route print 10.*    
  60. 若要新增帶有 1Array2.168.12.1 預設閘道器地址的預設路由,請鍵入:    
  61. route add 0.0.0.0 mask 0.0.0.0 1Array2.168.12.1    
  62. 若要向帶有 255.255.0.0 子網掩碼和 10.27.0.1 下一躍點地址的 10.41.0.0 目標中新增一個路由,請鍵入:    
  63. route add 10.41.0.0 mask 255.255.0.0 10.27.0.1    
  64. 若要向帶有 255.255.0.0 子網掩碼和 10.27.0.1 下一躍點地址的 10.41.0.0 目標中新增一個永久路由,請鍵入:    
  65. route -p add 10.41.0.0 mask 255.255.0.0 10.27.0.1    
  66. 若要向帶有 255.255.0.0 子網掩碼、10.27.0.1 下一躍點地址且其成本值標為 7 的 10.41.0.0 目標中新增一個路由,請鍵入:    
  67. route add 10.41.0.0 mask 255.255.0.0 10.27.0.1 metric 7    
  68. 若要向帶有 255.255.0.0 子網掩碼、10.27.0.1 下一躍點地址且使用 0x3 介面索引的 10.41.0.0 目標中新增一個路由,請鍵入:    
  69. route add 10.41.0.0 mask 255.255.0.0 10.27.0.1 if 0x3    
  70. 若要刪除到帶有 255.255.0.0 子網掩碼的 10.41.0.0 目標的路由,請鍵入:    
  71. route delete 10.41.0.0 mask 255.255.0.0    
  72. 若要刪除以 10. 起始的 IP 路由表中的所有路由,請鍵入:    
  73. route delete 10.*    
  74. 若要將帶有 10.41.0.0 目標和 255.255.0.0 子網掩碼的下一躍點地址從 10.27.0.1 修改為 10.27.0.25,請鍵入:    
  75. route change 10.41.0.0 mask 255.255.0.0 10.27.0.25    
  76. -------------------------------------------------------------------------    
  77.   首先,先了解傳統的網路配置命令:    
  78.   1. 使用ifconfig命令配置並檢視網路介面情況    
  79.   示例1: 配置eth0的IP,同時啟用裝置:    
  80.   # ifconfig eth0 192.168.4.1 netmask 255.255.255.0 up    
  81.   示例2: 配置eth0別名裝置 eth0:1 的IP,並新增路由    
  82.   # ifconfig eth0:1 192.168.4.2    
  83.   # route add –host 192.168.4.2 dev eth0:1    
  84.   示例3:啟用(禁用)裝置    
  85.   # ifconfig eth0:1 up(down)    
  86.   示例4:檢視所有(指定)網路介面配置    
  87.   # ifconfig (eth0)    
  88.   2. 使用route 命令配置路由表    
  89.   示例1:新增到主機路由    
  90.   # route add –host 192.168.4.2 dev eth0:1    
  91.   # route add –host 192.168.4.1 gw 192.168.4.250    
  92.   示例2:新增到網路的路由    
  93.   # route add –net IP netmask MASK eth0    
  94.   # route add –net IP netmask MASK gw IP    
  95.   # route add –net IP/24 eth1    
  96.   示例3:新增預設閘道器    
  97.   # route add default gw IP    
  98.   示例4:刪除路由    
  99.   # route del –host 192.168.4.1 dev eth0:1    
  100.   示例5:檢視路由資訊    
  101.   # route 或 route -n (-n 表示不解析名字,列出速度會比route 快)    
  102.   3.ARP 管理命令    
  103.   示例1:檢視ARP快取    
  104.   # arp    
  105.   示例2: 新增    
  106.   # arp –s IP MAC    
  107.   示例3: 刪除    
  108.   # arp –d IP    
  109.   4. ip是iproute2軟體包裡面的一個強大的網路配置工具,它能夠替代一些傳統的網路管理工具。例如:ifconfig、route等,    
  110.   上面的示例完全可以用下面的ip命令實現,而且ip命令可以實現更多的功能.下面介紹一些示例:    
  111.   4.0 ip命令的語法    
  112.   ip命令的用法如下:    
  113.   ip [OPTIONS] OBJECT [COMMAND [ARGUMENTS]]    
  114.   4.1 ip link set--改變裝置的屬性. 縮寫:set、s    
  115.   示例1:up/down 起動/關閉裝置。    
  116.   # ip link set dev eth0 up    
  117.   這個等於傳統的 # ifconfig eth0 up(down)    
  118.   示例2:改變裝置傳輸佇列的長度。    
  119.   引數:txqueuelen NUMBER或者txqlen NUMBER    
  120.   # ip link set dev eth0 txqueuelen 100    
  121.   示例3:改變網路裝置MTU(最大傳輸單元)的值。    
  122.   # ip link set dev eth0 mtu 1500    
  123.   示例4: 修改網路裝置的MAC地址。    
  124.   引數: address LLADDRESS    
  125.   # ip link set dev eth0 address 00:01:4f:00:15:f1    
  126.   4.2 ip link show--顯示裝置屬性. 縮寫:show、list、lst、sh、ls、l    
  127.   -s選項出現兩次或者更多次,ip會輸出更為詳細的錯誤資訊統計。    
  128.   示例:    
  129.   # ip -s -s link ls eth0    
  130.   eth0: mtu 1500 qdisc cbq qlen 100    
  131.   link/ether 00:a0:cc:66:18:78 brd ff:ff:ff:ff:ff:ff    
  132.   RX: bytes packets errors dropped overrun mcast    
  133.   2449949362 2786187 0 0 0 0    
  134.   RX errors: length crc fifo missed    
  135.   0 0 0 0 0    
  136.   TX: bytes packets errors dropped carrier collsns    
  137.   178558497 1783946 332 0 332 35172    
  138.   TX errors: aborted fifo window heartbeat    
  139.   0 0 0 332    
  140.   這個命令等於傳統的 ifconfig eth0    
  141.   5.1 ip address add--新增一個新的協議地址. 縮寫:add、a    
  142.   示例1:為每個地址設定一個字串作為標籤。為了和Linux-2.0的網路別名相容,這個字串必須以裝置名開頭,接著一個冒號,    
  143.   # ip addr add local 192.168.4.1/28 brd + label eth0:1 dev eth0    
  144.   示例2: 在乙太網介面eth0上增加一個地址192.168.20.0,掩碼長度為24位(155.155.155.0),標準廣播地址,標籤為eth0:Alias:    
  145.   # ip addr add 192.168.4.2/24 brd + dev eth1 label eth1:1    
  146.   這個命令等於傳統的: ifconfig eth1:1 192.168.4.2    
  147.   5.2 ip address delete--刪除一個協議地址. 縮寫:delete、del、d    
  148.   # ip addr del 192.168.4.1/24 brd + dev eth0 label eth0:Alias1    
  149.   5.3 ip address show--顯示協議地址. 縮寫:show、list、lst、sh、ls、l    
  150.   # ip addr ls eth0    
  151.   5.4.ip address flush--清除協議地址. 縮寫:flush、f    
  152.   示例1 : 刪除屬於私網10.0.0.0/8的所有地址:    
  153.   # ip -s -s a f to 10/8    
  154.   示例2 : 取消所有乙太網卡的IP地址    
  155.   # ip -4 addr flush label "eth0"
  156.   6. ip neighbour--neighbour/arp表管理命令    
  157.   縮寫 neighbour、neighbor、neigh、n    
  158.   命令 add、change、replace、delete、fulsh、show(或者list)    
  159.   6.1 ip neighbour add -- 新增一個新的鄰接條目    
  160.   ip neighbour change--修改一個現有的條目    
  161.   ip neighbour replace--替換一個已有的條目    
  162.   縮寫:add、a;change、chg;replace、repl    
  163.   示例1: 在裝置eth0上,為地址10.0.0.3新增一個permanent ARP條目:    
  164.   # ip neigh add 10.0.0.3 lladdr 0:0:0:0:0:1 dev eth0 nud perm    
  165.   示例2:把狀態改為reachable    
  166.   # ip neigh chg 10.0.0.3 dev eth0 nud reachable    
  167.   6.2.ip neighbour delete--刪除一個鄰接條目    
  168.   示例1:刪除裝置eth0上的一個ARP條目10.0.0.3    
  169.   # ip neigh del 10.0.0.3 dev eth0    
  170.   6.3.ip neighbour show--顯示網路鄰居的資訊. 縮寫:show、list、sh、ls    
  171.   示例1: # ip -s n ls 193.233.7.254    
  172.   193.233.7.254. dev eth0 lladdr 00:00:0c:76:3f:85 ref 5 used 12/13/20 nud reachable    
  173.   6.4.ip neighbour flush--清除鄰接條目. 縮寫:flush、f    
  174.   示例1: (-s 可以顯示詳細資訊)    
  175.   # ip -s -s n f 193.233.7.254    
  176.   7. 路由表管理    
  177.   7.1.縮寫 route、ro、r    
  178.   7.2.路由表    
  179.   從Linux-2.2開始,核心把路由歸納到許多路由表中,這些表都進行了編號,編號數字的範圍是1到255。另外,    
  180.   為了方便,還可以在/etc/iproute2/rt_tables中為路由表命名。    
  181.   預設情況下,所有的路由都會被插入到表main(編號254)中。在進行路由查詢時,核心只使用路由表main。    
  182.   7.3.ip route add -- 新增新路由    
  183.   ip route change -- 修改路由    
  184.   ip route replace -- 替換已有的路由    
  185.   縮寫:add、a;change、chg;replace、repl    
  186.   示例1: 設定到網路10.0.0/24的路由經過閘道器193.233.7.65    
  187.   # ip route add 10.0.0/24 via 193.233.7.65    
  188.   示例2: 修改到網路10.0.0/24的直接路由,使其經過裝置dummy    
  189.   # ip route chg 10.0.0/24 dev dummy    
  190.   示例3: 實現鏈路負載平衡.加入預設多路徑路由,讓ppp0和ppp1分擔負載(注意:scope值並非必需,它只不過是告訴核心,    
  191.   這個路由要經過閘道器而不是直連的。實際上,如果你知道遠端端點的地址,使用via引數來設定就更好了)。    
  192.   # ip route add default scope global nexthop dev ppp0 nexthop dev ppp1    
  193.   # ip route replace default scope global nexthop dev ppp0 nexthop dev ppp1    
  194.   示例4: 設定NAT路由。在轉發來自192.203.80.144的資料包之前,先進行網路地址轉換,把這個地址轉換為193.233.7.83    
  195.   # ip route add nat 192.203.80.142 via 193.233.7.83    
  196.   示例5: 實現資料包級負載平衡,允許把資料包隨機從多個路由發出。weight 可以設定權重.    
  197.   # 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    
  198.   7.4.ip route delete-- 刪除路由    
  199.   縮寫:delete、del、d    
  200.   示例1:刪除上一節命令加入的多路徑路由    
  201.   # ip route del default scope global nexthop dev ppp0 nexthop dev ppp1    
  202.   7.5.ip route show -- 列出路由    
  203.   縮寫:show、list、sh、ls、l    
  204.   示例1: 計算使用gated/bgp協議的路由個數    
  205.   # ip route ls proto gated/bgp |wc    
  206.   1413 9891 79010    
  207.   示例2: 計算路由快取裡面的條數,由於被快取路由的屬性可能大於一行,以此需要使用-o選項    
  208.   # ip -o route ls cloned |wc    
  209.   159 2543 18707    
  210.   示例3: 列出路由表TABLEID裡面的路由。預設設定是table main。TABLEID或者是一個真正的路由表ID或者是/etc/iproute2/rt_tables檔案定義的字串,    
  211.   或者是以下的特殊值:    
  212.   all -- 列出所有表的路由;    
  213.   cache -- 列出路由快取的內容。    
  214.   ip ro ls 193.233.7.82 tab cache    
  215.   示例4: 列出某個路由表的內容    
  216.   # ip route ls table fddi153    
  217.   示例5: 列出預設路由表的內容    
  218.   # ip route ls    
  219.   這個命令等於傳統的: route    
  220.   7.6.ip route flush -- 擦除路由表    
  221.   示例1: 刪除路由表main中的所有閘道器路由(示例:在路由監控程式掛掉之後):    
  222.   # ip -4 ro flush scope global type unicast    
  223.   示例2:清除所有被克隆出來的IPv6路由:    
  224.   # ip -6 -s -s ro flush cache    
  225.   示例3: 在gated程式掛掉之後,清除所有的BGP路由:    
  226.   # ip -s ro f proto gated/bgp    
  227.   示例4: 清除所有ipv4路由cache    
  228.   # ip route flush cache    
  229.   *** IPv4 routing cache is flushed.    
  230.   7.7 ip route get -- 獲得單個路由 .縮寫:get、g    
  231.   使用這個命令可以獲得到達目的地址的一個路由以及它的確切內容。    
  232.   ip route get命令和ip route show命令執行的操作是不同的。ip route show命令只是顯示現有的路由,而ip route get命令在必要時會派生出新的路由。    
  233.   示例1: 搜尋到193.233.7.82的路由    
  234.   # ip route get 193.233.7.82    
  235.   193.233.7.82 dev eth0 src 193.233.7.65 realms inr.ac cache mtu 1500 rtt 300    
  236.   示例2: 搜尋目的地址是193.233.7.82,來自193.233.7.82,從eth0裝置到達的路由(這條命令會產生一條非常有意思的路由,這是一條到193.233.7.82的迴環路由)    
  237.   # ip r g 193.233.7.82 from 193.233.7.82 iif eth0    
  238.   193.233.7.82 from 193.233.7.82 dev eth0 src 193.233.7.65 realms inr.ac/inr.ac    
  239.   cache     
  240.  mtu 1500 rtt 300 iif eth0    
  241.   8. ip route -- 路由策略資料庫管理命令    
  242.   命令    
  243.   add、delete、show(或者list)    
  244.   注意:策略路由(policy routing)不等於路由策略(rouing policy)。    
  245.   在某些情況下,我們不只是需要通過資料包的目的地址決定路由,可能還需要通過其他一些域:源地址、IP協議、傳輸層埠甚至資料包的負載。    
  246.   這就叫做:策略路由(policy routing)。    
  247.   8.1. ip rule add -- 插入新的規則    
  248.   ip rule delete -- 刪除規則    
  249.   縮寫:add、a;delete、del、d    
  250.   示例1: 通過路由表inr.ruhep路由來自源地址為192.203.80/24的資料包    
  251.   ip ru add from 192.203.80/24 table inr.ruhep prio 220    
  252.   示例2:把源地址為193.233.7.83的資料報的源地址轉換為192.203.80.144,並通過表1進行路由    
  253.   ip ru add from 193.233.7.83 nat 192.203.80.144 table 1 prio 320    
  254.   示