Sub topic 15
Configuring journald and rsyslog
The CIS logging section has two goals. Logs must survive a reboot and be protected from tampering and disk exhaustion. Logs must also reach a place the attacker cannot easily edit. This chapter covers systemd-journald and rsyslog, the two logging daemons on most modern distributions.
It assumes you can read systemd units, use journalctl, and edit files under /etc as root. It also assumes you have run a baseline scan, as in the OpenSCAP chapter, so you can confirm that failing logging rules now pass.
How the two daemons fit together
systemd-journald collects messages from services, the kernel, and stdout of units. It stores them in a binary journal. rsyslog is a traditional syslog daemon. It writes plain text files under /var/log and can forward messages to a remote log host.
CIS benchmarks usually assume both run. journald is the collector, and it forwards each message to rsyslog through a local socket. rsyslog then applies file permissions and remote forwarding rules.
Configuring journald
Do not edit /etc/systemd/journald.conf directly. Put your settings in a drop-in file so package updates do not overwrite them. The benchmark items for journald cover three areas:
- Persistence:
Storage=persistentwrites the journal to disk. With the defaultauto, the journal is kept only in memory unless/var/log/journalalready exists. After a reboot, or after an intruder crashes the host, volatile logs are gone. - Compression:
Compress=yeskeeps large messages from using excess disk. - Forwarding:
ForwardToSyslog=yeshands messages to rsyslog. This setting only matters when rsyslog is installed and running.
Size limits are not mandated by number, but you should set them so logs cannot fill the disk:
# /etc/systemd/journald.conf.d/60-cis.conf
[Journal]
Storage=persistent
Compress=yes
ForwardToSyslog=yes
SystemMaxUse=1G
SystemKeepFree=2G
MaxRetentionSec=1monthApply and check the result:
sudo mkdir -p /var/log/journal
sudo systemctl restart systemd-journald
journalctl --disk-usage
systemd-analyze cat-config systemd/journald.conf | tail -n 12systemd-analyze cat-config prints the main file followed by every drop-in, so you can see which value wins. For a repeated key, the file read last takes effect. Drop-ins are read in lexical order, so a later 99-local.conf can silently override your 60-cis.conf.
SystemMaxUse and SystemKeepFree interact. journald uses the smaller of the two limits, so a small disk may be capped well below 1 GB. Size them against the disk that holds /var/log, ideally a separate partition, as covered in the filesystem chapter.
Configuring rsyslog
Install rsyslog if it is missing, then enable it:
sudo dnf install -y rsyslog # RHEL family
# sudo apt install -y rsyslog # Debian family
sudo systemctl enable --now rsyslog
systemctl is-enabled rsyslogThree rsyslog checks come up most often in the benchmark:
- File creation mode. New log files must not be world-readable. Set
$FileCreateMode 0640or stricter, and place it before any rule that writes files. Because rsyslog reads/etc/rsyslog.d/*.confin order, a name with a low number such as00-runs first. - Remote forwarding. Logs kept only on the host can be erased by an attacker who gains root. Send a copy to a central log host.
- No unintended listeners. Unless this server is the log host, it should not accept remote messages. Make sure the
imtcpandimudpmodules are not loaded.
A forwarding configuration with TLS looks like this:
# /etc/rsyslog.d/60-cis.conf
$FileCreateMode 0640
global(
DefaultNetstreamDriver="gtls"
DefaultNetstreamDriverCAFile="/etc/pki/tls/certs/loghost-ca.pem"
)
*.* action(
type="omfwd"
target="loghost.example.com"
port="6514"
protocol="tcp"
StreamDriver="gtls"
StreamDriverMode="1"
StreamDriverAuthMode="x509/name"
StreamDriverPermittedPeers="loghost.example.com"
action.resumeRetryCount="-1"
queue.type="LinkedList"
queue.filename="fwd_loghost"
queue.maxDiskSpace="1g"
queue.saveOnShutdown="on"
)The TLS driver comes from the rsyslog-gnutls package. The CA path above is a RHEL-style example, so adjust it for your distribution. The disk-assisted queue (queue.filename) holds messages while the log host is unreachable, instead of dropping them. Setting action.resumeRetryCount="-1" retries forever.
Validate the syntax before you restart. A typo in an rsyslog file can stop all logging.
sudo rsyslogd -N1
sudo systemctl restart rsyslog
logger -p auth.notice "cis logging test"
sudo grep "cis logging test" /var/log/secure /var/log/auth.log 2>/dev/nullThe auth log is /var/log/secure on RHEL and /var/log/auth.log on Debian. Then check the log host to confirm the test message arrived.
Log file permissions and rotation
Rotation keeps logs from filling the disk, and permissions protect their contents. Find files that are too open:
sudo find /var/log -type f \( -perm /g+wx -o -perm /o+rwx \) -exec ls -l {} +Files with group write or execute, or any world access, fail the CIS check. Be aware that /var/log/btmp, /var/log/wtmp and /var/log/lastlog have their own expected modes, and some benchmark versions list them separately. Fix a real finding with chmod, then fix the cause, usually a create line in /etc/logrotate.d/ that sets a loose mode. If you only fix the file, the next rotation will recreate the problem.
Common errors
- Drop-in is ignored. The file lacks the
[Journal]section header, or a later file overrides it. Check withsystemd-analyze cat-config. - Journal is still volatile after reboot.
Storage=persistentwas set but the service was not restarted./var/log/journalalso needs to exist on older systemd versions. - Duplicate messages. Some services write to both the journal and their own file. This is expected, and the rsyslog copy is what you forward.
- Forwarding silently fails. The CA file is missing, the permitted peer name does not match the certificate, or a firewall blocks TCP 6514. Read
journalctl -u rsyslogfor the TLS error.
Exercise
After setting Storage=persistent in a drop-in file and restarting systemd-journald, journalctl --list-boots still shows only the current boot after a reboot. Name two checks you would run and what each would tell you.
Show answer
First, run systemd-analyze cat-config systemd/journald.conf to confirm the drop-in has a [Journal] header and that no later file sets Storage= to another value. Second, check that /var/log/journal exists and is owned by root:systemd-journal, for example with ls -ld /var/log/journal, since journald writes persistent logs only to that directory. Also running journalctl --disk-usage shows whether a size limit such as SystemKeepFree is forcing the journal to be small or empty.