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

CentOS 6/7 防火牆 iptables 管理全攻略:開放指定埠、儲存規則、永久生效與最佳實踐

編輯:Linux 教學

iptables 是 Linux 核心級封包過濾框架,CentOS 6 預設使用 iptables service,CentOS 7 雖引入 firewalld 但仍可透過 iptables-services 繼續使用傳統指令。本文系統整理:查看狀態、開放/關閉埠、儲存規則、重啟生效、永久關閉、配置檔直接編輯、完整範例規則集,並說明 INPUT、FORWARD、RH-Firewall-1-INPUT 鏈路關係,助您建立安全且可維護的防火牆策略。

一、核心管理指令速查

操作CentOS 6CentOS 7 (iptables-services)
查看狀態/etc/init.d/iptables statussystemctl status iptables
啟動/etc/init.d/iptables startsystemctl start iptables
停止/etc/init.d/iptables stopsystemctl stop iptables
重啟/etc/init.d/iptables restartsystemctl restart iptables
儲存規則/etc/rc.d/init.d/iptables savesystemctl restart iptables (自動儲存至 /etc/sysconfig/iptables)
開機自啟chkconfig iptables onsystemctl enable iptables

二、開放指定埠 (兩種等效方式)

方式 A:命令列即時生效 + 手動儲存

# 開放 TCP 8080
/sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
# 儲存
/etc/rc.d/init.d/iptables save
# 重啟生效
/etc/init.d/iptables restart

方式 B:直接編輯配置檔 (建議,版本可控)

vi /etc/sysconfig/iptables
# 在 *filter 區塊中,COMMIT 之前插入:
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT
# 或簡寫 (不檢查 state):
-A INPUT -p tcp --dport 8080 -j ACCEPT
# 存檔後重啟
service iptables restart

三、完整範例規則集 /etc/sysconfig/iptables

# Firewall configuration written by system-config-securitylevel
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]

-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT

# 允許本地回環
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
# 允許 ICMP (ping, traceroute 等)
-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
# IPsec ESP/AH
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
# 已建立/相關連線放行
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 服務埠 (依需求啟用)
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT   # SSH
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 53 -j ACCEPT   # DNS TCP
-A RH-Firewall-1-INPUT -m state --state NEW -m udp -p udp --dport 53 -j ACCEPT   # DNS UDP
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT   # HTTP
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT  # HTTPS
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 25 -j ACCEPT   # SMTP
# 自訂埠範例
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT

# 預設拒絕並回應 icmp-host-prohibited
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited

COMMIT

四、關鍵鏈路架構說明

  • INPUT:進入本機的封包
  • FORWARD:經由本機轉發的封包 (路由/NAT)
  • RH-Firewall-1-INPUT:自訂鏈,集中管理規則,INPUT 與 FORWARD 皆跳轉至此
  • 規則由上至下匹配,首次匹配即決定動作
  • -I INPUT 插入最前,-A INPUT 附加最後

五、常見操作場景

需求指令
暫時關閉防火牆 (測試用)systemctl stop iptables
永久關閉 (不推薦生產環境)systemctl disable iptables
刪除某條規則iptables -D INPUT -p tcp --dport 8080 -j ACCEPT
查看含行號規則iptables -L -n --line-numbers
限制單 IP 存取 SSHiptables -I INPUT -s 192.168.1.100 -p tcp --dport 22 -j ACCEPT
開放 Docker 容器網段iptables -I FORWARD -s 172.17.0.0/16 -j ACCEPT

六、最佳實踐建議

  1. 白名單策略:預設 DROP,僅開放必要埠。
  2. 保留管理通道:務必在規則最前放行 SSH (22) 或 VNC/IPMI 管理埠,避免鎖死自己。
  3. 版本控制:/etc/sysconfig/iptables 納入 Git 管理,變更留痕。
  4. 測試後再儲存:先用 -I 即時生效驗證,確認無誤再 save。
  5. IPv6 同步:同步配置 ip6tables,編輯 /etc/sysconfig/ip6tables。
Copyright © Windows教程網 All Rights Reserved