Cybersecurity

Rotate and retain Linux logs

Use logrotate, protect archives, and verify that you can recover them.

Systems and devices Author · iulian@iptest.ro ·
Editorial date · 2026-03-25

Visual example for: Rotate and retain Linux logs
  1. Identify the log source first. System services may write to journald and applications may write files under /var/log. This example uses /var/log/app/app.log.
  2. sudo journalctl --disk-usage
  3. Add a rule under /etc/logrotate.d/ for the application file. Choose daily rotation, 14 archives, compression, missingok and notifempty. Check ownership, permissions and the application's guidance for reopening its log file.
  4. sudo logrotate --debug /etc/logrotate.conf
  5. Check whether a timer or cron runs logrotate. After the first rotation, confirm that the app writes to the new file and the archive is readable.
  6. sudo systemctl status logrotate.timer
  7. Copy archives you need to private storage with restricted access and encryption. Pick a retention period that meets your needs and test restoring a file.
  8. During an incident, preserve originals and exact timestamps before rotation. Avoid logging passwords, tokens and authentication headers.

All guides