直接提取/var/log/secure中非授权sudo行为并实时告警,关键在于匹配固定日志短语“user is not in the sudoers file”,该特征稳定、无需外部校验;通过tail -f | grep -i精准捕获,再用awk结构化输出时间、用户、命令等字段,最后接入notify-send、日志记录或邮件实现轻量级告警。

直接提取 /var/log/secure 中非授权的 sudo 执行行为并实时告警,关键在于精准识别“用户未被配置在 sudoers 中却调用了 sudo”的日志特征——这类操作在日志中明确记录为 user is not in the sudoers file,且通常伴随拒绝提示 This incident will be reported。无需复杂权限校验或外部数据库比对,靠原生日志字段就能可靠捕获。
定位核心日志模式
非授权 sudo 行在 /var/log/secure 中有稳定格式,典型示例如下:
真正可依赖的告警触发点是 user is not in the sudoers file 这一固定短语,它不会因系统版本或 locale 变化而改变。其他字段(如用户名、TTY、命令)属于辅助信息,用于后续分析但不参与判断。
Zynq‑7000 / Zynq UltraScale+ 硬件调试技能。适用于 Xilinx Zynq(Cortex‑A9/A53 + FPGA)Linux 环境,调试 I2C、I2S、UART、GPIO 等外设。包括设备树配置、内核驱动、用户空间调试工具、交叉编译和常见问题排查,支持 PetaLinux 开发。
用 tail -f + grep 实现实时捕获
保持日志流持续监听,只筛选出目标行:
tail -f /var/log/secure | grep "user is not in the sudoers file"- 加
-i更稳妥:tail -f /var/log/secure | grep -i "user is not in the sudoers file"(避免大小写干扰) - 若需排除测试账号(如
testuser),可追加| grep -v "testuser"
用 awk 提取关键上下文并结构化输出
grep 捕获后,用 awk 提取时间、用户、终端和命令,便于告警内容清晰可读:
tail -f /var/log/secure | grep -i "user is not in the sudoers file" | awk '{print $1,$2,$3,"USER:"$9,"CMD:"$14,$15,$16,$17}'- 说明:$1–$3 是日期时间;$9 是实际执行 sudo 的用户名(位于
sudo:后第2字段);$14 起是COMMAND=后的完整命令路径 - 更健壮写法(适配不同日志格式):
awk -F': ' '/user is not in the sudoers file/ {split($2,a,";"); print $1, "USER:"substr(a[1],1,index(a[1]," ") -1), "CMD:"a[4]}'
接入轻量级告警(无需ELK)
将匹配结果转为系统通知或写入告警文件即可快速响应:
- 发桌面通知(需 GUI 环境):
... | while read line; do notify-send "SECURITY ALERT" "$line"; done - 写入专用告警日志:
... | tee -a /var/log/sudo_alerts.log - 邮件告警(配合 mail 命令):
... | while read line; do echo "$line" | mail -s "Unauthorized sudo attempt" admin@example.com; done - 简单阻断(可选):提取 IP 后用
iptables或fail2ban规则临时封禁(需额外解析 SSH 登录源 IP)










