如何在Ubuntu24系统中查看UFW防火墙的日志?
美国、香港服务器
如何在Ubuntu24系统中查看UFW防火墙的日志?
04-17 来源:
Ubuntu 24.04 查看 UFW 日志,核心分两步:先开启日志,再用 journalctl 或 日志文件 查看。
一、先启用 UFW 日志(必须)
bash
运行
# 开启日志(默认级别 low)
sudo ufw logging on
# 可选:设置日志级别
# low (默认): 仅记录被拒绝的流量
# medium: 记录拒绝+允许的流量
# high/full: 详细日志(生产慎用,量大)
sudo ufw logging medium
# 验证是否开启
sudo ufw status verbose
显示 Logging: on (medium) 即成功。
二、查看日志(两种方法)
方法 1:用 journalctl(推荐,Ubuntu 24 最小化镜像默认支持)
bash
运行
# 查看所有 UFW 日志
sudo journalctl -k | grep -i ufw
# 实时监控(推荐排查问题)
sudo journalctl -k -f | grep -i ufw
# 查看最近 50 条
sudo journalctl -k -n 50 | grep -i ufw
# 按时间查看(例如今天)
sudo journalctl -k --since today | grep -i ufw
方法 2:查看日志文件(需安装 rsyslog)
bash
运行
# 安装 rsyslog(生成独立 ufw.log)
sudo apt install -y rsyslog
sudo systemctl enable --now rsyslog
# 实时查看
sudo tail -f /var/log/ufw.log
# 分页查看
sudo less /var/log/ufw.log
# 过滤被拦截的记录
sudo grep "UFW BLOCK" /var/log/ufw.log
三、日志字段说明
plaintext
[UFW BLOCK] IN=eth0 OUT= MAC=xx:xx SRC=1.2.3.4 DST=5.6.7.8 PROTO=TCP SPT=12345 DPT=22
[UFW BLOCK]:动作(ALLOW 允许 / BLOCK 拒绝)
SRC:源 IP(攻击者 IP)
DST:目标 IP(你的服务器)
DPT:目标端口(被攻击端口)
PROTO:协议(TCP/UDP)
三二互联专业提供香港VPS,美国VPS主机,香港云服务器租用等业务香港美国到大陆CN2 GIA速度最快