Someone edited /etc/sudoers on a production server last Tuesday. The syslog shows a successful login and nothing else. Who changed the file, from which session, with which command, and did they touch anything after that? Ordinary logs rarely answer these questions, because they record what services chose to say, not what processes actually did. The Linux Audit framework fills that gap: it records events at the kernel boundary, so a process cannot skip logging by simply not writing a line.

This article shows how to use auditd for defensive detection: what to watch, how to write rules that do not drown you in noise, how to read the output, and where the limits are. It complements Linux Log Analysis for Security Monitoring, which focuses on the logs services already produce.

How the Audit Framework Works

Auditing has two halves:

  • The kernel audit subsystem evaluates rules on system calls, file accesses and some security events, and emits records.
  • The auditd daemon receives those records and writes them to /var/log/audit/audit.log. Tools such as auditctl, ausearch and aureport manage rules and query the log.

Rules come in three kinds:

  1. File watches (-w): log reads, writes, attribute changes or executions on a path.
  2. System call rules (-a): log specific syscalls, optionally filtered by user, architecture or success.
  3. Control rules: buffer size, failure mode, immutability.

Install and enable it:

# Debian / Ubuntu
sudo apt install auditd audispd-plugins

# RHEL family
sudo dnf install audit

sudo systemctl enable --now auditd
sudo auditctl -s     # status: enabled, backlog, lost

