Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

iptables 防火牆規則大全:從入門到進階實戰範例集

編輯:Linux 教學

基礎篇:建立乾淨規則環境

# 清空現有規則
iptables -F

# 設定預設鏈政策 (拒絕所有,再逐項放行)
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP

常用服務放行範例

SSH 遠端管理

# 允許外部連入 SSH
iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

# 僅允許特定網段 (192.168.100.0/24)
iptables -A INPUT -i eth0 -p tcp -s 192.168.100.0/24 --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT

Web 服務 (HTTP/HTTPS)

# HTTP 80
iptables -A INPUT -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT

# HTTPS 443
iptables -A INPUT -i eth0 -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT

# 合併寫法:multiport
iptables -A INPUT -i eth0 -p tcp -m multiport --dports 22,80,443 -m state --state NEW,ESTABLISHED -j ACCEPT

ICMP (Ping)

# 允許外部 Ping 我方
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT

# 允許我方 Ping 外部
iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT

Loopback 介面 (必要)

iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

DNS 查詢 (UDP 53)

iptables -A OUTPUT -p udp -o eth0 --dport 53 -j ACCEPT
iptables -A INPUT -p udp -i eth0 --sport 53 -j ACCEPT

進階篇:NAT 與連接埠轉發

DNAT - 連接埠轉發 (例: 422 → 22)

# 1. 啟用 DNAT
iptables -t nat -A PREROUTING -p tcp -d 192.168.102.37 --dport 422 -j DNAT --to-destination 192.168.102.37:22

# 2. 放行轉發後的連線
iptables -A INPUT -i eth0 -p tcp --dport 422 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 422 -m state --state ESTABLISHED -j ACCEPT

SNAT - 來源位址轉換 (固定公網 IP)

iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j SNAT --to-source 192.168.5.3

MASQUERADE - 動態公網 IP (ADSL/寬頻撥號)

iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

路由轉發 (雙網卡閘道器)

# eth0=外網, eth1=內網
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
iptables -A FORWARD -i eth0 -o eth1 -m state --state ESTABLISHED,RELATED -j ACCEPT

# 核心參數啟用 IP 轉發
echo 1 > /proc/sys/net/ipv4/ip_forward

防禦篇:DoS 緩解與日誌記錄

限制 HTTP 連線頻率

iptables -A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT
  • --limit 25/minute:平均每分鐘 25 連線
  • --limit-burst 100:突發允許 100 連線,超過才啟用限制

自訂鏈記錄被丟棄封包

# 1. 建立 LOGGING 鏈
iptables -N LOGGING

# 2. 導流 INPUT 至 LOGGING
iptables -A INPUT -j LOGGING

# 3. 記錄 (限制頻率避免日誌爆炸)
iptables -A LOGGING -m limit --limit 2/min -j LOG --log-prefix "IPTables Dropped: " --log-level 7

# 4. 最終丟棄
iptables -A LOGGING -j DROP

負載平衡篇:統計模組 nth

# 將 HTTPS 流量平均分流到 3 台後端
iptables -A PREROUTING -i eth0 -p tcp --dport 443 -m state --state NEW -m nth --counter 0 --every 3 --packet 0 -j DNAT --to-destination 192.168.1.101:443
iptables -A PREROUTING -i eth0 -p tcp --dport 443 -m state --state NEW -m nth --counter 0 --every 3 --packet 1 -j DNAT --to-destination 192.168.1.102:443
iptables -A PREROUTING -i eth0 -p tcp --dport 443 -m state --state NEW -m nth --counter 0 --every 3 --packet 2 -j DNAT --to-destination 192.168.1.103:443

持久化儲存

# CentOS/RHEL
service iptables save

# Debian/Ubuntu
iptables-save > /etc/iptables/rules.v4
Copyright © Windows教程網 All Rights Reserved