Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 117Security · Audit

How to watch file changes with auditd

"Who changed /etc/sudoers last night?" — normal logs can not tell you. The Linux audit system (auditd) records file access and system calls with the real user behind them (even after sudo). This how-to adds watch rules for important files, makes them permanent, searches the results in readable form, and keeps the audit log useful and safe.

  • 5 Lessons
  • 8 Commands

$ run as a normal user# run as root (or with sudo)

117.1

The quick answer

/etc/audit/rules.d/50-watch.rules
-w /etc/sudoers -p wa -k sudoers
-w /etc/sudoers.d/ -p wa -k sudoers
-w /etc/passwd -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/ssh/sshd_config -p wa -k sshd_config
augenrules --load
auditctl -l
ausearch -k sudoers -i --start today

-w = watch this file, -p wa = on write and attribute change, -k = a key to search by.

117.2

Step by step

  1. auditd is installed and running on RHEL; on Ubuntu: apt-get install -y auditd. Check:
systemctl status auditd --no-pager
auditctl -s
  1. **Write rules in /etc/audit/rules.d/** (quick answer). Watch what matters: identity files, sudo, SSH config, cron, your app's config:
/etc/audit/rules.d/50-watch.rules (more)
-w /etc/crontab -p wa -k cron
-w /etc/cron.d/ -p wa -k cron
-w /opt/shop/config/ -p wa -k app_config
  1. Load them — augenrules builds /etc/audit/audit.rules from all files:
augenrules --load
auditctl -l

(Do not restart auditd with systemctl restart on RHEL; use augenrules --load or service auditd restart.)

  1. Search the events — -i turns numbers into names:
ausearch -k sudoers -i --start today
type=SYSCALL ... comm="vim" exe="/usr/bin/vim" ... auid=alice uid=root ... key="sudoers"

auid = the login user (alice), even though she used sudo (uid=root). That is the answer to "who".

  1. Reports:
aureport -f -i --start this-week | tail
aureport --login --summary -i
  1. Command logging for root (heavy — use with care on busy servers):
/etc/audit/rules.d/60-rootcmd.rules
-a always,exit -F arch=b64 -S execve -F euid=0 -F auid>=1000 -F auid!=unset -k rootcmd
  1. Protect the trail: ship audit.log to a central SIEM (Wazuh, Elastic) in near real time; optionally make rules immutable until reboot with a last rule -e 2; size limits in /etc/audit/auditd.conf (max_log_file, num_logs).

117.3

Check that it worked

Change a watched file (for example add a comment with visudo), then:

ausearch -k sudoers -i --start recent | grep -E 'auid=|exe='

The event shows your login user, the program and the time.

117.4

Common problems

ProblemCauseFix
auditctl -l shows No rulesRules not loaded.augenrules --load; check for errors in the rule files.
Rules gone after rebootAdded only with auditctl.Put them in /etc/audit/rules.d/*.rules.
The audit system is in immutable mode-e 2 is set.Change the rule files and reboot.
Disk fills with audit logsNoisy rules (execve for everything).Narrow rules; set max_log_file and num_logs.
auid=unset in eventsProcess started by a service, not a login.Normal for daemons; filter with auid>=1000.
Too many events to readNo keys / no filters.Always use -k and search with ausearch -k.

117.5

Go further

1In an audit event, what is the difference between auid and uid?

auid is the user who logged in (stays the same after sudo); uid is the user the process runs as.

2Where should permanent audit rules be?

In /etc/audit/rules.d/*.rules, loaded with augenrules --load.

All commands in this chapter