Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 37 ยท Security Hardening

Auditing and File Integrity: auditd, AIDE and Lynis

Detect and prove changes on Linux: write auditd rules, search audit logs with ausearch, check file integrity with AIDE and checksums, and score your hardening with Lynis and CIS benchmarks.

Prevention fails sometimes, so a well-run server also answers two questions after the fact: what changed? and who did it? Linux gives you tools for both. The Linux Audit system records security-relevant events in the kernel. File-integrity tools such as AIDE remember what the important files looked like and tell you when they differ. Auditors such as Lynis grade the overall configuration and list improvements.

None of these tools can run on this site. Commands are read-only, and the Python stand-ins reproduce the core ideas (a hash baseline and an audit record parser) so you can see how the tools think.

The Linux Audit framework

The kernel generates audit events for system calls and watched files, and the auditd daemon writes them to /var/log/audit/audit.log. It is installed by default on the RHEL family and available as auditd on Debian and Ubuntu.

Terminal
sudo apt install auditd audispd-plugins     # Debian / Ubuntu
sudo systemctl enable --now auditd
sudo auditctl -s                            # status
sudo auditctl -l                            # loaded rules

Writing rules

Rules in /etc/audit/rules.d/*.rules are compiled into /etc/audit/audit.rules by sudo augenrules --load. Two kinds matter most.

File watches (-w path -p permissions -k key): permissions are r read, w write, x execute, a attribute change. The -k key is a label you search for later.

TEXT
-w /etc/passwd  -p wa -k identity
-w /etc/shadow  -p wa -k identity
-w /etc/sudoers -p wa -k privilege
-w /etc/sudoers.d/ -p wa -k privilege
-w /etc/ssh/sshd_config -p wa -k sshd
-w /var/log/lastlog -p wa -k logins

System-call rules (-a action,list -S syscall -F field=value -k key):

TEXT
-a always,exit -F arch=b64 -S execve -F euid=0 -k root_commands
-a always,exit -F arch=b64 -S unlink,unlinkat,rename -F auid>=1000 -F auid!=unset -k deletes
-a always,exit -F arch=b64 -S mount -k mounts
-e 2

auid is the login uid: the user who originally logged in, which survives sudo and su. That is why audit can tell you that alice did something as root. The last line -e 2 locks the rule set until reboot, so an intruder cannot turn auditing off (use it only once your rules are final).

Searching the log

Raw audit records are dense. Use the helper tools:

Terminal
sudo ausearch -k identity -i                 # by key, interpreted (names, not numbers)
sudo ausearch -m USER_LOGIN --success no -ts today
sudo ausearch -x /usr/bin/passwd -ts yesterday
sudo aureport --auth --summary
sudo aureport -f -i --summary                # file access report
sudo aureport --failed

A watch event looks like type=SYSCALL msg=audit(1731405723.114:2891): arch=c000003e syscall=257 success=yes exit=3 ... auid=1001 uid=0 exe="/usr/bin/vim.basic" key="identity" followed by a PATH record naming /etc/passwd. Read it as: user 1001 (auid), acting as uid 0, used vim to open /etc/passwd with write access, matched key identity.

Python Parse audit records and group by user

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Audit logs grow quickly. Tune max_log_file, num_logs and max_log_file_action = ROTATE in /etc/audit/auditd.conf, forward logs to a central server (so a compromised host cannot erase its own evidence), and be selective: auditing every read of every file will drown you.

File integrity with AIDE

AIDE (Advanced Intrusion Detection Environment) builds a database of file attributes and hashes, then compares the current state with it.

Terminal
sudo apt install aide                         # Debian: also "aide-common"
sudo dnf install aide
sudo aideinit                                  # Debian family: builds the baseline
sudo aide --init                               # RHEL family
sudo mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz
sudo aide --check                              # compare now with the baseline
sudo aide --update                             # after verified, intended changes

Rules in /etc/aide/aide.conf (or /etc/aide.conf) choose which paths and attributes to track, for example /etc with permissions, owner, size and SHA-512, and /var/log with permissions only, because log content changes constantly. Run a check from cron or a systemd timer and mail the result.

The crucial rule: store the baseline and the binary somewhere an attacker cannot rewrite (read-only media, a remote system), otherwise they can simply update the baseline after tampering. Take the baseline right after installation, before the machine is exposed, and refresh it after every legitimate change such as package updates.

The same idea works without AIDE: package managers verify installed files with rpm -Va and debsums -c (install debsums), and plain checksums work for small sets: sha256sum /usr/bin/ssh /etc/ssh/sshd_config > baseline.txt and later sha256sum -c baseline.txt.

Python Integrity baseline and check

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Scoring your configuration: Lynis and CIS

Lynis is an open-source auditing script. It inspects the system, prints warnings and suggestions, and gives a hardening index.

Terminal
sudo apt install lynis            # or download from the project
sudo lynis audit system
sudo lynis show details TEST-ID
# report: /var/log/lynis-report.dat

Treat the score as a to-do list, not a goal: it will suggest things (for example a separate partition for /tmp) that may not suit your workload. Run it after major changes and compare reports.

The CIS Benchmarks are consensus checklists for each distribution, with two profiles (Level 1: sensible baseline; Level 2: stricter, may reduce usability). Tools such as OpenSCAP (oscap) and the SCAP Security Guide can scan a machine against them (sudo oscap xccdf eval --profile ... ssg-ubuntu2404-ds.xml) and even generate remediation scripts. Use them for compliance work, review each remediation before applying, and apply changes through configuration management, not by hand on each host.

Detect, prove, grade

auditd Who did what, when.
AIDE Which files changed.
Lynis, OpenSCAP How hardened are we?

Send logs off the host, store baselines safely.

1 / 2

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is auid more useful than uid in audit records?

    auid is set at login and inherited, so actions taken as root can still be traced to a person.
  2. 2. What does the audit rule -w /etc/sudoers -p wa -k privilege do?

    Watch rules log events; they do not block. The key is used with ausearch -k.
  3. 3. Where should an AIDE baseline database be stored for the best protection?

    An attacker with root could update a local baseline after tampering, hiding the changes.
  4. 4. How should you treat a Lynis hardening score?

    Suggestions need judgement; blindly applying all of them can break workloads.

Summary

  • auditd records kernel-level events; file watches and syscall rules, tagged with keys, let you answer who changed what.
  • ausearch and aureport turn raw records into readable answers; forward logs to a remote host.
  • AIDE (or rpm -Va, debsums, sha256sum -c) detects changed files; protect the baseline from tampering.
  • Lynis and OpenSCAP with CIS benchmarks grade and guide hardening; review every suggestion before applying it.

Next: System Logs with journald and rsyslog