在 CentOS 中默認(rèn)的內(nèi)核配置已經(jīng)包含了路由功能,但默認(rèn)并沒有在系統(tǒng)啟動(dòng)時(shí)啟用此功能。開啟 Linux 的路由功能可以通過調(diào)整內(nèi)核的網(wǎng)絡(luò)參數(shù)來實(shí)現(xiàn)。要配置和調(diào)整內(nèi)核參數(shù)可以使用 sysctl 命令。例如:要開啟 Linux 內(nèi)核的數(shù)據(jù)包轉(zhuǎn)發(fā)功能可以使用如下的命令。
# sysctl -w net.ipv4.ip_forward=1
這樣設(shè)置之后,當(dāng)前系統(tǒng)就能實(shí)現(xiàn)包轉(zhuǎn)發(fā),但下次啟動(dòng)計(jì)算機(jī)時(shí)將失效。為了使在下次啟動(dòng)計(jì)算機(jī)時(shí)仍然有效,需要將下面的行寫入配置文件/etc/sysctl.conf。
# vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
用戶還可以使用如下的命令查看當(dāng)前系統(tǒng)是否支持包轉(zhuǎn)發(fā)。
# sysctl net.ipv4.ip_forward
route 命令:
Linux系統(tǒng)的route命令用于顯示和操作IP路由表(show / manipulate the IP routing table)。要實(shí)現(xiàn)兩個(gè)不同的子網(wǎng)之間的通信,需要一臺(tái)連接兩個(gè)網(wǎng)絡(luò)的路由器,或者同時(shí)位于兩個(gè)網(wǎng)絡(luò)的網(wǎng)關(guān)來實(shí)現(xiàn)。在Linux系統(tǒng)中,設(shè)置路由通常是為了解決以下問題:該Linux系統(tǒng)在一個(gè)局域網(wǎng)中,局域網(wǎng)中有一個(gè)網(wǎng)關(guān),能夠讓機(jī)器訪問Internet,那么就需要將這臺(tái)機(jī)器的IP地址設(shè)置為Linux機(jī)器的默認(rèn)路由。要注意的是,直接在命令行下執(zhí)行route命令來添加路由,不會(huì)永久保存,當(dāng)網(wǎng)卡重啟或者機(jī)器重啟之后,該路由就失效了;可以在/etc/rc.local中添加route命令來保證該路由設(shè)置永久有效。
1.命令格式:
route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]]
2.命令功能:
Route命令是用于操作基于內(nèi)核ip路由表,它的主要作用是創(chuàng)建一個(gè)靜態(tài)路由讓指定一個(gè)主機(jī)或者一個(gè)網(wǎng)絡(luò)通過一個(gè)網(wǎng)絡(luò)接口,如eth0。當(dāng)使用"add"或者"del"參數(shù)時(shí),路由表被修改,如果沒有參數(shù),則顯示路由表當(dāng)前的內(nèi)容。
3.命令參數(shù):
-c 顯示更多信息
-n 不解析名字
-v 顯示詳細(xì)的處理信息
-F 顯示發(fā)送信息
-C 顯示路由緩存
-f 清除所有網(wǎng)關(guān)入口的路由表。
-p 與 add 命令一起使用時(shí)使路由具有永久性。
add:添加一條新路由。
del:刪除一條路由。
-net:目標(biāo)地址是一個(gè)網(wǎng)絡(luò)。
-host:目標(biāo)地址是一個(gè)主機(jī)。
netmask:當(dāng)添加一個(gè)網(wǎng)絡(luò)路由時(shí),需要使用網(wǎng)絡(luò)掩碼。
gw:路由數(shù)據(jù)包通過網(wǎng)關(guān)。注意,你指定的網(wǎng)關(guān)必須能夠達(dá)到。
metric:設(shè)置路由跳數(shù)。
Command 指定您想運(yùn)行的命令 (Add/Change/Delete/Print)。
Destination 指定該路由的網(wǎng)絡(luò)目標(biāo)。
mask Netmask 指定與網(wǎng)絡(luò)目標(biāo)相關(guān)的網(wǎng)絡(luò)掩碼(也被稱作子網(wǎng)掩碼)。
Gateway 指定網(wǎng)絡(luò)目標(biāo)定義的地址集和子網(wǎng)掩碼可以到達(dá)的前進(jìn)或下一躍點(diǎn) IP 地址。
metric Metric 為路由指定一個(gè)整數(shù)成本值標(biāo)(從 1 至 9999),當(dāng)在路由表(與轉(zhuǎn)發(fā)的數(shù)據(jù)包目標(biāo)地址最匹配)的多個(gè)路由中進(jìn)行選擇時(shí)可以使用。
if Interface 為可以訪問目標(biāo)的接口指定接口索引。若要獲得一個(gè)接口列表和它們相應(yīng)的接口索引,使用 route print 命令的顯示功能??梢允褂檬M(jìn)制或十六進(jìn)制值進(jìn)行接口索引。
4.使用實(shí)例:
實(shí)例1:顯示當(dāng)前路由
命令:
route
route -n
輸出:
[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
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
[root@localhost ~]# 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
說明:
第一行表示主機(jī)所在網(wǎng)絡(luò)的地址為192.168.120.0,若數(shù)據(jù)傳送目標(biāo)是在本局域網(wǎng)內(nèi)通信,則可直接通過eth0轉(zhuǎn)發(fā)數(shù)據(jù)包;
第四行表示數(shù)據(jù)傳送目的是訪問Internet,則由接口eth0,將數(shù)據(jù)包發(fā)送到網(wǎng)關(guān)192.168.120.240
其中Flags為路由標(biāo)志,標(biāo)記當(dāng)前網(wǎng)絡(luò)節(jié)點(diǎn)的狀態(tài)。
Flags標(biāo)志說明:
U Up表示此路由當(dāng)前為啟動(dòng)狀態(tài)
H Host,表示此網(wǎng)關(guān)為一主機(jī)
G Gateway,表示此網(wǎng)關(guān)為一路由器
R Reinstate Route,使用動(dòng)態(tài)路由重新初始化的路由
D Dynamically,此路由是動(dòng)態(tài)性地寫入
M Modified,此路由是由路由守護(hù)程序或?qū)蚱鲃?dòng)態(tài)修改
! 表示此路由當(dāng)前為關(guān)閉狀態(tài)
備注:
route -n (-n 表示不解析名字,列出速度會(huì)比route 快)
實(shí)例2:添加網(wǎng)關(guān)/設(shè)置網(wǎng)關(guān)
命令:
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
輸出:
[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
[root@localhost ~]#
說明:
增加一條 到達(dá)244.0.0.0的路由
實(shí)例3:屏蔽一條路由
命令:
route add -net 224.0.0.0 netmask 240.0.0.0 reject
輸出:
[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
說明:
增加一條屏蔽的路由,目的地址為 224.x.x.x 將被拒絕
實(shí)例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
輸出:
[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 ~]#
說明:
實(shí)例5:刪除和添加設(shè)置默認(rèn)網(wǎng)關(guān)
命令:
route del default gw 192.168.120.240
route add default gw 192.168.120.240
輸出:
[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 ~]#
-
顯示現(xiàn)在所有路由
-
-
#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:~#
-
-
結(jié)果是自上而下, 就是說, 哪條在前面, 哪條就有優(yōu)先, 前面都沒有, 就用最后一條default
-
-
舉例, 添加一條路由(發(fā)往192.168.62這個(gè)網(wǎng)段的全部要經(jīng)過網(wǎng)關(guān)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
-
-
刪除的時(shí)候不用寫網(wǎng)關(guān)
-
-
linux下添加路由的方法:
-
-
一:使用 route 命令添加
-
-
使用route 命令添加的路由,機(jī)器重啟或者網(wǎng)卡重啟后路由就失效了,方法:
-
-
//添加到主機(jī)的路由
-
-
# route add –host 192.168.168.110 dev eth0
-
-
# route add –host 192.168.168.119 gw 192.168.168.1
-
-
//添加到網(wǎng)絡(luò)的路由
-
-
# route add –net IP netmask MASK eth0
-
-
# route add –net IP netmask MASK gw IP
-
-
# route add –net IP/24 eth2
-
-
//添加默認(rèn)網(wǎng)關(guān)
-
-
# route add default gw IP
-
-
//刪除路由
-
-
# route del –host 192.168.168.110 dev eth0
-
-
二:在linux下設(shè)置永久路由的方法:
-
-
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 命令行工具查看并編輯計(jì)算機(jī)的 IP 路由表。Route 命令和語法如下所示:
-
route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]]
-
-f 清除所有網(wǎng)關(guān)入口的路由表。
-
-p 與 add 命令一起使用時(shí)使路由具有永久性。
-
Command 指定您想運(yùn)行的命令 (Add/Change/Delete/Print)。
-
Destination 指定該路由的網(wǎng)絡(luò)目標(biāo)。
-
mask Netmask 指定與網(wǎng)絡(luò)目標(biāo)相關(guān)的網(wǎng)絡(luò)掩碼(也被稱作子網(wǎng)掩碼)。
-
Gateway 指定網(wǎng)絡(luò)目標(biāo)定義的地址集和子網(wǎng)掩碼可以到達(dá)的前進(jìn)或下一躍點(diǎn) IP 地址。
-
metric Metric 為路由指定一個(gè)整數(shù)成本值標(biāo)(從 1 至 ArrayArrayArrayArray),當(dāng)在路由表(與轉(zhuǎn)發(fā)的數(shù)據(jù)包目標(biāo)地址最匹配)的多個(gè)路由中進(jìn)行選擇時(shí)可以使用。
-
if Interface 為可以訪問目標(biāo)的接口指定接口索引。若要獲得一個(gè)接口列表和它們相應(yīng)的接口索引,使用 route print 命令的顯示功能。可以使用十進(jìn)制或十六進(jìn)制值進(jìn)行接口索引。
-
/? 在命令提示符處顯示幫助。
-
示例
-
若要顯示 IP 路由表的全部內(nèi)容,請(qǐng)鍵入:
-
route print
-
若要顯示以 10. 起始的 IP 路由表中的路由,請(qǐng)鍵入:
-
route print 10.*
-
若要添加帶有 1Array2.168.12.1 默認(rèn)網(wǎng)關(guān)地址的默認(rèn)路由,請(qǐng)鍵入:
-
route add 0.0.0.0 mask 0.0.0.0 1Array2.168.12.1
-
若要向帶有 255.255.0.0 子網(wǎng)掩碼和 10.27.0.1 下一躍點(diǎn)地址的 10.41.0.0 目標(biāo)中添加一個(gè)路由,請(qǐng)鍵入:
-
route add 10.41.0.0 mask 255.255.0.0 10.27.0.1
-
若要向帶有 255.255.0.0 子網(wǎng)掩碼和 10.27.0.1 下一躍點(diǎn)地址的 10.41.0.0 目標(biāo)中添加一個(gè)永久路由,請(qǐng)鍵入:
-
route -p add 10.41.0.0 mask 255.255.0.0 10.27.0.1
-
若要向帶有 255.255.0.0 子網(wǎng)掩碼、10.27.0.1 下一躍點(diǎn)地址且其成本值標(biāo)為 7 的 10.41.0.0 目標(biāo)中添加一個(gè)路由,請(qǐng)鍵入:
-
route add 10.41.0.0 mask 255.255.0.0 10.27.0.1 metric 7
-
若要向帶有 255.255.0.0 子網(wǎng)掩碼、10.27.0.1 下一躍點(diǎn)地址且使用 0x3 接口索引的 10.41.0.0 目標(biāo)中添加一個(gè)路由,請(qǐng)鍵入:
-
route add 10.41.0.0 mask 255.255.0.0 10.27.0.1 if 0x3
-
若要?jiǎng)h除到帶有 255.255.0.0 子網(wǎng)掩碼的 10.41.0.0 目標(biāo)的路由,請(qǐng)鍵入:
-
route delete 10.41.0.0 mask 255.255.0.0
-
若要?jiǎng)h除以 10. 起始的 IP 路由表中的所有路由,請(qǐng)鍵入:
-
route delete 10.*
-
若要將帶有 10.41.0.0 目標(biāo)和 255.255.0.0 子網(wǎng)掩碼的下一躍點(diǎn)地址從 10.27.0.1 修改為 10.27.0.25,請(qǐng)鍵入:
-
route change 10.41.0.0 mask 255.255.0.0 10.27.0.25
-
-
-------------------------------------------------------------------------
-
首先,先了解傳統(tǒng)的網(wǎng)絡(luò)配置命令:
-
1. 使用ifconfig命令配置并查看網(wǎng)絡(luò)接口情況
-
示例1: 配置eth0的IP,同時(shí)激活設(shè)備:
-
# ifconfig eth0 192.168.4.1 netmask 255.255.255.0 up
-
示例2: 配置eth0別名設(shè)備 eth0:1 的IP,并添加路由
-
# ifconfig eth0:1 192.168.4.2
-
# route add –host 192.168.4.2 dev eth0:1
-
示例3:激活(禁用)設(shè)備
-
# ifconfig eth0:1 up(down)
-
示例4:查看所有(指定)網(wǎng)絡(luò)接口配置
-
# ifconfig (eth0)
-
2. 使用route 命令配置路由表
-
示例1:添加到主機(jī)路由
-
# route add –host 192.168.4.2 dev eth0:1
-
# route add –host 192.168.4.1 gw 192.168.4.250
-
示例2:添加到網(wǎng)絡(luò)的路由
-
# route add –net IP netmask MASK eth0
-
# route add –net IP netmask MASK gw IP
-
# route add –net IP/24 eth2
-
示例3:添加默認(rèn)網(wǎng)關(guān)
-
# route add default gw IP
-
示例4:刪除路由
-
# route del –host 192.168.4.1 dev eth0:1
-
示例5:查看路由信息
-
# route 或 route -n (-n 表示不解析名字,列出速度會(huì)比route 快)
-
3.ARP 管理命令
-
示例1:查看ARP緩存
-
# arp
-
示例2: 添加
-
# arp –s IP MAC
-
示例3: 刪除
-
# arp –d IP
-
4. ip是iproute2軟件包里面的一個(gè)強(qiáng)大的網(wǎng)絡(luò)配置工具,它能夠替代一些傳統(tǒng)的網(wǎng)絡(luò)管理工具。例如:ifconfig、route等,
-
上面的示例完全可以用下面的ip命令實(shí)現(xiàn),而且ip命令可以實(shí)現(xiàn)更多的功能.下面介紹一些示例:
-
4.0 ip命令的語法
-
ip命令的用法如下:
-
ip [OPTIONS] OBJECT [COMMAND [ARGUMENTS]]
-
4.1 ip link set--改變?cè)O(shè)備的屬性. 縮寫:set、s
-
示例1:up/down 起動(dòng)/關(guān)閉設(shè)備。
-
# ip link set dev eth0 up
-
這個(gè)等于傳統(tǒng)的 # ifconfig eth0 up(down)
-
示例2:改變?cè)O(shè)備傳輸隊(duì)列的長度。
-
參數(shù):txqueuelen NUMBER或者txqlen NUMBER
-
# ip link set dev eth0 txqueuelen 100
-
示例3:改變網(wǎng)絡(luò)設(shè)備MTU(最大傳輸單元)的值。
-
# ip link set dev eth0 mtu 1500
-
示例4: 修改網(wǎng)絡(luò)設(shè)備的MAC地址。
-
參數(shù): address LLADDRESS
-
# ip link set dev eth0 address 00:01:4f:00:15:f1
-
4.2 ip link show--顯示設(shè)備屬性. 縮寫:show、list、lst、sh、ls、l
-
-s選項(xiàng)出現(xiàn)兩次或者更多次,ip會(huì)輸出更為詳細(xì)的錯(cuò)誤信息統(tǒng)計(jì)。
-
示例:
-
# 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
-
這個(gè)命令等于傳統(tǒng)的 ifconfig eth0
-
5.1 ip address add--添加一個(gè)新的協(xié)議地址. 縮寫:add、a
-
示例1:為每個(gè)地址設(shè)置一個(gè)字符串作為標(biāo)簽。為了和Linux-2.0的網(wǎng)絡(luò)別名兼容,這個(gè)字符串必須以設(shè)備名開頭,接著一個(gè)冒號(hào),
-
# ip addr add local 192.168.4.1/28 brd + label eth0:1 dev eth0
-
示例2: 在以太網(wǎng)接口eth0上增加一個(gè)地址192.168.20.0,掩碼長度為24位(155.155.155.0),標(biāo)準(zhǔn)廣播地址,標(biāo)簽為eth0:Alias:
-
# ip addr add 192.168.4.2/24 brd + dev eth2 label eth2:1
-
這個(gè)命令等于傳統(tǒng)的: ifconfig eth2:1 192.168.4.2
-
5.2 ip address delete--刪除一個(gè)協(xié)議地址. 縮寫:delete、del、d
-
# ip addr del 192.168.4.1/24 brd + dev eth0 label eth0:Alias1
-
5.3 ip address show--顯示協(xié)議地址. 縮寫:show、list、lst、sh、ls、l
-
# ip addr ls eth0
-
5.4.ip address flush--清除協(xié)議地址. 縮寫:flush、f
-
示例1 : 刪除屬于私網(wǎng)10.0.0.0/8的所有地址:
-
# ip -s -s a f to 10/8
-
示例2 : 取消所有以太網(wǎng)卡的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 -- 添加一個(gè)新的鄰接條目
-
ip neighbour change--修改一個(gè)現(xiàn)有的條目
-
ip neighbour replace--替換一個(gè)已有的條目
-
縮寫:add、a;change、chg;replace、repl
-
示例1: 在設(shè)備eth0上,為地址10.0.0.3添加一個(gè)permanent ARP條目:
-
# ip neigh add 10.0.0.3 lladdr 0:0:0:0:0:1 dev eth0 nud perm
-
示例2:把狀態(tài)改為reachable
-
# ip neigh chg 10.0.0.3 dev eth0 nud reachable
-
6.2.ip neighbour delete--刪除一個(gè)鄰接條目
-
示例1:刪除設(shè)備eth0上的一個(gè)ARP條目10.0.0.3
-
# ip neigh del 10.0.0.3 dev eth0
-
6.3.ip neighbour show--顯示網(wǎng)絡(luò)鄰居的信息. 縮寫: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 可以顯示詳細(xì)信息)
-
# ip -s -s n f 193.233.7.254
-
7. 路由表管理
-
7.1.縮寫 route、ro、r
-
7.2.路由表
-
從Linux-2.2開始,內(nèi)核把路由歸納到許多路由表中,這些表都進(jìn)行了編號(hào),編號(hào)數(shù)字的范圍是1到255。另外,
-
為了方便,還可以在/etc/iproute2/rt_tables中為路由表命名。
-
默認(rèn)情況下,所有的路由都會(huì)被插入到表main(編號(hào)254)中。在進(jìn)行路由查詢時(shí),內(nèi)核只使用路由表main。
-
7.3.ip route add -- 添加新路由
-
ip route change -- 修改路由
-
ip route replace -- 替換已有的路由
-
縮寫:add、a;change、chg;replace、repl
-
示例1: 設(shè)置到網(wǎng)絡(luò)10.0.0/24的路由經(jīng)過網(wǎng)關(guān)193.233.7.65
-
# ip route add 10.0.0/24 via 193.233.7.65
-
示例2: 修改到網(wǎng)絡(luò)10.0.0/24的直接路由,使其經(jīng)過設(shè)備dummy
-
# ip route chg 10.0.0/24 dev dummy
-
示例3: 實(shí)現(xiàn)鏈路負(fù)載平衡.加入缺省多路徑路由,讓ppp0和ppp1分擔(dān)負(fù)載(注意:scope值并非必需,它只不過是告訴內(nèi)核,
-
這個(gè)路由要經(jīng)過網(wǎng)關(guān)而不是直連的。實(shí)際上,如果你知道遠(yuǎn)程端點(diǎn)的地址,使用via參數(shù)來設(shè)置就更好了)。
-
# 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: 設(shè)置NAT路由。在轉(zhuǎn)發(fā)來自192.203.80.144的數(shù)據(jù)包之前,先進(jìn)行網(wǎng)絡(luò)地址轉(zhuǎn)換,把這個(gè)地址轉(zhuǎn)換為193.233.7.83
-
# ip route add nat 192.203.80.142 via 193.233.7.83
-
示例5: 實(shí)現(xiàn)數(shù)據(jù)包級(jí)負(fù)載平衡,允許把數(shù)據(jù)包隨機(jī)從多個(gè)路由發(fā)出。weight 可以設(shè)置權(quán)重.
-
# ip route replace default equalize nexthop via 211.139.218.145 dev eth0 weight 1 nexthop via 211.139.218.145 dev eth2 weight 1
-
7.4.ip route delete-- 刪除路由
-
縮寫:delete、del、d
-
示例1:刪除上一節(jié)命令加入的多路徑路由
-
# ip route del default scope global nexthop dev ppp0 nexthop dev ppp1
-
7.5.ip route show -- 列出路由
-
縮寫:show、list、sh、ls、l
-
示例1: 計(jì)算使用gated/bgp協(xié)議的路由個(gè)數(shù)
-
# ip route ls proto gated/bgp |wc
-
1413 9891 79010
-
示例2: 計(jì)算路由緩存里面的條數(shù),由于被緩存路由的屬性可能大于一行,以此需要使用-o選項(xiàng)
-
# ip -o route ls cloned |wc
-
159 2543 18707
-
示例3: 列出路由表TABLEID里面的路由。缺省設(shè)置是table main。TABLEID或者是一個(gè)真正的路由表ID或者是/etc/iproute2/rt_tables文件定義的字符串,
-
或者是以下的特殊值:
-
all -- 列出所有表的路由;
-
cache -- 列出路由緩存的內(nèi)容。
-
ip ro ls 193.233.7.82 tab cache
-
示例4: 列出某個(gè)路由表的內(nèi)容
-
# ip route ls table fddi153
-
示例5: 列出默認(rèn)路由表的內(nèi)容
-
# ip route ls
-
這個(gè)命令等于傳統(tǒng)的: route
-
7.6.ip route flush -- 擦除路由表
-
示例1: 刪除路由表main中的所有網(wǎng)關(guān)路由(示例:在路由監(jiān)控程序掛掉之后):
-
# 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 -- 獲得單個(gè)路由 .縮寫:get、g
-
使用這個(gè)命令可以獲得到達(dá)目的地址的一個(gè)路由以及它的確切內(nèi)容。
-
ip route get命令和ip route show命令執(zhí)行的操作是不同的。ip route show命令只是顯示現(xiàn)有的路由,而ip route get命令在必要時(shí)會(huì)派生出新的路由。
-
示例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設(shè)備到達(dá)的路由(這條命令會(huì)產(chǎn)生一條非常有意思的路由,這是一條到193.233.7.82的回環(huán)路由)
-
# 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 -- 路由策略數(shù)據(jù)庫管理命令
-
命令
-
add、delete、show(或者list)
-
注意:策略路由(policy routing)不等于路由策略(rouing policy)。
-
在某些情況下,我們不只是需要通過數(shù)據(jù)包的目的地址決定路由,可能還需要通過其他一些域:源地址、IP協(xié)議、傳輸層端口甚至數(shù)據(jù)包的負(fù)載。
-
這就叫做:策略路由(policy routing)。
-
8.1. ip rule add -- 插入新的規(guī)則
-
ip rule delete -- 刪除規(guī)則
-
縮寫:add、a;delete、del、d
-
示例1: 通過路由表inr.ruhep路由來自源地址為192.203.80/24的數(shù)據(jù)包
-
ip ru add from 192.203.80/24 table inr.ruhep prio 220
-
示例2:把源地址為193.233.7.83的數(shù)據(jù)報(bào)的源地址轉(zhuǎn)換為192.203.80.144,并通過表1進(jìn)行路由
-
ip ru add from 193.233.7.83 nat 192.203.80.144 table 1 prio 320
-
示例3:刪除無用的缺省規(guī)則
-
ip ru del prio 32767
-
8.2. ip rule show -- 列出路由規(guī)則
-
縮寫: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
-
使用這兩個(gè)命令,我們可以添加/刪除在網(wǎng)絡(luò)接口上監(jiān)聽的鏈路層多播地址。這個(gè)命令只能管理鏈路層地址。
-
示例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 -- 修改現(xiàn)有的通道
-
ip tunnel delete -- 刪除一個(gè)通道
-
縮寫:add、a;change、chg;delete、del、d
-
示例1:建立一個(gè)點(diǎn)對(duì)點(diǎn)通道,最大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 -- 列出現(xiàn)有的通道
-
縮寫:show、list、sh、ls、l
-
示例1: # ip -s tunl ls Cisco
-
12. ip monitor和rtmon -- 狀態(tài)監(jiān)視
-
ip命令可以用于連續(xù)地監(jiān)視設(shè)備、地址和路由的狀態(tài)。這個(gè)命令選項(xiàng)的格式有點(diǎn)不同,命令選項(xiàng)的名字叫做monitor,接著是操作對(duì)象:
-
ip monitor [ file FILE ] [ all | OBJECT-LIST ]
-
示例1: # rtmon file /var/log/rtmon.log
-
示例2: # ip monitor file /var/log/rtmon.log r