在 Linux 系统中,防火墙通常由 iptables、firewalld 或 ufw 管理。以下是查看防火墙状态的常用方法:
1. 检查防火墙是否启用
1.1 使用 systemctl 检查防火墙服务状态
-
对于 firewalld: - sudo systemctl status firewalld
复制代码如果显示 active (running),表示 firewalld 正在运行。如果显示inactive (dead),表示firewalld未运行。 -
对于 ufw(Ubuntu/Debian): - sudo systemctl status ufw
复制代码如果显示 active (running),表示 ufw 正在运行。 -
对于 iptables: - sudo systemctl status iptables
复制代码如果显示 active (running),表示 iptables 正在运行。
1.2 查看防火墙规则
1.2.1 使用 firewalld
-
查看当前区域和规则: - sudo firewall-cmd --list-all
复制代码输出示例: - public (active)
- target: default
- icmp-block-inversion: no
- interfaces: eth0
- sources:
- services: ssh dhcpv6-client
- ports:
- protocols:
- masquerade: no
- forward-ports:
- source-ports:
- icmp-blocks:
- rich rules:
复制代码 -
查看开放的端口: - sudo firewall-cmd --list-ports
复制代码
1.2.2 使用 ufw
- 查看防火墙状态和规则:输出示例:
- Status: active
- To Action From
- -- ------ ----
- 22/tcp ALLOW Anywhere
- 80/tcp ALLOW Anywhere
复制代码
1.2.3 使用 iptables
- 查看所有规则:输出示例:
- Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
- 0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0
- Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
- Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
复制代码
3. 检查防火墙是否阻止了特定端口
3.1 使用 nmap
安装 nmap: - sudo yum install nmap # CentOS/RHEL
- sudo apt install nmap # Ubuntu/Debian
复制代码
扫描本地端口: - nmap -sT -p 22,80,3306 localhost
复制代码
输出示例: - Starting Nmap 7.80 ( https://nmap.org ) at 2023-10-01 12:00 UTC
- Nmap scan report for localhost (127.0.0.1)
- Host is up (0.00010s latency).
- PORT STATE SERVICE
- 22/tcp open ssh
- 80/tcp closed http
- 3306/tcp closed mysql
复制代码
4. 临时关闭防火墙
4.1 关闭 firewalld - sudo systemctl stop firewalld
复制代码
4.2 关闭 ufw
4.3 清空 iptables 规则
5. 永久关闭防火墙
5.1 禁用 firewalld - sudo systemctl disable firewalld
复制代码
5.2 禁用 ufw
5.3 清空并保存 iptables 规则 - sudo iptables -F
- sudo service iptables save
复制代码
6. 总结
- 使用 systemctl 检查防火墙服务状态。
- 使用 firewall-cmd、ufw 或 iptables 查看防火墙规则。
- 使用 nmap 检查端口是否开放。
- 如果需要,可以临时或永久关闭防火墙。
根据你的需求选择合适的方法查看或管理防火墙。如果问题仍然存在,请提供更多上下文信息(如操作系统版本、防火墙工具等),以便进一步分析。 |