Sub topic 9
Taming Logs with logrotate and journald Limits
Assume you can use a shell with sudo, read df and du output, and understand files, directories, and services. This chapter focuses on controlling log growth after you have found that logs consume filesystem blocks or inodes.
Two logging systems
Linux servers commonly use both plain-text log files and the systemd journal.
- logrotate manages files such as
/var/log/auth.log,/var/log/nginx/access.log, and application logs. It renames, compresses, removes, or recreates files according to configuration. - journald stores structured log entries managed by
systemd-journald. You inspect them withjournalctl.
These systems can overlap. A service may write directly to a file, send messages to the journal, or do both.
| System | Main storage | Typical control |
|---|---|---|
| logrotate | Log files under `/var/log` or application directories | Rotation rules in `/etc/logrotate.conf` and `/etc/logrotate.d/` |
| journald | Journal files under `/var/log/journal` or `/run/log/journal` | Settings in `journald.conf` and vacuum commands |
Rotate text logs safely
A rotation rule usually runs daily or weekly. It keeps a fixed number of old files, compresses them, and creates a fresh file with suitable ownership and permissions.
A typical rule looks like this:
/var/log/myapp/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
create 0640 myapp adm
sharedscripts
postrotate
systemctl reload myapp.service >/dev/null 2>&1 || true
endscript
}rotate 14 keeps fourteen old rotations. compress saves space with gzip, while delaycompress leaves the newest rotated file uncompressed for one cycle. Some programs keep the old file descriptor open, so they must be reloaded after rotation. A reload lets the service close the old file and open the new one.
The create line sets the mode, owner, and group of the replacement file. Verify these values against the service. Incorrect ownership can stop a service from writing logs.
Test a rule without changing files:
sudo logrotate -d /etc/logrotate.confForce a rotation only when you understand its effects:
sudo logrotate -f /etc/logrotate.confThe -d option means debug mode. The -f option forces rotation and can create many files during testing.
Limit the systemd journal
Check the journal's current disk usage before changing anything:
journalctl --disk-usageRemove archived entries until the journal is approximately 500 MB:
sudo journalctl --vacuum-size=500MYou can also remove entries older than a period:
sudo journalctl --vacuum-time=14dVacuuming removes archived files. It does not normally remove the journal file currently being written.
For a persistent limit, create a drop-in configuration file:
# /etc/systemd/journald.conf.d/limits.conf
[Journal]
SystemMaxUse=1G
SystemKeepFree=2G
MaxRetentionSec=14daySystemMaxUse limits journal storage. SystemKeepFree reserves free space for other system activity. The effective limit is influenced by both settings and the available filesystem capacity.
Apply the configuration:
sudo systemctl restart systemd-journaldChoose the right control
First identify the owner of the data with du, lsof, service configuration, or journal commands. Then change the matching control. Avoid deleting files blindly, because a service may recreate them with wrong permissions or continue holding deleted data open.
Exercise
A server has large files under /var/log, but the application continues writing to the old file after rotation. What should you check and change?
Show answer
Check whether the rotation rule has a postrotate action. Configure the application to reload or reopen its logs after rotation, then test the rule with logrotate's debug mode before forcing it.