1. 程式人生 > >linux 路由表設定 之 route 指令詳解

linux 路由表設定 之 route 指令詳解

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

[cpp] view plain copy

  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 該路由表項對應的輸出介面

3 種路由型別

主機路由

主機路由是路由選擇表中指向單個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 命令,設定核心路由表的命令格式是:

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

其中:

  • add : 新增一條路由規則
  • del : 刪除一條路由規則
  • -net : 目的地址是一個網路
  • -host : 目的地址是一個主機
  • target : 目的網路或主機
  • netmask : 目的地址的網路掩碼
  • gw : 路由資料包通過的閘道器
  • dev : 為路由指定的網路介面

route 命令使用舉例

新增到主機的路由

[cpp] view plain copy print?

  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的網管  
# 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的網管

 

新增到網路的路由

[cpp] view plain copy print?

  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  
# 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

 

新增預設路由

[cpp] view plain copy print?

  1. # route add default gw 192.168.1.1  
# route add default gw 192.168.1.1

 

刪除路由

[cpp] view plain copy print?

  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  
# route del -host 192.168.1.2 dev eth0:0
# route del -host 10.20.30.148 gw 10.20.30.40
# route del -net 10.20.30.40 netmask 255.255.255.248 eth0
# route del -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41
# route del -net 192.168.1.0/24 eth1
# route del default gw 192.168.1.1

設定包轉發

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

[cpp] view plain copy print?

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

 

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

[cpp] view plain copy print?

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

 

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

[cpp] view plain copy print?

  1. # sysctl net.ipv4.ip_forward  
# 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

輸出:

 

 

[cpp] view plain copy print?

  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  
[[email protected] ~]# 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
e192.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
[[email protected] ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   0.0.0.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
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

輸出:

 

[cpp] view plain copy print?

  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] ~]# route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
[[email protected] ~]# 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

 

[[email protected] ~]#  

說明:

增加一條 到達244.0.0.0的路由

 

例項3:遮蔽一條路由

命令:

route add -net 224.0.0.0 netmask 240.0.0.0 reject

輸出:

 

 

[cpp] view plain copy print?

  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  
[[email protected] ~]# route add -net 224.0.0.0 netmask 240.0.0.0 reject
[[email protected] ~]# 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

說明:

增加一條遮蔽的路由,目的地址為 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

輸出:

 

 

[cpp] view plain copy print?

  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] ~]#   
[[email protected] ~]# 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
[[email protected] ~]# route del -net 224.0.0.0 netmask 240.0.0.0
[[email protected] ~]# 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
[[email protected] ~]# route del -net 224.0.0.0 netmask 240.0.0.0 reject
[[email protected] ~]# 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
[[email protected] ~]# 

 

說明:

 

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

命令:

route del default gw 192.168.120.240

route add default gw 192.168.120.240

輸出:

 

[cpp] view plain copy print?

  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] ~]#   
[[email protected] ~]# route del default gw 192.168.120.240
[[email protected] ~]# 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
[[email protected] ~]# route add default gw 192.168.120.240
[[email protected] ~]# 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
[[email protected] ~]# 

 

