在 CentOS 中,防火墙通常使用 firewalld(CentOS 7 及以后版本)。下面从基础概念 → 常用操作 → 实战示例给你讲清楚。
systemctl status firewalld
# 启动
systemctl start firewalld
# 停止
systemctl stop firewalld
# 开机自启
systemctl enable firewalld
# 关闭开机自启
systemctl disable firewalld
常用 zone:
public:默认外网区域trusted:完全信任(所有流量允许)# 查看当前 zone
firewall-cmd --get-active-zones
# 查看 public 区域规则
firewall-cmd --zone=public --list-all
firewall-cmd --zone=public --add-port=80/tcp
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
# 开放 SSH
firewall-cmd --zone=public --add-service=ssh --permanent
# 开放 HTTP
firewall-cmd --zone=public --add-service=http --permanent
# 开放 HTTPS
firewall-cmd --zone=public --add-service=https --permanent
firewall-cmd --reload
# 关闭端口
firewall-cmd --zone=public --remove-port=8080/tcp --permanent
# 关闭服务
firewall-cmd --zone=public --remove-service=http --permanent
firewall-cmd --reload
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
firewall-cmd --permanent --add-rich-rule='
rule family="ipv4"
source address="192.168.1.100"
service name="ssh"
accept'
firewall-cmd --reload
firewall-cmd --permanent --add-forward-port=port=8080:proto=tcp:toport=80
firewall-cmd --reload
如果是 CentOS 6:
# 开放 80 端口
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
service iptables restart
--reloadfirewall-cmd --list-all如果你有具体需求(比如:开放某个端口、限制 IP、Docker 场景),告诉我,我可以直接给你命令。