Rule files live in /etc/audit/rules.d/*.rules. They are merged by augenrules --load into /etc/audit/audit.rules. Edit the files in rules.d, never the generated one.

Start With a Plan, Not a Rule Dump

The most common failure mode of auditd is copying a huge community rule set, enabling it, and finding a day later that the log grew by gigabytes and nobody can find anything. Rules should map to questions you want answered:

  • Did someone change identity or authentication files?
  • Did someone gain privileges?
  • Did someone alter logging, audit rules or time?
  • Did a service account run an interactive shell or compiler?
  • Did files in sensitive directories get written outside of deployments?
  • Did someone load a kernel module?

Start with a handful of high-value rules, watch the volume for a few days, then extend.

A Baseline Rule Set

Create /etc/audit/rules.d/10-baseline.rules:

## Buffer and failure behaviour
-D
-b 8192
-f 1

## Identity and authentication files
-w /etc/passwd   -p wa -k identity
-w /etc/shadow   -p wa -k identity
-w /etc/group    -p wa -k identity
-w /etc/gshadow  -p wa -k identity

## Privilege configuration
-w /etc/sudoers   -p wa -k privilege
-w /etc/sudoers.d -p wa -k privilege

## SSH configuration and keys
-w /etc/ssh/sshd_config   -p wa -k sshd
-w /etc/ssh/sshd_config.d -p wa -k sshd

## Scheduled tasks are a classic persistence spot
-w /etc/crontab      -p wa -k cron
-w /etc/cron.d       -p wa -k cron
-w /var/spool/cron   -p wa -k cron

## Systemd units added or changed
-w /etc/systemd/system -p wa -k systemd

## Tampering with audit itself
-w /etc/audit/        -p wa -k audit_config
-w /var/log/audit/    -p wa -k audit_log

## Kernel modules
-a always,exit -F arch=b64 -S init_module,finit_module,delete_module -k modules

## Changes to system time
-a always,exit -F arch=b64 -S adjtimex,settimeofday,clock_settime -k time_change

The -p wa option means “write and attribute change”, which avoids logging every read. The -k key is a label you will search by later. Keys are what make an audit log usable.

-f 1 tells the kernel to print a message on failure rather than panic. Setting -f 2 (panic) is appropriate only on systems where losing audit records is worse than downtime, such as some regulated environments.

Load and verify:

sudo augenrules --load
sudo auditctl -l            # list active rules
sudo auditctl -s            # check "lost" stays at 0

Watching Execution and Privilege Changes

File watches tell you what was modified. To learn what was run, use syscall rules on execve. Logging every command on a busy server is too noisy, so scope it:

## Commands run by a user whose uid is not a regular login user (service accounts)
-a always,exit -F arch=b64 -S execve -F uid>=1 -F uid<1000 -F auid=unset -k service_exec

## Commands run with root effective privileges by a logged-in human
-a always,exit -F arch=b64 -S execve -F euid=0 -F auid>=1000 -F auid!=unset -k root_exec

## Use of privilege-related syscalls
-a always,exit -F arch=b64 -S setuid,setgid,setreuid,setresuid -F auid>=1000 -F auid!=unset -k priv_change

The auid field is the key to attribution. It is the audit user id, set at login and inherited by every child process, even after sudo or su changes the effective user. When an attacker logs in as deploy and runs sudo bash, the commands executed as root still show auid of deploy. That is how you answer “who really did this”.

Some adjustments to keep in mind:

  • On 64-bit systems that also run 32-bit binaries, duplicate syscall rules with arch=b32, because an attacker can use the 32-bit entry points to avoid a 64-bit-only rule.
  • If a rule is too noisy, exclude known benign executables with -a never,exit placed before the matching rule, or tighten the filters rather than deleting the rule.
  • Container hosts generate large volumes of execve events. Scope rules by auid or by directory to keep them useful.

Watching Sensitive Directories

Applications that should not change after deployment make good targets: their code directories and configuration.

-w /srv/app/releases -p wa -k app_code
-w /etc/nginx        -p wa -k web_config
-w /root/.ssh        -p wa -k ssh_keys
-w /home/deploy/.ssh -p wa -k home_ssh

Avoid watching all of /home, which is very noisy. Narrow it to specific files such as authorized_keys, or to dot-directories used for persistence. A watch on a directory covers files created in it; a watch on a single file is attached to that file, which matters when an editor replaces the file by renaming a new one. Watching the parent directory handles that case.

Reading the Log

Raw audit records are dense. A single event is split over several lines that share a timestamp and serial, such as SYSCALL, PATH, CWD and PROCTITLE. Use the search tools instead of grep:

# All events with a key, today, interpreted (numeric ids resolved to names)
sudo ausearch -k identity --start today -i

# Who touched sudoers in the last week
sudo ausearch -k privilege --start this-week -i

# Failed logins and authentication events
sudo ausearch -m USER_LOGIN,USER_AUTH --success no -i

# Summary reports
sudo aureport --auth --summary
sudo aureport -x --summary       # which executables ran most
sudo aureport --failed

A typical interpreted event for a changed sudoers file shows the process (exe=/usr/bin/vim.basic), the auid of the human, the working directory and the full path. Combined, that gives you the session, the program and the file. If you also need the command line, PROCTITLE events hold it in hex; ausearch -i decodes it.

Pay attention to the ses field. All events from one login share a session id, so one ausearch --session 42 -i reconstructs what that login did.

Shipping Events Off the Host

An attacker with root can edit or delete local audit logs. The audit trail is only trustworthy if a copy leaves the machine in near real time. Options include:

  • audisp plugins (audisp-remote) to forward to a central audit server.
  • A log shipper reading /var/log/audit/audit.log and forwarding to your central logging system.
  • The syslog plugin to feed the records into rsyslog.

Whichever you choose, parse the records into fields on the receiving side, using key, auid, exe and name as search dimensions, and write alerts on the keys: any identity event outside a change window, any audit_config event, any modules event, any root_exec event for a binary like nc, nmap or a compiler on a server that should never run them.

A simple alert idea expressed as a query description: “a service_exec event whose executable is /bin/sh or /bin/bash for the web server’s user”. Web server accounts do not normally spawn shells, so this is a strong signal for a web-application compromise. For deeper investigation workflows once an alert fires, see Incident Response Playbook.

Protecting the Configuration

Once the rule set is stable, lock it so that a compromised root session cannot silently turn auditing off. Add this as the last file loaded, for example 99-finalize.rules:

-e 2

With -e 2 the configuration becomes immutable until reboot: rules cannot be changed or removed, and auditd cannot be reconfigured without restarting the machine. The trade-off is operational: any rule tweak needs a reboot, so test thoroughly first and only enable it where change control is mature. A rebooted-then-quiet audit system is itself a signal worth alerting on.

Also manage disk behaviour in /etc/audit/auditd.conf:

max_log_file = 50
num_logs = 10
max_log_file_action = ROTATE
space_left_action = EMAIL
admin_space_left_action = HALT
disk_full_action = SUSPEND

Choose actions that match your risk tolerance. HALT on low space protects the integrity of the trail but can take a server down; SUSPEND keeps the server alive but loses events. Whatever you pick, monitor free space on the audit partition.

Limits and Trade-offs

Auditing is powerful, but it is not magic:

  • Performance cost. Broad syscall rules, especially on open, read or execve on busy systems, add measurable overhead and can fill the backlog. Check auditctl -s for lost and backlog counters and tune -b.
  • It records, it does not prevent. Auditing tells you what happened. Preventive controls such as mandatory access control, read-only filesystems and least privilege are separate layers; see Securing a Linux Server for the baseline.
  • Kernel-level evasion exists. An attacker who gains kernel-level control can tamper with what is recorded. Remote shipping and integrity monitoring reduce, not eliminate, that risk.
  • Containers. Audit records from containers are attributed using host ids and namespaces; mapping them back to a container name requires extra enrichment.
  • Not a substitute for analysis. An unreviewed audit log is a very expensive way to produce disk usage.

Alternatives and complements include eBPF-based runtime sensors and file integrity tools. Auditd’s advantages are that it is built into the kernel, widely supported by compliance frameworks, and requires no extra agent.

Common Mistakes

  • Enabling a giant rule set without baselining volume.
  • Forgetting arch=b32 counterparts for syscall rules.
  • Editing /etc/audit/audit.rules directly instead of rules.d.
  • Watching a single file that gets replaced by rename instead of watching its directory.
  • Never testing alerts: trigger a harmless change to /etc/sudoers.d in a test VM and confirm an alert fires.
  • Keeping audit logs only on the machine that may be compromised.
  • Writing rules without keys, so events cannot be filtered later.

Key Takeaways

  • The audit framework records what processes did at the kernel boundary, so it answers who, what and from which session in a way ordinary logs cannot.
  • Start from questions you want answered, use small keyed rule sets, and baseline log volume before extending.
  • Use the auid and ses fields to attribute actions through sudo and su back to a real login.
  • Search with ausearch -i -k <key> and aureport rather than raw grep.
  • Ship events off the host in near real time and alert on keys such as identity changes, audit configuration changes and shells spawned by service accounts.
  • Consider -e 2 to make rules immutable once they are stable, accepting that changes then require a reboot.