Here’s a list of tools to monitor or watch file openings on Linux, along with their use cases and examples:
1. inotify Tools (inotifywait, inotifywatch)
Description: inotify is a Linux kernel subsystem that monitors file system events. Tools like inotifywait (waits for events) and inotifywatch (gathers statistics) are built on it.
Example:
bash
inotifywait -m /path/to/watch -e open
This watches for open events in real time.
2. lsof (List Open Files)
Description:
Lists files opened by processes. Useful for snapshots, not real-time monitoring.
Example:
bash
lsof /path/to/file
To monitor changes periodically:
bash
watch -n 1 'lsof /path/to/watch'
3. auditd (Linux Audit Daemon)
Description:
Tracks system calls, including file access. Requires configuration.
Example:
Add a rule to monitor a directory:
bash
auditctl -w /path/to/watch -p war -k file_open
View logs:
bash
ausearch -k file_open
4. strace
Description:
Traces system calls (e.g., open(), openat()) for a specific process.
Example:
Attach to a process by PID:
bash
strace -p <PID> -f -e trace=open
5. opensnoop (BCC Tool)
Description:
Uses eBPF to trace open() system calls in real time.
Example:
bash
opensnoop
To monitor a specific file:
bash
opensnoop -f /path/to/file
6. procmon (Sysinternals)
Description:
A Linux port of the Windows tool for monitoring system activity, including file opens.
Example:
bash
procmon -l --file-name /path/to/file
7. fanotify
Description:
A kernel interface for monitoring file access at a higher level than inotify (e.g., permission control).
Usage:
Typically requires custom C/Python scripts using the fanotify API.
8. Log Files (via auditd or syslog)
Description:
Logs from auditd (/var/log/audit/audit.log) or other system logs (/var/log/syslog) can be analyzed for file access events.
Summary Table
Tool
Real-Time Monitoring
Requires Root
Notes
inotify
Yes
No
Lightweight, file-level events.
lsof
No
No
Shows current open files.
auditd
Yes
Yes
System-wide, persistent rules.
strace
Yes
Yes
Process-specific tracing.
opensnoop
Yes
Yes
Efficient eBPF-based tracing.
procmon
Yes
Yes
High-level system monitoring.
fanotify
Yes
Yes
Custom scripting required.
Notes:
Install tools like inotify-tools, auditd, or bcc via your package manager (e.g., sudo apt install inotify-tools auditd bcc-tools).
Use sudo for tools requiring root access (auditd, opensnoop, etc.).