萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

Linux 防火牆關閉與管理完整指南

永久生效方式(重啟後保持)

# 開啟
chkconfig iptables on
# 關閉
chkconfig iptables off

即時生效方式(重啟後失效)

# 開啟
service iptables start
# 關閉
service iptables stop

以上命令對其他服務同樣適用(如 httpd、mysqld 等)。

開放常用端口(防火牆開啟時)

編輯 /etc/sysconfig/iptables,添加規則:

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

或使用服務管理命令:

/etc/init.d/iptables status  # 查看狀態
/etc/init.d/iptables stop      # 關閉

圖形界面關閉(適用於桌面環境)

以 root 執行 setup → 選擇 Firewall configuration → 將 Security Level 設為 Disabled → 保存並重啟。

不同發行版特殊處理

Fedora / RHEL / CentOS

/etc/init.d/iptables stop

Ubuntu / Debian

無專用服務命令,建議用以下安全方式關閉:

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

警告:直接用 iptables -F 清空規則極危險!若默認策略為 DROP,會導致所有連接(含 SSH)中斷。務必先設置默認策略為 ACCEPT 再清空規則。

最佳實踐建議

  • 生產環境修改前,必須在測試環境驗證。
  • 深入理解 iptables 處理數據包流程(鏈、表、規則匹配順序)。
  • 優先使用 firewall-cmd (CentOS 7+) 或 ufw (Ubuntu) 等高級工具管理規則。
  • 保留遠程管理端口(如 22)規則,防止自己被鎖在外面。
copyright © 萬盛學電腦網 all rights reserved