linux 路由表設定 之 route 指令詳解
使用下面的 route 命令可以檢視 Linux 核心路由表。
[cpp] view plain copy
- # route
- Destination Gateway Genmask Flags Metric Ref Use Iface
- 192.168.0.0 * 255.255.255.0 U 0 0 0 eth0
- 169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
- 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?
- # 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的網管
# 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?
- # 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
# 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?
- # route add default gw 192.168.1.1
# route add default gw 192.168.1.1
刪除路由
[cpp] view plain copy print?
- # 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
# 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?
- # sysctl -w net.ipv4.ip_forward=1
# sysctl -w net.ipv4.ip_forward=1
這樣設定之後,當前系統就能實現包轉發,但下次啟動計算機時將失效。為了使在下次啟動計算機時仍然有效,需要將下面的行寫入配置檔案/etc/sysctl.conf。
[cpp] view plain copy print?
- # vi /etc/sysctl.conf
- net.ipv4.ip_forward = 1
# vi /etc/sysctl.conf net.ipv4.ip_forward = 1
使用者還可以使用如下的命令檢視當前系統是否支援包轉發。
[cpp] view plain copy print?
- # 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?
- [[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
[[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?
- [[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] ~]# 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?
- [[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
[[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?
- [[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] ~]#
[[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?
- [[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] ~]#
[[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?
- 顯示現在所有路由
- #route -n
- [email protected]:~# 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
- [email protected]:~#
- 結果是自上而下, 就是說, 哪條在前面, 哪條就有優先, 前面都沒有, 就用最後一條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條目: