Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux 進階路由實戰:策略路由、網卡綁定與網橋

傳統路由只認目的位址,當網路與需求變得複雜時就會力不從心。策略路由(Policy Routing)給了我們更靈活的選擇:除了目的位址,還能依來源 IP、服務類型、報文大小甚至防火牆標記來決定轉送路徑。Linux 從 2.1 版核心起就支援這套機制,工具則是 iproute2。

一、策略路由

路由表定義放在 /etc/iproute2/rt_tables,系統預設有三張表:

255 local——本地路由表,存放本地介面位址、廣播位址與 NAT 位址,由系統自動維護,管理員不可直接更動。

254 main——主路由表,傳統路由表。ip route 未指定表時操作的就是它,平時用 ip ro sh 看到的也是這張表。

253 default——預設路由表,通常存放預設路由。

表以數字編號區分,0 保留,最多支援 255 張。查表方式有兩種:

ip route list table table_number
ip route list table table_name

新增路由的寫法:

ip route add 192.168.1.0/24 dev eth0 via 192.168.1.66 realm 4
ip route add default via 192.168.1.1 table int1
ip route add 192.168.1.0/24 via 192.168.1.1 table int2
ip route add 172.16.0.0/16 via 172.16.0.1 table int3

實務上每張自訂表都應該指明一條預設路由,盡量避免回查其他表造成路徑不確定性。

規則(rule)

路由表備好之後,用 ip rule 讓它們生效:

ip rule sh
0:     from all lookup local
32766: from all lookup main
32767: from all lookup default

查路由時依 pref 數值從小到大(優先級由高到低)逐條比對,直到找到符合的規則。新增規則的範例:

ip rule add from 192.168.1.112/32 [tos 0x10] table test2 pref 999 prohibit
ip rule add to 192.168.1.2 pref 1000 table test1
ip rule add from 192.168.1.0/24 pref 1001 table test1
ip rule add fwmark 1 pref 1002 table test2

其中 fwmark 要搭配 iptables 的 mangle 表使用,先標記再導流:

iptables -t mangle -A PREROUTING -p tcp -m multiport \
  --dports 80,8080,20,21 -s 192.168.1.0/24 -j MARK --set-mark 1

規則建好後若要立即生效,記得刷新路由快取:

ip route flush cache

選擇條件(SELECTOR)可用的欄位:

from 來源位址、to 目的位址、tos IP 標頭的服務類型、dev 實體介面、fwmark 防火牆標記。

可採取的動作(ACTION):table 指定查表、nat 透明閘道、prohibit 丟棄並回送 COMM.ADM.PROHIBITED、reject 直接丟棄、unreachable 丟棄並回送 NET UNREACHABLE。

Usage: ip rule [ list | add | del ] SELECTOR ACTION

二、網卡綁定(bonding)

modprobe bonding mode=1 miimon=200 primary=eth1
ip addr add dev bond0 10.0.0.1/24
ifenslave bond0 eth1 eth2

常見模式:

mode=0 輪轉平衡(預設),MAC 頻繁切換會增加交換機負載。

mode=1 主備模式,用 primary 指定主網卡。

mode=4 802.3ad 動態鏈路聚合,需要交換機端支援並完成捆綁設定。

miimon 是鏈路檢測間隔(毫秒);另有 arp_interval 與 arp_ip_target 等參數可用。

要讓設定開機固化,可在 /etc/modules 或 modules.conf 中加入 alias bond0 bonding,然後編輯 /etc/sysconfig/network-scripts/ 下的 ifcfg-bond0、ifcfg-eth0、ifcfg-eth1,把 bond0 設為 DEVICE=bond0 並填好 IP、NETMASK、ONBOOT=yes,兩張實體網卡則寫 MASTER=bond0、SLAVE=yes。重啟網路服務後可用 cat /proc/net/bonding/bond0 檢視綁定狀態。

三、網橋(bridge)

工具是 bridge-utils(原始碼包名 bridge-utils-*,先跑 autoconf 再照一般原始碼流程安裝)。以兩張網卡 eth0、eth1 組橋為例,先把原介面的 IP 清掉:

ifconfig eth0 down; ifconfig eth0 0.0.0.0 up
ifconfig eth1 down; ifconfig eth1 0.0.0.0 up
brctl addbr br0
brctl addif br0 eth0 eth1
ip li set dev br0 up
brctl showmacs br0

拆除網橋:

