cheat-sheets

Lynis and auditd

Lynis is a host security auditing scanner: it inspects a Linux system and reports warnings, suggestions and a hardening index. auditd is the Linux Audit daemon: it records security-relevant events (file access, commands, logins) selected by rules. Use Lynis to find what to harden and auditd to record what happens afterwards. See Security for the wider hardening checklist.

Project: Lynis Documentation: man lynis, man auditctl, man audit.rules, man ausearch, man aureport

Verification: options come from the Lynis 3.0.9 and Linux audit user-space man pages and --help output on Ubuntu 24.04. lynis audit system --quick was run in a throwaway container and wrote /var/log/lynis.log and /var/log/lynis-report.dat. auditd needs the host kernel and was not run: the audit commands below are from the man pages only (unverified in practice).

See also: Security · fail2ban · CrowdSec · Container Scanning · systemd


Lynis #

COMMANDDESCRIPTION
sudo lynis audit systemFull local audit (interactive; press Enter between sections)
sudo lynis audit system --quickSame without pauses
sudo lynis audit system --warnings-onlyRun quietly, show warnings only
sudo lynis audit system --pentestNon-privileged scan, points of interest for pentesting
sudo lynis audit dockerfile <FILE>Analyse a Dockerfile
sudo lynis --tests "<TEST_ID> <TEST_ID>"Run only specific tests
sudo lynis --tests-from-category "<CATEGORY>"Run one category (see lynis show categories)
sudo lynis --profile <FILE>Use a custom profile
sudo lynis show commandsList commands
sudo lynis show categoriesList test categories (for example security, performance, privacy)
sudo lynis show profilesProfiles in use
sudo lynis show versionVersion
sudo lynis update infoUpdate details

Results are written to:

PATHCONTENT
/var/log/lynis.logDetailed log of every test
/var/log/lynis-report.datMachine-readable results (warning[], suggestion[], hardening_index)
/etc/lynis/default.prfDefault profile; override in a custom .prf file

Typical flow: run the audit, work through suggestion[] and warning[] lines, re-run and watch the hardening index.

bash
sudo lynis audit system --quick
sudo grep -E '^(warning|suggestion)\[\]' /var/log/lynis-report.dat
sudo grep '^hardening_index' /var/log/lynis-report.dat

Distribution packages can lag the upstream release (the audit warns when the version is old). Fetch current versions from the project if you need the latest tests.


auditd: Rules #

Rules live in /etc/audit/rules.d/*.rules and are merged by augenrules. Load changes with augenrules --load.

COMMANDDESCRIPTION
sudo auditctl -sDaemon status
sudo auditctl -lList loaded rules
sudo auditctl -w <PATH> -p wa -k <KEY>Watch a file or directory for writes and attribute changes (r, w, x, a)
sudo auditctl -W <PATH> -p wa -k <KEY>Remove that watch
sudo auditctl -a always,exit -F arch=b64 -S <SYSCALL> -k <KEY>Audit a system call
sudo auditctl -DDelete all rules (live only)
sudo auditctl -e 2Make the configuration immutable until reboot (put last in the rules)
sudo augenrules --checkCheck whether the rules files have changed
sudo augenrules --loadRebuild audit.rules and load it

Example /etc/audit/rules.d/50-homelab.rules:

text
-w /etc/passwd -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/ssh/sshd_config -p wa -k sshd
-a always,exit -F arch=b64 -S execve -F euid=0 -k root_commands

Rules set with auditctl are lost on reboot; files in rules.d persist.


auditd: Searching Logs #

Events are in /var/log/audit/audit.log (root only).

COMMANDDESCRIPTION
sudo ausearch -k <KEY>Events matching a rule key
sudo ausearch -k <KEY> -iSame, with names instead of numbers
sudo ausearch -f <PATH>Events for a file
sudo ausearch -ui <UID> -ts todayOne user’s events since the start of today
sudo ausearch -m USER_LOGIN --success no -ts recentRecent failed logins (message type and success filters)
sudo aureport --summarySummary report
sudo aureport -au --failed --summaryFailed authentication summary
sudo aureport -x --summaryExecutables summary
sudo aureport -f -iFile access report, interpreted

Troubleshooting #

SYMPTOMFIX
Error opening /var/log/audit/audit.logRun as root; check the auditd service is running
Rules vanish after rebootPut them in /etc/audit/rules.d/ and run augenrules --load
auditctl rules refusedThe configuration is immutable (-e 2); reboot to change
Huge audit logTune max_log_file and num_logs in /etc/audit/auditd.conf (see man auditd.conf)
Lynis keeps reporting the same suggestionSkip it with a profile (skip-test=<TEST_ID> in a custom .prf; see the profile comments)

See Also #

Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.