在 CentOS 系统中,查看防火墙状态的方法取决于你使用的 CentOS 版本以及防火墙管理工具(主要是 firewalld 或 iptables)。
以下是几种常见的方法:
firewalld (CentOS 7/8/Stream 默认)这是现代 CentOS 版本(7 及以后)的标准防火墙服务。
查看防火墙运行状态:
sudo systemctl status firewalld
更简洁的查看方式(推荐):
sudo firewall-cmd --state
running 表示正在运行。not running 表示未运行。查看当前活动的区域和规则概览:
sudo firewall-cmd --list-all
iptables (CentOS 6 及更早版本,或手动安装)如果你使用的是 CentOS 6,或者手动禁用了 firewalld 而使用了传统的 iptables 服务。
查看当前规则(包含状态):
sudo iptables -L -n -v
查看服务状态(如果 iptables 是作为服务安装的):
sudo service iptables status
# 或者
sudo systemctl status iptables
有时候你不需要看防火墙软件本身,只需要看端口是否真的被拦截了。可以使用 ss 或 netstat 检查端口是否处于监听状态。
# 查看所有监听端口
sudo ss -tuln
# 或者
sudo netstat -tuln
| 场景 | 命令 |
|---|---|
| CentOS 7/8/Stream (最常用) | sudo firewall-cmd --state |
| 查看详细规则 | sudo firewall-cmd --list-all |
| CentOS 6 或旧系统 | sudo service iptables status |
| 查看端口是否开放 | sudo ss -tuln |
常见操作补充:
sudo systemctl start firewalldsudo systemctl stop firewalldsudo systemctl enable firewalldsudo systemctl disable firewalld