[cpp] view plain copy print?

  1. 顯示現在所有路由    
  2.     
  3.   #route -n    
  4.     
  5.   [email protected]:~# route    
  6.     
  7.   Kernel IP routing table    
  8.     
  9.   Destination Gateway Genmask Flags Metric Ref Use Iface    
  10.     
  11.   10.147.9.0 * 255.255.255.0 U 1 0 0 eth0    
  12.     
  13.   192.168.1.0 * 255.255.255.0 U 2 0 0 wlan0    
  14.     
  15.   192.168.122.0 * 255.255.255.0 U 0 0 0 virbr0    
  16.     
  17.   link-local * 255.255.0.0 U 1000 0 0 eth0    
  18.     
  19.   192.168.0.0 192.168.1.1 255.255.0.0 UG 0 0 0 wlan0    
  20.     
  21.   default 10.147.9.1 0.0.0.0 UG 0 0 0 eth0    
  22.     
  23.   [email protected]:~#    
  24.     
  25.   結果是自上而下, 就是說, 哪條在前面, 哪條就有優先, 前面都沒有, 就用最後一條default    
  26.     
  27.   舉例, 新增一條路由(發往192.168.62這個網段的全部要經過閘道器192.168.1.1)    
  28.     
  29.   route add -net 192.168.62.0 netmask 255.255.255.0 gw 192.168.1.1    
  30.     
  31.   刪除一條路由    
  32.     
  33.   route del -net 192.168.122.0 netmask 255.255.255.0    
  34.     
  35.   刪除的時候不用寫閘道器    
  36.     
  37.   linux下新增路由的方法:    
  38.     
  39.   一:使用 route 命令新增    
  40.     
  41.   使用route 命令新增的路由,機器重啟或者網絡卡重啟後路由就失效了,方法:    
  42.     
  43.   //新增到主機的路由    
  44.     
  45.   # route add –host 192.168.168.110 dev eth0    
  46.     
  47.   # route add –host 192.168.168.119 gw 192.168.168.1    
  48.     
  49.   //新增到網路的路由    
  50.     
  51.   # route add –net IP netmask MASK eth0    
  52.     
  53.   # route add –net IP netmask MASK gw IP    
  54.     
  55.   # route add –net IP/24 eth1    
  56.     
  57.   //新增預設閘道器    
  58.     
  59.   # route add default gw IP    
  60.     
  61.   //刪除路由    
  62.     
  63.   # route del –host 192.168.168.110 dev eth0    
  64.     
  65.   二:在linux下設定永久路由的方法:    
  66.     
  67.   1.在/etc/rc.local裡新增    
  68.     
  69.   方法:    
  70.     
  71.   route add -net 192.168.3.0/24 dev eth0    
  72.     
  73.   route add -net 192.168.2.0/24 gw 192.168.3.254    
  74.     
  75.   2.在/etc/sysconfig/network裡新增到末尾    
  76.     
  77.   方法:GATEWAY=gw-ip 或者 GATEWAY=gw-dev    
  78.     
  79.   3./etc/sysconfig/static-router :    
  80.     
  81.   any net x.x.x.x/24 gw y.y.y.y    
  82.     
  83.     
  84. ------------------------------------------------------------------------------------------    
  85. --  Route命令的正確用法    
  86. 使用 Route 命令列工具檢視並編輯計算機的 IP 路由表。Route 命令和語法如下所示:    
  87. route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]]    
  88. -f 清除所有閘道器入口的路由表。      
  89. -p 與 add 命令一起使用時使路由具有永久性。     
  90. Command 指定您想執行的命令 (Add/Change/Delete/Print)。     
  91. Destination 指定該路由的網路目標。      
  92. mask Netmask 指定與網路目標相關的網路掩碼(也被稱作子網掩碼)。      
  93. Gateway 指定網路目標定義的地址集和子網掩碼可以到達的前進或下一躍點 IP 地址。      
  94. metric Metric 為路由指定一個整數成本值標(從 1 至 ArrayArrayArrayArray),當在路由表(與轉發的資料包目標地址最匹配)的多個路由中進行選擇時可以使用。      
  95. if Interface 為可以訪問目標的介面指定介面索引。若要獲得一個介面列表和它們相應的介面索引,使用 route print 命令的顯示功能。可以使用十進位制或十六進位制值進行介面索引。     
  96. /?  在命令提示符處顯示幫助。      
  97. 示例    
  98. 若要顯示 IP 路由表的全部內容,請鍵入:    
  99. route print    
  100. 若要顯示以 10. 起始的 IP 路由表中的路由,請鍵入:    
  101. route print 10.*    
  102. 若要新增帶有 1Array2.168.12.1 預設閘道器地址的預設路由,請鍵入:    
  103. route add 0.0.0.0 mask 0.0.0.0 1Array2.168.12.1    
  104. 若要向帶有 255.255.0.0 子網掩碼和 10.27.0.1 下一躍點地址的 10.41.0.0 目標中新增一個路由,請鍵入:    
  105. route add 10.41.0.0 mask 255.255.0.0 10.27.0.1    
  106. 若要向帶有 255.255.0.0 子網掩碼和 10.27.0.1 下一躍點地址的 10.41.0.0 目標中新增一個永久路由,請鍵入:    
  107. route -p add 10.41.0.0 mask 255.255.0.0 10.27.0.1    
  108. 若要向帶有 255.255.0.0 子網掩碼、10.27.0.1 下一躍點地址且其成本值標為 7 的 10.41.0.0 目標中新增一個路由,請鍵入:    
  109. route add 10.41.0.0 mask 255.255.0.0 10.27.0.1 metric 7    
  110. 若要向帶有 255.255.0.0 子網掩碼、10.27.0.1 下一躍點地址且使用 0x3 介面索引的 10.41.0.0 目標中新增一個路由,請鍵入:    
  111. route add 10.41.0.0 mask 255.255.0.0 10.27.0.1 if 0x3    
  112. 若要刪除到帶有 255.255.0.0 子網掩碼的 10.41.0.0 目標的路由,請鍵入:    
  113. route delete 10.41.0.0 mask 255.255.0.0    
  114. 若要刪除以 10. 起始的 IP 路由表中的所有路由,請鍵入:    
  115. route delete 10.*    
  116. 若要將帶有 10.41.0.0 目標和 255.255.0.0 子網掩碼的下一躍點地址從 10.27.0.1 修改為 10.27.0.25,請鍵入:    
  117. route change 10.41.0.0 mask 255.255.0.0 10.27.0.25    
  118.     
  119. -------------------------------------------------------------------------    
  120.   首先,先了解傳統的網路配置命令:    
  121.   1. 使用ifconfig命令配置並檢視網路介面情況    
  122.   示例1: 配置eth0的IP,同時啟用裝置:    
  123.   # ifconfig eth0 192.168.4.1 netmask 255.255.255.0 up    
  124.   示例2: 配置eth0別名裝置 eth0:1 的IP,並新增路由    
  125.   # ifconfig eth0:1 192.168.4.2    
  126.   # route add –host 192.168.4.2 dev eth0:1    
  127.   示例3:啟用(禁用)裝置    
  128.   # ifconfig eth0:1 up(down)    
  129.   示例4:檢視所有(指定)網路介面配置    
  130.   # ifconfig (eth0)    
  131.   2. 使用route 命令配置路由表    
  132.   示例1:新增到主機路由    
  133.   # route add –host 192.168.4.2 dev eth0:1    
  134.   # route add –host 192.168.4.1 gw 192.168.4.250    
  135.   示例2:新增到網路的路由    
  136.   # route add –net IP netmask MASK eth0    
  137.   # route add –net IP netmask MASK gw IP    
  138.   # route add –net IP/24 eth1    
  139.   示例3:新增預設閘道器    
  140.   # route add default gw IP    
  141.   示例4:刪除路由    
  142.   # route del –host 192.168.4.1 dev eth0:1    
  143.   示例5:檢視路由資訊    
  144.   # route 或 route -n (-n 表示不解析名字,列出速度會比route 快)    
  145.   3.ARP 管理命令    
  146.   示例1:檢視ARP快取    
  147.   # arp    
  148.   示例2: 新增    
  149.   # arp –s IP MAC    
  150.   示例3: 刪除    
  151.   # arp –d IP    
  152.   4. ip是iproute2軟體包裡面的一個強大的網路配置工具,它能夠替代一些傳統的網路管理工具。例如:ifconfig、route等,    
  153.   上面的示例完全可以用下面的ip命令實現,而且ip命令可以實現更多的功能.下面介紹一些示例:    
  154.   4.0 ip命令的語法    
  155.   ip命令的用法如下:    
  156.   ip [OPTIONS] OBJECT [COMMAND [ARGUMENTS]]    
  157.   4.1 ip link set--改變裝置的屬性. 縮寫:set、s    
  158.   示例1:up/down 起動/關閉裝置。    
  159.   # ip link set dev eth0 up    
  160.   這個等於傳統的 # ifconfig eth0 up(down)    
  161.   示例2:改變裝置傳輸佇列的長度。    
  162.   引數:txqueuelen NUMBER或者txqlen NUMBER    
  163.   # ip link set dev eth0 txqueuelen 100    
  164.   示例3:改變網路裝置MTU(最大傳輸單元)的值。    
  165.   # ip link set dev eth0 mtu 1500    
  166.   示例4: 修改網路裝置的MAC地址。    
  167.   引數: address LLADDRESS    
  168.   # ip link set dev eth0 address 00:01:4f:00:15:f1    
  169.   4.2 ip link show--顯示裝置屬性. 縮寫:show、list、lst、sh、ls、l    
  170.   -s選項出現兩次或者更多次,ip會輸出更為詳細的錯誤資訊統計。    
  171.   示例:    
  172.   # ip -s -s link ls eth0    
  173.   eth0: mtu 1500 qdisc cbq qlen 100    
  174.   link/ether 00:a0:cc:66:18:78 brd ff:ff:ff:ff:ff:ff    
  175.   RX: bytes packets errors dropped overrun mcast    
  176.   2449949362 2786187 0 0 0 0    
  177.   RX errors: length crc fifo missed    
  178.   0 0 0 0 0    
  179.   TX: bytes packets errors dropped carrier collsns    
  180.   178558497 1783946 332 0 332 35172    
  181.   TX errors: aborted fifo window heartbeat    
  182.   0 0 0 332    
  183.   這個命令等於傳統的 ifconfig eth0    
  184.   5.1 ip address add--新增一個新的協議地址. 縮寫:add、a    
  185.   示例1:為每個地址設定一個字串作為標籤。為了和Linux-2.0的網路別名相容,這個字串必須以裝置名開頭,接著一個冒號,    
  186.   # ip addr add local 192.168.4.1/28 brd + label eth0:1 dev eth0    
  187.   示例2: 在乙太網介面eth0上增加一個地址192.168.20.0,掩碼長度為24位(155.155.155.0),標準廣播地址,標籤為eth0:Alias:    
  188.   # ip addr add 192.168.4.2/24 brd + dev eth1 label eth1:1    
  189.   這個命令等於傳統的: ifconfig eth1:1 192.168.4.2    
  190.   5.2 ip address delete--刪除一個協議地址. 縮寫:delete、del、d    
  191.   # ip addr del 192.168.4.1/24 brd + dev eth0 label eth0:Alias1    
  192.   5.3 ip address show--顯示協議地址. 縮寫:show、list、lst、sh、ls、l    
  193.   # ip addr ls eth0    
  194.   5.4.ip address flush--清除協議地址. 縮寫:flush、f    
  195.   示例1 : 刪除屬於私網10.0.0.0/8的所有地址:    
  196.   # ip -s -s a f to 10/8    
  197.   示例2 : 取消所有乙太網卡的IP地址    
  198.   # ip -4 addr flush label "eth0"    
  199.   6. ip neighbour--neighbour/arp表管理命令    
  200.   縮寫 neighbour、neighbor、neigh、n    
  201.   命令 add、change、replace、delete、fulsh、show(或者list)    
  202.   6.1 ip neighbour add -- 新增一個新的鄰接條目    
  203.   ip neighbour change--修改一個現有的條目    
  204.   ip neighbour replace--替換一個已有的條目    
  205.   縮寫:add、a;change、chg;replace、repl    
  206.   示例1: 在裝置eth0上,為地址10.0.0.3新增一個permanent ARP條目: