Audit system events with auditd¶
The Linux kernel includes an audit subsystem that records security-relevant events, such as AppArmor denials, logins, and changes to user accounts. The auditd(8) daemon collects these events and processes them, and the auditd package provides tools to define what is recorded and to search the results.
This guide shows how to install auditd, where audit events are logged, and how to monitor some common account and security events.
Install auditd¶
Install the auditd package:
$ The auditd service starts automatically.
Warning
Expect a large increase in logged events as soon as you install auditd, even though it is configured with zero rules to begin with. When it starts, auditd enables kernel auditing, and from then on the system records audit events. For example, every login, sudo command, and cron job (through PAM), and every systemd service start and stop.
Many of these events are hardwired: they’re generated regardless of the audit rules you load, so an empty rule set doesn’t prevent them. To stop recording specific types of events, see Exclude events from the logs.
Where audit events are logged¶
Audit events are generated once auditing is enabled in the kernel. Where they end up after that varies. If there is nothing specifically consuming these events, they get sent to the kernel log where they are subject to rate limiting, which means some events can get lost.
Examples of audit event consumers are systemd-journald, via the systemd-journald-audit.socket unit, and the auditd daemon. If either, or both, are running, they will receive a copy of the audit events and process them.
Kernel audit status |
Consumers |
Audit messages destination |
|---|---|---|
disabled |
None |
Kernel log: just normal kernel events, including AppArmor messages |
enabled |
None |
Kernel log: |
enabled |
|
Kernel log and systemd journal: |
enabled |
|
Kernel log: None; |
Note
The kernel log stops receiving audit events when auditd is running because auditd registers itself as the primary (unicast) consumer, whereas systemd-journald registers as a multicast consumer.
The kernel limits how many audit messages it writes to the kernel log, and drops the rest. When this happens, the kernel log shows a message such as kauditd_printk_skb: 85 callbacks suppressed. Don’t rely on the kernel log for a complete record of audit events.
If you enable the systemd-journald-audit.socket unit, systemd-journald also enables kernel auditing when it (re)starts, even if auditd isn’t installed. This produces the same increase in logged events as installing auditd.
Basic concepts¶
The following concepts help you understand when events are recorded, and how to read the output of ausearch and the audit log.
Audit states¶
Kernel auditing is in one of three states, which sudo auditctl -s shows in the enabled field:
enabled 0Disabled. The kernel still generates some of its own events, such as AppArmor events, and doesn’t apply any audit rules that might be loaded. This is the kernel default, and it can be overridden with
audit=1in the kernel command line. A userspace program likeauditdcan enable it later during boot, though.enabled 1Enabled. Audit events are generated normally, and subject to the loaded rules (note that rules can also be used to exclude events).
auditdenables auditing when it starts. You can switch between0and1at any time withsudo auditctl -e 0andsudo auditctl -e 1, and loaded rules stay in place while auditing is disabled.enabled 2Enabled and immutable. Auditing can’t be disabled, and the audit rules can’t be changed, until the system reboots. Attempts to change the configuration are denied and recorded. To use this state, see Lock the audit configuration.
Events and records¶
A single audit event is often logged as several lines, called records. Each record has a type, and an audit event ID in msg=audit(<timestamp>:<id>) that identifies the event that the record belongs to. The event ID starts from 1 again after each reboot, so only the combination of timestamp and event ID is unique. All records with the same timestamp and event ID belong to the same event.
For example, these records in /var/log/audit/audit.log, shortened here, all belong to the event ID 558, which is an AppArmor denial:
type=AVC msg=audit(1796059637.028:558): apparmor="DENIED" operation="mknod" class="file" profile="/usr/sbin/chronyd" name="/auditdemo-denied" pid=7601 comm="touch" ...
type=SYSCALL msg=audit(1796059637.028:558): arch=c000003e syscall=257 success=no exit=-13 ... auid=1000 uid=0 ... ses=1 comm="touch" exe="/usr/lib/cargo/bin/coreutils/touch" ...
type=CWD msg=audit(1796059637.028:558): cwd="/home/ubuntu"
type=PATH msg=audit(1796059637.028:558): item=0 name="/" inode=2 dev=08:01 mode=040755 ... nametype=PARENT ...
type=PROCTITLE msg=audit(1796059637.028:558): proctitle=746F756368002F617564697464656D6F2D64656E696564
The AVC record describes the AppArmor decision, and the other records describe what triggered it: the system call (SYSCALL), the working directory (CWD), the file path involved (PATH), and the full command line, encoded in hexadecimal (PROCTITLE).
ausearch shows all the records of each matching event together, and separates events with a ---- line. To show a single event, pass its event ID to the -a option:
$ Login user ID¶
Each process has a login user ID, recorded in the auid field. It’s set when a user logs in, for example through SSH or on the console, by the pam_loginuid(8) module, and all processes started from that login inherit it. Unlike the uid field, the login user ID doesn’t change when the user switches to another user with sudo or su. This lets you trace an action back to the user who logged in.
In the previous example, the user with ID 1000 ran touch with sudo: uid=0 shows that the command ran as root, and auid=1000 shows who ran it. The ses field identifies the login session, so you can group the events from the same login.
Processes that don’t come from a login, such as services started by systemd, have no login user ID. Their auid is 4294967295, which ausearch -i shows as unset.
Rules¶
The next section will introduce rules, which control what events are recorded. The rules are loaded into the kernel, and the kernel applies them to each event it generates. The rules can also be used to exclude events from being recorded, as described in Exclude events from the logs.
Of particular importance is rule ordering: first match wins. Therefore the rules files should be prefixed by a number, so that the files are loaded in a predictable order. The upstream audit documentation suggests this grouping:
10 - Kernel and
auditctlconfiguration20 - Rules that could match general rules - but we want a different match (override)
30 - Main rules
40 - Optional rules
50 - Server Specific rules
70 - System local rules
90 - Finalize (immutable)
For our examples, we will use the 50- prefix for rules that are specific to our server, 20- for the exclusions, and the 99- prefix for the rule that locks the configuration.
Examples¶
The following examples show how to record and search for some common account and security events.
Monitor user and group changes¶
The user management tools, such as useradd(8), usermod(8) and passwd(1), report their changes to the audit system directly, so you don’t need any rules to record them. To search for these events, pass the event type to ausearch with the -m option. The -i option translates numeric values, such as user IDs, into names.
To find users being added:
$ ----
type=ADD_USER msg=audit(11/30/26 17:27:16.844:511) : pid=7567 uid=root auid=ubuntu ses=1 subj=unconfined msg='op=add-user acct=auditdemo exe=/usr/sbin/useradd hostname=? addr=? terminal=? res=success'
Here, uid=root and auid=ubuntu show that the user ubuntu ran useradd with sudo, as described in Login user ID.
To find changes to group membership, such as usermod -aG or gpasswd -a, search for USER_MGMT events, whose op field shows the change made (for example, op=add-user-to-group):
$ ----
type=USER_MGMT msg=audit(11/30/26 17:27:16.931:535) : pid=7580 uid=root auid=ubuntu ses=1 subj=unconfined msg='op=add-user-to-group grp=auditgrp acct=auditdemo exe=/usr/sbin/usermod hostname=? addr=? terminal=? res=success'
----
type=USER_MGMT msg=audit(11/30/26 17:27:16.969:547) : pid=7587 uid=root auid=ubuntu ses=1 subj=unconfined msg='op=delete-user-from-group grp=auditgrp acct=auditdemo exe=/usr/bin/gpasswd hostname=? addr=? terminal=? res=success'
To find local password changes:
$ You can limit any search to a time period with the -ts option, for example -ts today, -ts boot or -ts recent (the last ten minutes).
Watch the account files¶
The events in the previous section are only generated when the account files are changed through the user management tools. To also record changes made by other means, such as a text editor, add watch rules for the account files.
Create the file /etc/audit/rules.d/50-identity.rules with the following content:
-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
Each line watches a file for writes (w) and attribute changes (a), and tags any matching events with the key identity. See audit.rules(7) for the full rule syntax.
Load the rules with augenrules(8), and then list the active rules with auditctl(8) to confirm they’re loaded:
$ The command prints the status of the audit system, and might also print Old style watch rules are slower. This warning refers to the -w rule syntax, and doesn’t prevent the rules from loading.
$ -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
To search for events matching these rules, use the -k option with the key:
$ Track changes to sudoers¶
The /etc/sudoers file and the files in /etc/sudoers.d control who can run commands as root with sudo. A user who can already become root can change these files directly, instead of using the visudo command (visudo-rs(8)). To find out who changed them and how, add watch rules for them.
Create the file /etc/audit/rules.d/50-sudoers.rules with the following content:
-w /etc/sudoers -p wa -k sudoers
-w /etc/sudoers.d/ -p wa -k sudoers
The second rule watches the directory, so it records changes to any file in it. Load the rules:
$ For example, suppose that the user alice logs in through SSH, runs sudo -i to become root, and then appends a line to /etc/sudoers. The following steps show how to trace this.
First, find out whether someone changed /etc/sudoers, and who it was. The --format text option prints a one-line summary of each event:
$ At 18:04:57 11/30/26 alice, acting as root, successfully opened-file /etc/sudoers using bash
This shows that alice opened /etc/sudoers for writing while acting as root: the summary shows the login user ID first, followed by the user ID that the command ran as. The audit system records that the file was written, not what changed in it.
To see the details of the change, search for the same key without --format text. In the SYSCALL record of the event, shortened here, the ses field shows the login session that the change was made in:
$ ...
type=SYSCALL msg=audit(11/30/26 18:04:57.462:460) : arch=x86_64 syscall=openat success=yes exit=3 ... auid=alice uid=root ... tty=pts5 ses=5 comm=bash exe=/usr/bin/bash subj=unconfined key=sudoers
Next, find out how alice got there, by listing all events from that login session:
$ At 18:04:51 11/30/26 alice, acting as root, successfully triggered-unknown-audit-rule using /usr/lib/openssh/sshd-session
At 18:04:51 11/30/26 alice, acting as root, successfully started-session ssh using /usr/lib/openssh/sshd-session
At 18:04:51 11/30/26 alice, acting as root, successfully acquired-credentials alice using /usr/lib/openssh/sshd-session
At 18:04:51 11/30/26 alice successfully logged-in /dev/pts/4 using /usr/lib/openssh/sshd-session
At 18:04:55 11/30/26 alice successfully authenticated alice using /usr/lib/cargo/bin/sudo
At 18:04:55 11/30/26 alice successfully was-authorized alice using /usr/lib/cargo/bin/sudo
At 18:04:55 11/30/26 alice successfully refreshed-credentials root using /usr/lib/cargo/bin/sudo
At 18:04:55 11/30/26 alice successfully started-session /dev/pts/4 using /usr/lib/cargo/bin/sudo
At 18:04:57 11/30/26 alice, acting as root, successfully opened-file /etc/sudoers using bash
At 18:04:58 11/30/26 alice successfully ended-session /dev/pts/4
At 18:04:59 11/30/26 alice, acting as root, successfully ended-session ssh
At 18:04:59 11/30/26 alice, acting as root, successfully disposed-credentials alice using /usr/lib/openssh/sshd-session
The output shows the whole sequence: alice logged in through SSH, sudo authenticated her and started a session as root, she changed /etc/sudoers, and then she logged out.
Session IDs start from 1 again after each reboot. If the audit logs cover more than one boot, limit the search to the time of the change with the -ts and -te options.
Finally, to find where alice logged in from, search the session for the USER_LOGIN event, whose addr field shows the address of the SSH client:
$ ----
type=USER_LOGIN msg=audit(11/30/26 18:04:51.732:455) : pid=3520 uid=root auid=alice ses=5 subj=unconfined msg='op=login id=alice exe=/usr/lib/openssh/sshd-session hostname=10.0.10.200 addr=10.0.10.200 terminal=/dev/pts/4 res=success'
Monitor AppArmor events¶
AppArmor reports its decisions to the audit system as AVC events, so you don’t need any rules to record them. To find AppArmor events:
$ To show only denials, filter the output for apparmor=DENIED:
$ type=AVC msg=audit(11/30/26 17:27:17.028:558) : apparmor=DENIED operation=mknod class="file" profile=/usr/sbin/chronyd name=/auditdemo-denied pid=7601 comm=touch requested_mask=c denied_mask=c fsuid=root ouid=root
For more information about AppArmor, see AppArmor.
Exclude events from the logs¶
Some types of events are recorded in large numbers, and you might not need them. To stop recording them, add rules to the exclude filter. This also works for the hardwired events that are recorded even without any rules. The kernel discards excluded events before it sends them anywhere, so they don’t appear in the audit log, the journal, or the kernel log.
Caution
Only exclude events that you’re sure you don’t need. Excluded events are never recorded, so they can’t be recovered later. Some security standards require you to keep certain events, such as logins.
For example, on a server with many cron jobs, cron can fill the logs. Every time cron runs a job, it opens a session for it through PAM, and each job run produces several events. To list the events from cron:
$ At 18:32:01 11/30/26 system, acting as root, successfully was-authorized root using /usr/sbin/cron
At 18:32:01 11/30/26 system, acting as root, successfully acquired-credentials root using /usr/sbin/cron
At 18:32:01 11/30/26 root successfully triggered-unknown-audit-rule using /usr/sbin/cron
At 18:32:01 11/30/26 root successfully started-session cron using /usr/sbin/cron
At 18:32:01 11/30/26 root successfully refreshed-credentials root using /usr/sbin/cron
At 18:32:01 11/30/26 root successfully disposed-credentials root using /usr/sbin/cron
At 18:32:01 11/30/26 root successfully ended-session cron
...
These seven events come from a single run of one job. To find out which programs produce the most events on your system, run sudo aureport -x --summary -i.
To exclude all events from cron, create the file /etc/audit/rules.d/20-exclude-cron.rules with the following content:
-a always,exclude -F exe=/usr/sbin/cron
The exe field matches the program that generates the event, so this rule excludes all the events that are directly emitted by cron, whatever their type. The commands that the jobs run aren’t affected: events from them are still recorded.
Load the rules:
$ To exclude only some types of events, add the msgtype field. A rule only matches when all of its -F fields match, so use a separate rule for each type. For example, the following rules exclude only the account and credential events from cron, and keep the session events:
-a always,exclude -F msgtype=USER_ACCT -F exe=/usr/sbin/cron
-a always,exclude -F msgtype=CRED_ACQ -F exe=/usr/sbin/cron
-a always,exclude -F msgtype=CRED_REFR -F exe=/usr/sbin/cron
-a always,exclude -F msgtype=CRED_DISP -F exe=/usr/sbin/cron
See auditctl(8) for the other fields that you can use in exclude rules.
Useful ausearch and aureport commands¶
Use ausearch to find individual events, and aureport to summarize them. Both read the current and rotated audit logs. Add the -i option to show names instead of numeric values, such as user names instead of user IDs.
The following ausearch options select which events to show:
Option |
Shows events |
|---|---|
|
From midnight onwards. Other keywords include |
|
Between two times. The date format depends on your locale: run |
|
Of the given types |
|
That match the audit rules with the given key |
|
That involve the given file |
|
From the given program |
|
With the given login user ID, which doesn’t change with |
|
Where the login user ID, the user ID, or the effective user ID matches |
|
From the given login session |
|
With the given event ID |
|
That failed, such as denied file access or failed authentication |
User names only work for users that still exist. To search for a user that has been deleted, use their user ID, for example -ua 1001.
Options can be combined, and an event must match all of them. For example, to find the failed authentication attempts of today:
$ To show a one-line summary of each event instead of its records, add --format text. To export events for use in other tools, use --format csv.
The following aureport commands show reports. They also accept the -ts and -te options to limit the report to a time range:
Command |
Shows |
|---|---|
|
A summary of the audit logs, such as the number of logins, failed authentications, and account changes |
|
Logins |
|
Failed authentication attempts |
|
Changes to user accounts, groups, and passwords |
|
File access events, such as those recorded by watch rules |
|
AppArmor events |
|
The number of events of each type |
|
The number of events from each program |
|
The number of events for each rule key |
|
The number of events for each login user ID |
To create a report from the results of a search, pass the raw output of ausearch to aureport. For example, to list the file access events recorded by the sudoers rules:
$ See ausearch(8) and aureport(8) for all the options.
Configure audit log rotation¶
Unlike most log files on Ubuntu, the audit logs aren’t rotated by logrotate(8), and there’s no logrotate configuration for them in /etc/logrotate.d. Instead, auditd rotates its own logs, following the settings in /etc/audit/auditd.conf. To change how the audit logs are rotated, edit these settings rather than adding a logrotate configuration.
By default, auditd rotates the log based on its size, with the following settings:
max_log_file = 8
max_log_file_action = ROTATE
num_logs = 5
When /var/log/audit/audit.log reaches max_log_file MiB, auditd renames it to audit.log.1, renames any older logs to audit.log.2, audit.log.3, and so on, and starts a new log. It keeps num_logs files, including the current one, and deletes the oldest, so with a setting of 5, the oldest file will be audit.log.4. The rotated logs aren’t compressed, and ausearch searches them as well as the current log.
If you make changes to /etc/audit/auditd.conf, tell auditd to reload its configuration:
$ To rotate the log immediately, regardless of its size:
$ Note
In Ubuntu 22.04 LTS, auditctl doesn’t accept the reload and rotate keywords. Use the signal names instead: sudo auditctl --signal HUP to reload the configuration, and sudo auditctl --signal USR1 to rotate the log.
Lock the audit configuration¶
To prevent anyone, including root, from disabling auditing or changing the audit rules while the system is running, lock the audit configuration. This sets the immutable state described in Audit states, and any further changes require a reboot.
Warning
Load and test your rules before you lock the configuration. Once it’s locked, you can only fix an incorrect rule by rebooting after applying the fix.
Create the file /etc/audit/rules.d/99-finalize.rules with the following content:
-e 2
augenrules always places the -e option at the end of the rules it generates, so the lock applies after all other rules are loaded. Load the rules:
$ Confirm that the configuration is locked:
$ enabled 2
From now on, attempts to change the rules fail with errors such as Error deleting rule (Operation not permitted). The lock is applied again at every boot, when the rules are loaded. You can still stop the auditd service, but kernel auditing stays enabled, and events go to the kernel log until auditd starts again.
To remove the lock, delete /etc/audit/rules.d/99-finalize.rules, and then reboot the system.
Uninstall auditd¶
Removing auditd doesn’t disable kernel auditing or unload any rules that are already loaded. Because auditctl is removed together with auditd, delete the rules and disable kernel auditing before you remove the package.
Delete all loaded rules:
$ Disable kernel auditing:
$ If the audit configuration is locked, as described in Lock the audit configuration, these commands fail, and the rules and auditing stay active until you reboot the system.
To remove auditd but keep its configuration and logs:
$ To also remove the configuration in /etc/audit, except the extra rules files you added, run the following command instead.
Warning
Purging the package also deletes /var/log/audit and all the audit logs it contains.
$ Further reading¶
Ubuntu Security Guide, which can apply audit rules from compliance profiles such as the CIS Benchmarks
Manual pages: auditd(8), auditd.conf(5), auditctl(8), audit.rules(7), augenrules(8), ausearch(8), aureport(8)