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 防火牆開啟/關閉埠口完整指令:iptables 與 firewall-cmd 兩種方式

編輯:Linux 教學

CentOS 6 使用 iptables,CentOS 7/8 預設 firewalld(底層仍為 iptables/nftables)。本文提供兩種工具的開啟、關閉、查看埠口指令。

一、查看埠口佔用

# 查看特定埠 (如 80)
lsof -i tcp:80
netstat -ntlp | grep :80
ss -ntlp | grep :80

二、CentOS 6 (iptables 傳統指令)

方法 A:命令列直接修改

# 開啟 80
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
service iptables restart

# 關閉 80
iptables -I INPUT -p tcp --dport 80 -j DROP
service iptables save
service iptables restart

方法 B:編輯設定檔

vi /etc/sysconfig/iptables
# 加入開啟規則
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
# 或關閉規則
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j DROP
service iptables restart

查看狀態

/etc/init.d/iptables status

三、CentOS 7/8 (firewall-cmd)

# 立即生效(重啟失效)
firewall-cmd --add-port=80/tcp
firewall-cmd --remove-port=80/tcp

# 永久生效
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --remove-port=80/tcp
firewall-cmd --reload

# 查看
firewall-cmd --list-all
firewall-cmd --list-ports

四、常用埠口速查

服務埠口協定
HTTP80TCP
HTTPS443TCP
SSH22TCP
MySQL3306TCP
DNS53TCP/UDP

依據系統版本選擇對應工具,並養成「永久生效 + reload」習慣,避免重啟後規則遺失。

Copyright © Windows教程網 All Rights Reserved