Ansible Role linuxfabrik.lfops.lynis¶
This role installs Lynis, the security auditing tool, runs an audit of the host once a day through a systemd timer, and deploys /etc/lynis/custom.prf, which lists the Lynis tests accepted on the host. The results are left in /var/log/lynis.log and /var/log/lynis-report.dat for the lynis-logfile monitoring plugin to evaluate.
Available in the next LFOps release.
How the Role Behaves¶
- The role installs the
lynispackage of the distribution, from EPEL on the Red Hat family and from the distribution repositories on Debian and Ubuntu. - The Lynis release of the distribution is several releases behind upstream on Debian 12 and Ubuntu 22.04. On Debian and Ubuntu the package disables the online check for a newer release (
skip-upgrade-test=yesin/etc/lynis/default.prf), so an outdated release is not reported there. On the Red Hat family Lynis looks up the latest release in a DNS TXT record on every audit and reports an outdated release (testLYNIS), as a suggestion first and as a warning once it is ten releases behind. lynis.timerstartslynis.serviceonce a day atlynis__on_calendar, which runslynis audit system --cronjob --quietas root with low CPU and I/O priority. A run takes about two minutes. A host that was down at that time catches up after the next boot (Persistent=true).- The role deploys both units to
/etc/systemd/systemon every platform. On Debian and Ubuntu they replace thelynis.timerandlynis.servicethe package ships, so the audit runs at the same time and with the same options everywhere. - Every audit overwrites
/var/log/lynis.logand/var/log/lynis-report.dat.lynis show details <TEST-ID>explains a finding from that log. /etc/lynis/custom.prfis fully templated fromlynis__skip_tests. On every run it is re-rendered (a timestamped backup is kept), so a hand-editedcustom.prfis overwritten. Every audit on the host reads it: the daily one oflynis.timer, and a network scan with the lynis monitoring plugin from a management host.- By default
/etc/lynis/custom.prfskips the Lynis tests whose suggestion LFOps deliberately does not follow, so the monitoring plugin only reports what is left to do. Where Lynis and the CIS Benchmarks cover the same setting and disagree, LFOps follows CIS (for exampleMaxSessions 10in sshd, where Lynis wants 2). A suggestion only Lynis makes is skipped only where following it would break something (for exampleallow_url_fopen = Offin PHP). Each entry carries its reason as a comment incustom.prf. A test that checks several values is only skipped for the one value, for exampleKRNL-6000:kernel.sysrqinstead of all ofKRNL-6000. To have Lynis report one of them again, add it tolynis__skip_tests__host_varorlynis__skip_tests__group_varwithstate: 'absent'. - To accept a finding the monitoring plugin reports, take its test ID from the plugin output. Where the plugin says "add
skip-test=NETW-3015to/etc/lynis/custom.prf", add- name: 'NETW-3015'tolynis__skip_tests__host_varorlynis__skip_tests__group_var, and state the reason incomment. The finding disappears with the next audit. - The role validates each test ID before it writes the file. Lynis refuses to run at all if a setting line of a profile contains a character outside of letters, digits and
/[]()_|,.:;=-, so a typo would otherwise silence the whole audit instead of one test.
Dependent Roles¶
Any LFOps playbook that installs this role runs these for you. Optional ones can be disabled via the playbook's skip variables.
- On RHEL-compatible systems, the EPEL repository must be enabled (role: linuxfabrik.lfops.repo_epel).
- On Rocky 9+, the CRB ("Code Ready Builder") repository must be enabled, since EPEL packages depend on it (role: linuxfabrik.lfops.repo_baseos).
Tags¶
lynis
- Installs Lynis.
- Deploys
/etc/lynis/custom.prf. - Deploys
lynis.serviceandlynis.timerand ensures the timer is in the desired state. - Triggers: none.
lynis:configure
- Deploys
/etc/lynis/custom.prf. - Triggers: none.
lynis:state
- Manages the state of
lynis.timer(start, stop, enable, disable). - Triggers: none.
Optional Role Variables¶
lynis__on_calendar
- When
lynis.timerruns the audit, in the calendar event format ofsystemd.time(7). - Type: String.
- Default:
'*-*-* 06:{{ 59 | random(seed=inventory_hostname) }}' - Deviates from the upstream default
daily(midnight, randomized by up to 30 minutes): the audit runs at a fixed minute per host, so the results are in place at a predictable time. It runs after the reboot window of theschedule_rebootrole (04:xx), so a report from before the reboot does not keep findings like KRNL-5830 ("Reboot of system is most likely needed") alive for a whole day.
lynis__skip_tests__host_var / lynis__skip_tests__group_var
- Lynis tests to skip on the host, written to
/etc/lynis/custom.prfasskip-test=<name>lines. Use this to accept a finding or a suggestion, for example one the lynis-logfile monitoring plugin reports. - Type: List of dictionaries.
- Default:
ACCT-9622,ACCT-9626,ACCT-9630,AUTH-9230,AUTH-9282,AUTH-9284,AUTH-9328,BOOT-5264,DBS-1884,DBS-1886,HRDN-7222,HRDN-7230,HTTP-6640,KRNL-6000:dev.tty.ldisc_autoload,KRNL-6000:fs.protected_fifos,KRNL-6000:fs.protected_regular,KRNL-6000:kernel.modules_disabled,KRNL-6000:kernel.sysrq,KRNL-6000:net.ipv4.conf.all.forwarding,KRNL-6000:net.ipv4.conf.all.rp_filter,LOGG-2190,PHP-2320,PHP-2368,PHP-2372,PHP-2374,PHP-2376,PHP-2378,SSH-7408:allowtcpforwarding,SSH-7408:maxsessions,SSH-7408:port, seeroles/lynis/vars/main.ymlfor the reason per entry, which ends up as a comment incustom.prf. Platform-specific entries are added on Debian and Ubuntu. - Deviates from the upstream default
[]: Lynis would otherwise keep reporting every deliberate LFOps default as a suggestion (see "How the Role Behaves"). -
Subkeys:
-
name:- Mandatory. The Lynis test ID as the monitoring plugin reports it, for example
NETW-3015. To skip a single check within a test, append it after a colon, for exampleSSH-7408:loglevel.lynis show details <TEST-ID>on the host explains a test. - Type: String.
- Mandatory. The Lynis test ID as the monitoring plugin reports it, for example
-
comment:- Optional. Why the test is skipped. Written as a comment line above the
skip-testline, so the reason is on the host as well. - Type: String.
- Optional. Why the test is skipped. Written as a comment line above the
-
state:- Optional.
presentorabsent. - Type: String.
- Default:
'present'
- Optional.
-
lynis__timer_enabled
- Enables or disables
lynis.timer, analogous tosystemctl enable/disable. - Type: Bool.
- Default:
true
lynis__timer_state
- Changes the state of
lynis.timer, analogous tosystemctl start/stop/restart. - Type: String. One of
restarted,started,stopped. - Default:
'started'
Example:
# optional
lynis__on_calendar: '*-*-* 03:15'
lynis__skip_tests__host_var:
- name: 'SSH-7408:loglevel'
comment: 'sshd logs to a central log server with its own log level'
- name: 'HRDN-7222'
state: 'absent'
lynis__timer_enabled: true
lynis__timer_state: 'started'
Troubleshooting¶
The run aborts with lynis__skip_tests: "..." is not a Lynis test ID
- An entry in
lynis__skip_tests__*_varis not of the formNETW-3015,KRB5-1030orSSH-7408:loglevel. Copy the test ID from the output of the monitoring plugin or fromlynis show details.