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