萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 網路安全 >  >> 資訊安全防護

CentOS 7 關閉 firewalld 改用 iptables 完整指令教學

CentOS 7 預設改用 firewalld,如何切回傳統 iptables?

CentOS 7 預設啟用 firewalld(動態防火牆),操作指令與 iptables 不同。若習慣 iptables 靜態規則,可按以下步驟切換:

1. 停用並屏蔽 firewalld

systemctl stop firewalld
systemctl mask firewalld   # 防止開機自動啟動

2. 安裝 iptables 服務套件

yum -y install iptables-services

3. 設定開機自啟並啟動服務

systemctl enable iptables
systemctl enable ip6tables   # 若需 IPv6
systemctl start iptables
systemctl start ip6tables

4. 編輯規則開放常用埠(以 HTTP/HTTPS/SSH 為例)

vi /etc/sysconfig/iptables

在 *filter 區塊加入:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT

存檔後重啟生效:systemctl restart iptables

5. 驗證規則

iptables -L -n -v --line-numbers

注意事項

  • firewalld 與 iptables 不可同時運行,mask 後前者完全失效。
  • 生產環境修改前請備份 /etc/sysconfig/iptables。
  • 雲端主機(阿里雲、騰訊雲)另設有安全群組,需雙向放行。
copyright © 萬盛學電腦網 all rights reserved