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 5.8 iptables 防火牆管理實戰:解決 8080 端口無法訪問

編輯:Linux 教學

在 CentOS 5.8 環境部署 Tomcat 時,啟動成功但 8080 端口無法訪問,排查發現是 iptables 默認規則阻攔。本文記錄完整排查思路與解決方案,並對比 CentOS 6.x 的變化。

一、 現象與初步排查

環境:第三方提供的 CentOS 5.8 VMX 虛擬機。
Tomcat 啟動無報錯,但 curl localhost:8080 無響應,瀏覽器顯示連接超時。

1. 排除 SELinux

# 查看 SELinux 狀態
sestatus

# 查看端口策略
semanage port -l | grep 8080

確認 SELinux 未阻擋 8080 端口後,轉向防火牆排查。

二、 定位 iptables 問題

CentOS 5.x 引入一個特殊的用戶自定義鏈 RH-Firewall-1-INPUT,所有 INPUT 和 FORWARD 流量均跳轉至該鏈:

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

該鏈默認只放行有限服務(SSH、ICMP 等),導致 8080 端口被攔截。注意:CentOS 6.x 已廢除此鏈,直接使用標準鏈管理。

三、 解決方案(測試環境適用)

由于是測試環境,選擇清空所有規則:

# 清空規則、刪除用戶鏈、歸零計數器
iptables -F
iptables -X
iptables -Z

# 查看當前規則
iptables -L -n

# 保存規則
service iptables save

# 重啟服務驗證
service iptables restart
iptables -L -n

執行後 8080 端口即可正常訪問。

四、 生產環境建議:精準放行而非清空

生產環境嚴禁直接清空規則,應精準添加允許規則:

# 方法 1:插入到 RH-Firewall-1-INPUT 鏈首部
iptables -I RH-Firewall-1-INPUT -p tcp --dport 8080 -j ACCEPT

# 方法 2:直接在 INPUT 鏈添加(需在跳轉規則之前)
iptables -I INPUT -p tcp --dport 8080 -j ACCEPT

# 保存並重啟
service iptables save && service iptables restart

五、 參考資源

Red Hat 官方文檔詳細說明了防火牆配置:
Firewall Configuration Guide

六、 核心知識點總結

  • CentOS 5.x 使用 RH-Firewall-1-INPUT 統一管理入站轉發流量。
  • iptables -F/-X/-Z 是「核彈級」操作,僅限測試環境。
  • 生產環境務必使用 -I 插入規則,配合 service iptables save 持久化。
  • CentOS 7+ 推薦使用 firewalld 或 nftables,語法更直觀。
Copyright © Windows教程網 All Rights Reserved