利用 inotify 提升 Debian 安全的实操指南
一 核心思路与适用场景
二 快速落地步骤
sudo apt update && sudo apt install inotify-tools#!/usr/bin/env bash
set -euo pipefail
WATCH_DIR="/etc"
LOG_FILE="/var/log/inotify.log"
ALERT_CMD="logger -p authpriv.alert 'inotify ALERT: %w%f modified'"
mkdir -p "$(dirname "$LOG_FILE")"
inotifywait -m -r -e modify,attrib --format '%w%f %e' "$WATCH_DIR" | while IFS= read -r line; do
echo "$(date '+%F %T') $line" >> "$LOG_FILE"
eval "$ALERT_CMD"
done
[Unit]
Description=Inotify File System Monitor
After=network.target
[Service]
ExecStart=/usr/local/bin/inotify-monitor.sh
Restart=always
User=nobody
Group=nogroup
[Install]
WantedBy=multi-user.target
sudo systemctl enable --now inotify-monitor.service三 关键加固与性能调优
sudo sysctl -w fs.inotify.max_user_watches=524288fs.inotify.max_user_watches=524288
fs.inotify.max_user_instances=1024
fs.inotify.max_queued_events=16384
sudo sysctl -p四 联动响应与体系化防护