Logs grow without end unless something trims them. A full /var can stop services, so Linux rotates logs: the current file is renamed, a fresh one is started, and old copies are compressed and eventually deleted.
logrotate
logrotate is the usual tool for text logs. It is run periodically, by a systemd timer or a daily cron job depending on the distribution.
Global settings are in /etc/logrotate.conf. Packages and administrators add files to /etc/logrotate.d/.
A configuration for an application:
/var/log/myapp/*.log {
weekly
rotate 4
compress
delaycompress
missingok
notifempty
create 0640 myapp adm
}weekly: rotate once a weekrotate 4: keep four old copiescompress: gzip old logsdelaycompress: leave the most recent old copy uncompressed until next timemissingok: no error if the log is absentnotifempty: skip empty filescreate: make a new file with this mode, owner and group
Programs that keep files open
A program may keep writing to the renamed file. A postrotate script, closed by endscript, can tell it to reopen its log. Another choice is copytruncate, which copies the file and then empties the original. It needs no signal, but lines written between the two steps can be lost.
Testing
Debug mode shows what would happen without changing anything:
$ sudo logrotate -d /etc/logrotate.d/myappTo force a rotation now, use -f:
$ sudo logrotate -f /etc/logrotate.d/myappTesting one file this way skips the global defaults from /etc/logrotate.conf.
Journal size
logrotate does not manage the journal. journald has its own limits:
$ journalctl --disk-usage
Archived and active journals take up 120.0M in the file system.
$ sudo journalctl --vacuum-time=2weeks
$ sudo journalctl --vacuum-size=200MVacuuming removes archived journal files, not the active ones. For a permanent cap, set SystemMaxUse=200M under [Journal] in /etc/systemd/journald.conf (or a drop-in file), then restart systemd-journald.
Do not just delete
If you remove a log that a program still has open, the space is not freed until the program closes it. To empty one in place, use sudo truncate -s 0 file. Check you do not need its contents for an audit or investigation first.
Try it
Look through /etc/logrotate.d/ and read one entry. Run logrotate -d on it with sudo, then check journalctl --disk-usage.
Check your understanding
Managing Log Files
All 5 sections ยท no timer
Unlimited retries
Questions from every section are combined into one assessment. Answer them all correctly to complete the article.
Your completion saves in this browser. Sign in to sync it to your account.