Ubuntu文件系统监控常用工具
inotify-tools是基于Linux内核inotify机制的经典命令行工具集,专注于实时监控文件或目录的变化(如创建、修改、删除、移动等事件)。它包含inotifywait
(持续监控并输出事件)和inotifywatch
(统计事件发生次数)两个核心命令,适合脚本自动化处理(如文件更新后触发同步、重启服务等场景)。
sudo apt-get install inotify-tools
/root
目录下的创建、修改事件:inotifywait -m -r -e create,modify /root
/root
目录的事件次数:inotifywatch -r -e create,modify /root
/proc/sys/fs/inotify/max_user_watches
限制(可通过修改该文件调整);不支持递归监控子目录(需手动遍历添加);对NFS等网络文件系统支持有限。fswatch是跨平台的文件系统监控工具(支持Linux、macOS、Windows等),可实时监控文件或目录的变化,支持自定义事件类型(如修改、创建、删除)和过滤条件(如文件扩展名)。适合需要跨平台兼容性的用户。
sudo apt-get install fswatch
fswatch /path/to/file
fswatch -r /path/to/directory
.txt
文件的修改事件:fswatch -e ".txt" /path/to/directory
Watchdog是用Python编写的事件驱动型文件系统监控库,支持实时监听文件/目录的变化(如创建、修改、删除、移动),并提供Python API接口,适合开发者集成到自定义脚本或应用中(如自动化测试、文件同步工具)。
pip install watchdog
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import time
class MyHandler(FileSystemEventHandler):
def on_modified(self, event):
if not event.is_directory:
print(f"文件被修改: {event.src_path}")
observer = Observer()
observer.schedule(MyHandler(), path="/path/to/directory", recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Directory Monitor是一款图形化文件系统监控工具,界面简洁易用,支持监控特定目录下的文件/子目录变化,可配置事件类型(如创建、修改、删除)、过滤条件(如文件大小、类型)和通知方式(如弹窗、邮件)。适合不熟悉命令行的用户。
Wazuh是企业级安全监控解决方案,内置**文件完整性监控(FIM)**模块,可监控文件系统的创建、修改、删除事件,同时支持检测恶意修改(如篡改配置文件、植入木马)。适合需要安全审计的企业环境。
sudo apt-get install wazuh-agent
/var/ossec/etc/ossec.conf
):<directories check_all="yes" report_changes="yes" realtime="yes">/root</directories>
sudo systemctl restart wazuh-agent