Log files grow without bound unless managed. Logrotate is the standard Linux tool that automatically rotates, compresses, and deletes old log files. Ubuntu 24.04 LTS includes Logrotate pre-installed. This guide explains how to configure it.

Tested and valid on:

  • Ubuntu 24.04 LTS

Prerequisites

  • Ubuntu 24.04 LTS server
  • A user with sudo privileges

Step 1 – Verify Logrotate is Installed

Check the version:

logrotate --version

Step 2 – View the Default Configuration

The main config is at /etc/logrotate.conf:

cat /etc/logrotate.conf

Application-specific configs are in /etc/logrotate.d/:

ls /etc/logrotate.d/

Step 3 – Understand Logrotate Directives

Key directives:

  • daily / weekly / monthly — rotation frequency
  • rotate N — keep N old log files
  • compress — gzip old logs
  • delaycompress — compress on next rotation (useful for running daemons)
  • missingok — no error if log file is missing
  • notifempty — skip rotation if file is empty
  • postrotate / endscript — run a command after rotation

Step 4 – Create a Custom Configuration

Create a config for your application:

sudo nano /etc/logrotate.d/myapp

Add:

/var/log/myapp/*.log {
    daily
    rotate 14
    compress
    delaycompress
    missingok
    notifempty
    create 0640 www-data www-data
    postrotate
        systemctl reload myapp 2>/dev/null || true
    endscript
}

Step 5 – Test the Configuration

Do a dry run to verify without rotating:

sudo logrotate -d /etc/logrotate.d/myapp

Step 6 – Force a Rotation

Manually trigger a rotation for testing:

sudo logrotate -f /etc/logrotate.d/myapp

Step 7 – View Rotation History

Check the Logrotate status file:

sudo cat /var/lib/logrotate/status | grep myapp

Conclusion

Logrotate is configured on Ubuntu 24.04 LTS to manage your application logs automatically. Adjust rotation frequency and retention based on your disk space and compliance requirements.