Linux查看防火墙
本文介绍了Linux中防火墙的相关知识。
·
在 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
- 查看防火墙状态和规则:
输出示例:sudo ufw status verboseStatus: active To Action From -- ------ ---- 22/tcp ALLOW Anywhere 80/tcp ALLOW Anywhere
1.2.3 使用 iptables
- 查看所有规则:
输出示例:sudo iptables -L -n -vChain 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
sudo ufw disable
4.3 清空 iptables 规则
sudo iptables -F
5. 永久关闭防火墙
5.1 禁用 firewalld
sudo systemctl disable firewalld
5.2 禁用 ufw
sudo ufw disable
5.3 清空并保存 iptables 规则
sudo iptables -F
sudo service iptables save
6. 总结
- 使用
systemctl检查防火墙服务状态。 - 使用
firewall-cmd、ufw或iptables查看防火墙规则。 - 使用
nmap检查端口是否开放。 - 如果需要,可以临时或永久关闭防火墙。
根据你的需求选择合适的方法查看或管理防火墙。如果问题仍然存在,请提供更多上下文信息(如操作系统版本、防火墙工具等),以便进一步分析。
更多推荐
所有评论(0)