作為運(yùn)維工程師,IPTables 作為 Linux 系統(tǒng)的核心防火墻工具,其規(guī)則配置的合理性直接影響服務(wù)器安全與網(wǎng)絡(luò)性能。本文整理 25 個(gè)高頻場(chǎng)景規(guī)則示例,結(jié)合深度解析與解決方案,助你構(gòu)建高效安全的網(wǎng)絡(luò)防護(hù)體系。
一、基礎(chǔ)規(guī)則配置
- 初始化防火墻
iptables -F && iptables -X && iptables -Z? # 清空所有規(guī)則
iptables -P INPUT DROP? # 設(shè)置默認(rèn)策略為拒絕
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
解決方案:初始化規(guī)則鏈并設(shè)置嚴(yán)格默認(rèn)策略,避免配置漏洞。
- 允許本地回環(huán)通信
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
?場(chǎng)景:保障本地進(jìn)程間通信(如localhost服務(wù)調(diào)用)。
- 阻止特定 IP 訪問
iptables -A INPUT -s 192.168.1.100 -j DROP
解決方案:快速封禁惡意 IP,結(jié)合日志分析工具(如iptables -L -v)定位異常流量。
二、服務(wù)訪問控制
- 允許 SSH 訪問(僅指定網(wǎng)段)
iptables -A INPUT -p tcp --dport 22 -s 10.0.0.0/24 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
場(chǎng)景:限制 SSH 登錄權(quán)限,僅允許公司內(nèi)網(wǎng) IP 訪問。
- 開放 Web 服務(wù)(HTTP/HTTPS)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
優(yōu)化方案:使用multiport模塊合并規(guī)則:
iptables -A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT
- 允許 DNS 查詢
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --sport 53 -j ACCEPT
解決方案:保障域名解析服務(wù)正常運(yùn)行。
三、網(wǎng)絡(luò)安全防護(hù)
- 防范 DoS 攻擊
iptables -A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT
參數(shù)解析:限制每分鐘 25 個(gè)新連接,突發(fā)流量不超過 100 個(gè)。
- 阻止 ICMP 泛洪
iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/second -j ACCEPT
?場(chǎng)景:防止 Ping 洪流導(dǎo)致網(wǎng)絡(luò)阻塞。
- 記錄丟棄數(shù)據(jù)包
iptables -N LOGGING
iptables -A INPUT -j LOGGING
iptables -A LOGGING -m limit --limit 2/min -j LOG --log-prefix "DROPPED_PACKET: " --log-level 7
iptables -A LOGGING -j DROP
解決方案:通過 syslog 記錄丟棄包,便于安全審計(jì)。
四、網(wǎng)絡(luò)地址轉(zhuǎn)換(NAT)
- 端口轉(zhuǎn)發(fā)(SSH 映射)
iptables -t nat -A PREROUTING -p tcp --dport 422 -j DNAT --to-destination 192.168.1.200:22
iptables -A FORWARD -p tcp -d 192.168.1.200 --dport 22 -j ACCEPT
場(chǎng)景:將公網(wǎng) 422 端口映射到內(nèi)網(wǎng)服務(wù)器 22 端口。
- 共享上網(wǎng)(SNAT)
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE
解決方案:內(nèi)網(wǎng)設(shè)備通過防火墻共享公網(wǎng) IP 訪問互聯(lián)網(wǎng)。
五、高級(jí)應(yīng)用
- 負(fù)載均衡(HTTP 流量分發(fā))
iptables -t nat -A PREROUTING -p tcp --dport 80 -m nth --counter 0 --every 3 --packet 0 -j DNAT --to-destination 192.168.1.101:80
iptables -t nat -A PREROUTING -p tcp --dport 80 -m nth --counter 0 --every 3 --packet 1 -j DNAT --to-destination 192.168.1.102:80
場(chǎng)景:將 HTTP 流量輪詢分發(fā)到兩臺(tái)后端服務(wù)器。
- 基于時(shí)間的訪問控制
iptables -A INPUT -p tcp --dport 22 -m time --timestart 08:00 --timestop 18:00 -j ACCEPT
解決方案:限制 SSH 僅在工作日辦公時(shí)間可用。
- 多網(wǎng)卡策略路由
iptables -A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT? # 公網(wǎng)接口開放HTTP
iptables -A INPUT -i eth1 -p tcp --dport 8080 -j ACCEPT? # 內(nèi)網(wǎng)接口開放管理端口
場(chǎng)景:根據(jù)流量來源接口實(shí)施差異化訪問控制。
六、規(guī)則管理與優(yōu)化
- 保存與恢復(fù)規(guī)則
iptables-save > /etc/iptables/rules.v4? # 保存規(guī)則
iptables-restore < /etc/iptables/rules.v4? # 恢復(fù)規(guī)則
解決方案:避免重啟后規(guī)則丟失。
- 可視化規(guī)則鏈
使用iptable_vis工具生成流程圖:
iptables -L -n --line-numbers | iptable_vis -o iptables.png
工具優(yōu)勢(shì):直觀展示規(guī)則鏈邏輯,快速定位配8置錯(cuò)誤。
?? 解決方案總結(jié)
場(chǎng)景 |
核心規(guī)則示例 |
工具 / 模塊 |
基礎(chǔ)防護(hù) |
初始化規(guī)則、設(shè)置默認(rèn)策略 |
iptables -F,?-P |
服務(wù)訪問控制 |
限制 SSH/HTTP 訪問源 IP、合并多端口規(guī)則 |
multiport |
安全防護(hù) |
DoS 防范、ICMP 限速、日志記錄 |
limit,?LOG |
NAT 與端口轉(zhuǎn)發(fā) |
端口映射、共享上網(wǎng) |
DNAT,?MASQUERADE |
高級(jí)應(yīng)用 |
負(fù)載均衡、時(shí)間控制、多網(wǎng)卡策略 |
nth,?time |
規(guī)則管理 |
保存恢復(fù)、可視化 |
iptables-save,?iptable_vis |