Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

CentOS 開放 80/8080 端口實戰:iptables 配置文件修改與服務重載驗證

編輯:Linux 系統常見問題解答

新裝 CentOS 默認僅開放 22 端口,部署 Nginx/Tomcat 後需手動開放 Web 端口。本文記錄修改 /etc/sysconfig/iptables 開放 80/8080、端口範圍、驗證生效的標準化流程。

1. 確認現狀

iptables -L -n
# 無 80/8080 ACCEPT 規則

2. 修改配置文件(推薦,永久生效)

vim /etc/sysconfig/iptables
# 在 COMMIT 前插入:
-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 8080 -j ACCEPT
# 或端口範圍:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3000:5000 -j ACCEPT

3. 重載防火牆

service iptables restart
# 或
/etc/init.d/iptables restart

4. 驗證生效

iptables -L -n -v --line-numbers | grep -E '80|8080'
# 應見 ACCEPT tcp dpt:80 / dpt:8080

5. 服務管理對照(SysVinit vs systemd)

操作SysVinit (CentOS 6)systemd (CentOS 7/8)
立即啟動service iptables startsystemctl start iptables
立即關閉service iptables stopsystemctl stop iptables
開機自啟chkconfig iptables onsystemctl enable iptables
開機不啟動chkconfig iptables offsystemctl disable iptables

6. 替代方案:firewalld (CentOS 7+ 默認)

firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
firewall-cmd --list-ports

按發行版選擇 iptables 或 firewalld,遵循「配置文件/命令修改 → 重載 → 驗證」三步走,確保端口策略準確生效。

Copyright © Windows教程網 All Rights Reserved