ip li set dev br0 down
brctl delif br0 eth0 eth1
brctl delbr br0

網橋介面同樣能做流量控制;而沒有 IP 的 Linux 網橋能有效降低被攻擊的風險,整體安全性更高。一個啟動腳本示例 start_bridge.sh:

#!/bin/sh
brctl addbr net1
brctl addif net1 eth0
ifconfig eth0 down
ifconfig eth0 0.0.0.0 up
ifconfig net1 192.168.5.1 up
ip route del 192.168.1.0/24
ip route add default via 192.168.5.2
echo "1" > /proc/sys/net/ipv4/ip_forward

要讓進階路由真正發揮威力,通常還得搭配 iptables 做標記與過濾,這部分需要另外深入學習與實作累積經驗。

完整案例:多網卡策略路由器

以下實例曾在 2.4G Pentium 4、256MB 記憶體的機器上跑通,並在 160 多台電腦的環境中穩定運作,系統為 Red Hat Enterprise Linux WS 3(核心 2.4.21)。

需求:內網 1 只能透過遠端路由器 1(172.22.254.254)連外,且上班時間(07:30–16:30)僅放行 HTTP 與 FTP,其餘協定於下班後與週末開放;內網 1 不得訪問內網 2 與上級網,但可存取內網 2 上的伺服器。內網 2 可訪問外網;上級網只能訪問 192.168.1.2 這台伺服器。防火牆則負責阻擋外網主動進入內網。

1. 設定 IP

ifconfig eth0 10.89.9.1 netmask 255.255.255.0
ifconfig eth1 192.168.1.1 netmask 255.255.255.0
ifconfig eth2 172.22.254.14 netmask 255.255.255.0
ifconfig eth3 10.140.133.14 netmask 255.255.255.0

接著編輯 /etc/sysconfig/network-scripts/ifcfg-eth0~ifcfg-eth3,把 ONBOOT 設為 yes;也可以直接在圖形介面的「系統設定 → 網路」中設定並勾選開機啟用。

2. 開啟轉送

echo "1" > /proc/sys/net/ipv4/ip_forward

或在 /etc/sysconfig/network 中加入 FORWARD_IPV4=yes。

3. 建立路由表——於 /etc/iproute2/rt_tables 尾端新增 int1~int4:

1 int1
2 int2
3 int3
4 int4

4. 加入路由

ip route add default via 10.89.9.1 table int1
ip route add default via 192.168.1.1 table int2
ip route add default via 172.22.254.254 table int3
ip route add 192.168.0.0/16 via 10.140.133.254 table int4
ip route add default via 172.22.254.254 table int4

5. 標記特殊封包

iptables -t mangle -A PREROUTING -p tcp -m multiport \
  --dports 80,8080,20,21 -s 10.89.9.0/24 -j MARK --set-mark 1
iptables -t mangle -A PREROUTING -p udp --dport 53 \
  -s 10.89.9.0/24 -j MARK --set-mark 2
iptables -t mangle -A PREROUTING -p ALL -d 10.89.9.0/24 \
  -m state --state ESTABLISHED,RELATED -j MARK --set-mark 3
iptables -t mangle -A PREROUTING -p ALL -d 192.168.1.0/24 \
  -m state --state ESTABLISHED,RELATED -j MARK --set-mark 4

6. 建立規則

ip rule add from 192.168.1.0/24 pref 11 table int4
ip rule add to 192.168.1.2 pref 21 table int2
ip rule add fwmark 4 pref 31 table int2
ip rule add fwmark 1 pref 41 table int3
ip rule add fwmark 2 pref 42 table int3
ip rule add fwmark 3 pref 51 table int1
ip route flush cache

7. 分時段放行——建立兩個腳本:

# /bin/ropen
/sbin/ip rule add from 10.89.9.0/24 pref 40 table int3
/sbin/ip route flush cache

# /bin/rclose
/sbin/ip rule del from 10.89.9.0/24 pref 40
/sbin/ip route flush cache

加上執行權限 chmod +x ropen rclose,再用 crontab 定時切換。cron 格式為「分 時 日 月 週 命令」,各欄範圍分別是 00–59、00–23、01–31、01–12、01–07,不需要的欄位用 * 帶過:

30 07 * * 01,02,03,04,05 "/bin/ropen"
30 16 * * 01,02,03,04,05 "/bin/rclose"

最後執行 crontab mycron 載入排程,分時段管控就完成了。

Copyright © Linux教程網 All Rights Reserved