路由表和靜態(tài)路由
內(nèi)容提要
-
查看 Linux 內(nèi)核路由表
-
使用 route 命令設(shè)置靜態(tài)路由
-
設(shè)置包轉(zhuǎn)發(fā)
Linux 內(nèi)核路由表
查看 Linux 內(nèi)核路由表
使用下面的 route 命令可以查看 Linux 內(nèi)核路由表。
# 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 | 目標(biāo)網(wǎng)段或者主機(jī) |
Gateway | 網(wǎng)關(guān)地址,”*” 表示目標(biāo)是本主機(jī)所屬的網(wǎng)絡(luò),不需要路由 |
Genmask | 網(wǎng)絡(luò)掩碼 |
Flags | 標(biāo)記。一些可能的標(biāo)記如下: |
|
U — 路由是活動的 |
|
H — 目標(biāo)是一個主機(jī) |
|
G — 路由指向網(wǎng)關(guān) |
|
R — 恢復(fù)動態(tài)路由產(chǎn)生的表項 |
|
D — 由路由的后臺程序動態(tài)地安裝 |
|
M — 由路由的后臺程序修改 |
|
! — 拒絕路由 |
Metric | 路由距離,到達(dá)指定網(wǎng)絡(luò)所需的中轉(zhuǎn)數(shù)(linux 內(nèi)核中沒有使用) |
Ref | 路由項引用次數(shù)(linux 內(nèi)核中沒有使用) |
Use | 此路由項被路由軟件查找的次數(shù) |
Iface | 該路由表項對應(yīng)的輸出接口 |
3 種路由類型
主機(jī)路由
主機(jī)路由是路由選擇表中指向單個IP地址或主機(jī)名的路由記錄。主機(jī)路由的Flags字段為H。例如,在下面的示例中,本地主機(jī)通過IP地址192.168.1.1的路由器到達(dá)IP地址為10.0.0.10的主機(jī)。
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
網(wǎng)絡(luò)路由
網(wǎng)絡(luò)路由是代表主機(jī)可以到達(dá)的網(wǎng)絡(luò)。網(wǎng)絡(luò)路由的Flags字段為N。例如,在下面的示例中,本地主機(jī)將發(fā)送到網(wǎng)絡(luò)192.19.12的數(shù)據(jù)包轉(zhuǎn)發(fā)到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
默認(rèn)路由
當(dāng)主機(jī)不能在路由表中查找到目標(biāo)主機(jī)的IP地址或網(wǎng)絡(luò)路由時,數(shù)據(jù)包就被發(fā)送到默認(rèn)路由(默認(rèn)網(wǎng)關(guān))上。默認(rèn)路由的Flags字段為G。例如,在下面的示例中,默認(rèn)路由是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
配置靜態(tài)路由
route 命令
設(shè)置和查看路由表都可以用 route 命令,設(shè)置內(nèi)核路由表的命令格式是:
# route [add|del] [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]
其中:
-
add : 添加一條路由規(guī)則
-
del : 刪除一條路由規(guī)則
-
-net : 目的地址是一個網(wǎng)絡(luò)
-
-host : 目的地址是一個主機(jī)
-
target : 目的網(wǎng)絡(luò)或主機(jī)
-
netmask : 目的地址的網(wǎng)絡(luò)掩碼
-
gw : 路由數(shù)據(jù)包通過的網(wǎng)關(guān)
-
dev : 為路由指定的網(wǎng)絡(luò)接口
route 命令使用舉例
添加到主機(jī)的路由
# route add -host 192.168.1.2 dev eth0:0
# route add -host 10.20.30.148 gw 10.20.30.40
添加到網(wǎng)絡(luò)的路由
# route add -net 10.20.30.40 netmask 255.255.255.248 eth0
# route add -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41
# route add -net 192.168.1.0/24 eth1
添加默認(rèn)路由
# route add 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
設(shè)置包轉(zhuǎn)發(fā)
在 CentOS 中默認(rèn)的內(nèi)核配置已經(jīng)包含了路由功能,但默認(rèn)并沒有在系統(tǒng)啟動時啟用此功能。開啟 Linux 的路由功能可以通過調(diào)整內(nèi)核的網(wǎng)絡(luò)參數(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)就能實現(xiàn)包轉(zhuǎn)發(fā),但下次啟動計算機(jī)時將失效。為了使在下次啟動計算機(jī)時仍然有效,需要將下面的行寫入配置文件/etc/sysctl.conf。
# vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
用戶還可以使用如下的命令查看當(dāng)前系統(tǒng)是否支持包轉(zhuǎn)發(fā)。
# sysctl net.ipv4.ip_forward