萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux 手動關閉防火牆完整指南:iptables、firewalld、ufw 多系統適用

防火牆是系統安全第一道防線,但測試、調試或特定服務部署時常需臨時關閉。本文涵蓋 CentOS/RHEL(iptables、firewalld)、Fedora、Ubuntu(ufw/iptables)等主流發行版的臨時關閉與永久關閉方法,並強調生產環境操作風險與最佳實踐。

一、CentOS 6 / RHEL 6(基於 iptables service)

類型開啟關閉
永久(重啟生效)chkconfig iptables onchkconfig iptables off
即時(重啟失效)service iptables startservice iptables stop

二、CentOS 7 / RHEL 7 / Fedora(firewalld)

類型命令
查看狀態firewall-cmd --state / systemctl status firewalld
即時關閉systemctl stop firewalld
永久關閉systemctl disable firewalld
開放端口(永久)firewall-cmd --permanent --add-port=80/tcp && firewall-cmd --reload

三、Ubuntu / Debian(ufw / iptables)

ufw(推薦)

  • 啟用:ufw enable
  • 禁用:ufw disable
  • 開放端口:ufw allow 22/tcp
  • 查看規則:ufw status numbered

直接操作 iptables(通用、彈性最大)

安全關閉步驟(避免默認 DROP 鎖死 SSH):

iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F          # 清空所有規則
iptables -X          # 刪除用戶自定義鏈

保存規則: iptables-save > /etc/iptables/rules.v4(需安裝 iptables-persistent)

四、通用圖形化工具

Root 下執行 setup → Firewall configuration → Security Level 選 Disabled → 保存重啟。

五、生產環境警示與最佳實踐

  1. 切勿直接 iptables -F: 若默認鏈策略為 DROP,清空規則會阻斷所有連接(含 SSH)。
  2. 先設默認 ACCEPT 再清規則,如上節所示。
  3. 變更前必須在測試環境驗證,確認業務無影響。
  4. 採用白名單策略:默認 DROP,僅開放必要端口(22、80、443 等)。
  5. 使用 firewalld/ufw 管理規則,避免手工 iptables 易出錯;複雜場景可用 ferm、shorewall 等高層工具。

Linux 防火牆管理示意圖

copyright © 萬盛學電腦網 all rights reserved