Sub topic 7
Limiting Journal Size and Age
Prerequisites and the storage model
This chapter assumes that you can edit files with sudo, use systemctl, and read basic output from journalctl. You should also understand that a Linux server may have both a systemd journal and traditional files under /var/log.
systemd-journald is the service that collects messages from the kernel, services, and applications. It stores these messages in journal files. With Storage=persistent, journal files are stored under /var/log/journal and survive a reboot. With volatile storage, they normally live under /run/log/journal and are lost when the server restarts.
Journal retention works by deleting older archived journal files when a size or age limit is reached.
Inspect current usage
Use journalctl --disk-usage to see the space currently used by archived and active journal files:
sudo journalctl --disk-usageYou can inspect the current journal settings and their source files with:
sudo systemd-analyze cat-config systemd/journald.confDistribution defaults may be stored in /usr/lib/systemd/ or /lib/systemd/. Put local changes in /etc, where package upgrades will not overwrite them.
Set persistent size and age limits
Create a drop-in directory and configuration file:
sudo install -d -m 0755 /etc/systemd/journald.conf.d
sudo tee /etc/systemd/journald.conf.d/retention.conf >/dev/null <<'EOF'
[Journal]
Storage=persistent
SystemMaxUse=500M
SystemKeepFree=1G
MaxRetentionSec=1month
EOFThese settings have different purposes:
Storage=persistentrequests storage in/var/log/journal.SystemMaxUse=500Mlimits space used by persistent journal files.SystemKeepFree=1Gtells journald to leave at least 1 GB free on the containing file system.MaxRetentionSec=1monthremoves journal files older than one month when they are eligible for cleanup.
The effective size limit can be lower than SystemMaxUse if keeping 1 GB free requires more space. Journald also creates and rotates files internally, so usage may not stop at an exact byte value.
Apply the configuration and check the result:
sudo systemctl restart systemd-journald
sudo journalctl --disk-usageA restart does not immediately remove every file that violates a newly chosen policy. To clean archived files now, use:
sudo journalctl --vacuum-size=500M
sudo journalctl --vacuum-time=1monthVacuuming affects archived files. The journal may retain the currently active file until journald rotates it.
Runtime storage and failure cases
RuntimeMaxUse limits journals stored under /run, which is normally a memory-backed file system. It matters when persistent storage is unavailable or when Storage=volatile is configured. For servers that must retain logs across reboots, verify that /var/log/journal exists and that its file system is writable.
Exercise
A server has SystemMaxUse=500M and SystemKeepFree=1G on a file system with only 1.2 GB free. Why might journald use less than 500 MB?
Show answer
SystemKeepFree reserves 1 GB for other data. The available free space therefore imposes a lower effective limit than SystemMaxUse.