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

iptables 規則重置為預設值:一鍵清空腳本

編輯:關於伺服器

一鍵重置腳本 (生產環境謹慎執行!)

#!/bin/bash
# reset_iptables.sh - 重置 iptables 為預設允許所有

IPT=/usr/sbin/iptables

# 1. 設定預設政策為 ACCEPT
$IPT -P INPUT ACCEPT
$IPT -P FORWARD ACCEPT
$IPT -P OUTPUT ACCEPT

# 2. NAT 表預設政策
$IPT -t nat -P PREROUTING ACCEPT
$IPT -t nat -P POSTROUTING ACCEPT
$IPT -t nat -P OUTPUT ACCEPT

# 3. 清空所有規則 (filter & nat)
$IPT -F
$IPT -t nat -F

# 4. 刪除所有非預設鏈
$IPT -X
$IPT -t nat -X

echo "iptables 已重置為預設值 (全開放)"
$IPT -L -n -v

執行方式

chmod +x reset_iptables.sh
./reset_iptables.sh

分表說明

表別預設鏈用途
filter (預設)INPUT, FORWARD, OUTPUT封包過濾、防火牆核心
natPREROUTING, POSTROUTING, OUTPUT網址轉換 (DNAT/SNAT/Masquerade)
manglePREROUTING, INPUT, FORWARD, OUTPUT, POSTROUTING封包標記、TOS、TTL 修改
rawPREROUTING, OUTPUT連線追蹤豁免 (NOTRACK)
securityINPUT, FORWARD, OUTPUTSELinux 安全標記

進階:僅重置 filter 表 (保留 NAT/轉發規則)

iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
iptables -X

持久化儲存 (重開機生效)

# CentOS/RHEL 7+
service iptables save
# 或
iptables-save > /etc/sysconfig/iptables

# Ubuntu/Debian
iptables-save > /etc/iptables/rules.v4
# 需安裝 iptables-persistent

⚠️ 生產環境警示

執行前必須確認:
1. 具備主機板/遠程管理 (IPMI/iDRAC/iLO) 存取權
2. 已備份現有規則:iptables-save > ~/iptables.backup.$(date +%F)
3. 確認 SSH 連線不會中斷 (重置後預設允許所有,但若在腳本中斷點前斷線...)

相關教學連結

完整 iptables 配置教學:《iptables配置》 (站內搜尋)

Copyright © Windows教程網 All Rights Reserved