Logrotate prevents log files from consuming all available disk space by automatically rotating, compressing, and deleting old log files. Ubuntu uses logrotate by default for system logs. This guide configures logrotate for system and custom application logs on Ubuntu 26.04 LTS.
Tested and valid on:
- Ubuntu 26.04 LTS
Prerequisites
- Ubuntu 26.04 LTS
- A user with sudo privileges
Step 1 – Check Logrotate Installation
which logrotate
logrotate --version
Step 2 – Understand the Configuration
cat /etc/logrotate.conf
ls /etc/logrotate.d/
Step 3 – Inspect an Existing Config
cat /etc/logrotate.d/nginx
Step 4 – Create a Custom Log Rotation Config
sudo nano /etc/logrotate.d/myapp
Add:
/var/log/myapp/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data www-data
sharedscripts
postrotate
systemctl reload myapp 2>/dev/null || true
endscript
}
Step 5 – Test the Configuration
sudo logrotate --debug /etc/logrotate.d/myapp
sudo logrotate --force /etc/logrotate.d/myapp
Step 6 – Logrotate Directives Explained
daily/weekly/monthly— rotation frequencyrotate N— keep N old log filescompress— gzip rotated logsdelaycompress— compress on next rotation (for apps that keep file open)missingok— no error if log file is missingnotifempty— do not rotate empty filescreate— create new log file with specified permissionspostrotate— run command after rotation
Step 7 – Check Rotation Status
cat /var/lib/logrotate/status | head -20
Conclusion
Logrotate is configured on Ubuntu 26.04 LTS. It runs daily via cron/systemd timers. Create per-application configurations in /etc/logrotate.d/ to ensure all application logs are rotated appropriately.