Home UbuntuHow to Configure Logrotate for Ubuntu 24.04 Servers

How to Configure Logrotate for Ubuntu 24.04 Servers

by Knowledge Base Linux
logrotate configuration on Ubuntu 24.04 server

Log files grow forever if nobody tends them, and on a busy Ubuntu server an untended log can fill a disk, crash a service, or hide the very errors you need to diagnose. Learning how to configure logrotate on Ubuntu 24.04 is one of the highest-value maintenance skills for any system administrator: it keeps logs small, searchable, and safely archived without manual intervention.

In this guide you will set up logrotate for a custom application, control rotation by size and time, enable compression and retention, reload services cleanly after rotation, and troubleshoot the common reasons rotation silently fails.

logrotate rotating application logs into dated archives

How Logrotate Works on Ubuntu 24.04

Logrotate is installed by default on Ubuntu. Its main configuration file is /etc/logrotate.conf, and individual rules live in /etc/logrotate.d/. On Ubuntu 24.04, logrotate runs daily through a systemd timer (logrotate.timer) rather than a cron job, although the effect is the same: once a day it reads every rule, checks which logs are due for rotation, and acts on them.

Rotation means renaming the current log file (for example app.log becomes app.log.1), optionally compressing older archives, deleting archives past the retention limit, and then signalling the application to reopen its log file. Most services need that signal, because a process that keeps writing to a renamed file descriptor will keep filling the old file.

Prerequisites

  • An Ubuntu 24.04 server with sudo access.
  • A service or application writing logs to /var/log/ (this guide uses a fictional app writing to /var/log/myapp/*.log).
  • Basic familiarity with the terminal and a text editor such as nano.

Step 1: Inspect the Current Logrotate Setup

Start by looking at the global defaults and confirming the timer is active:

cat /etc/logrotate.conf
systemctl status logrotate.timer
ls /etc/logrotate.d/

The global file sets defaults such as weekly rotation and keeping four archives. Rules in /etc/logrotate.d/ override those defaults for specific logs. You should almost never edit the global file for one application; instead, add a dedicated file.

Step 2: Create a Custom Logrotate Configuration

Create a rule for your application with nano:

sudo nano /etc/logrotate.d/myapp

Add the following configuration:

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

What each directive does:

  • daily rotates once per day. Alternatives are hourly, weekly, and monthly.
  • missingok suppresses errors if no log file exists yet.
  • rotate 14 keeps fourteen archives, roughly two weeks of history.
  • compress gzips old archives; delaycompress leaves the most recent archive uncompressed so tools reading yesterday’s log still work.
  • notifempty skips rotation when the log is empty.
  • create 0640 www-data adm recreates the log file with the right owner, group, and permissions after rotation.
  • postrotate reloads the service so it reopens the fresh log file.

Step 3: Test the Configuration Before Waiting a Day

Never wait until tomorrow to discover a typo. Run logrotate in debug mode first, which simulates a run without changing anything:

sudo logrotate -d /etc/logrotate.d/myapp 2>&1 | head -60

Read the output carefully: it shows which files match, which rules apply, and whether the postrotate script would run. Then force a real rotation once to prove the whole chain works:

sudo logrotate -f /etc/logrotate.d/myapp
ls -lh /var/log/myapp/
systemctl status myapp.service

If the service is still running and a fresh empty log file exists, your configuration is correct.

Step 4: Rotate by Size for High-Volume Logs

Daily rotation is not enough for a log that grows by gigabytes in hours. Combine a time rule with a size limit:

/var/log/myapp/*.log {
    daily
    maxsize 100M
    rotate 30
    compress
    missingok
    notifempty
    create 0640 www-data adm
    sharedscripts
    postrotate
        systemctl reload myapp.service > /dev/null 2>&1 || true
    endscript
}

With maxsize 100M, logrotate still runs daily but also rotates a file the moment it passes 100 MB. Note that maxsize only takes effect when logrotate runs, so for extremely chatty applications consider enabling the hourly timer with systemctl enable --now logrotate.timer adjustments or a dedicated cron entry.

Step 5: Retention, Compression, and Disk Safety

Retention is a trade-off between debuggability and disk space. Fourteen to thirty archives suit most web applications; audit or compliance logs may need ninety days or more, ideally shipped off the server with a tool like rsyslog forwarding. Check how much space archives consume:

du -sh /var/log/myapp/
df -h /var/log

If logs still grow too fast, prefer fixing the source: raise the application log level from debug to warning in production, or add maxsize before reaching for shorter retention.

Troubleshooting Logrotate Problems

Logs are not rotating at all

First check the logrotate state file, which records when each log was last rotated:

grep myapp /var/lib/logrotate/status

If the timestamp looks current, logrotate believes it already rotated and is waiting for the next interval. Run with -d to see its reasoning, and confirm the timer actually fires with systemctl list-timers logrotate.timer.

Rotation happens but the old file keeps growing

This is the classic missed-signal problem: the application still holds the renamed file open. Verify your postrotate command works by hand, and prefer copytruncate only as a last resort for applications that cannot be signalled, since it risks losing lines written during the copy.

Permission errors after rotation

If the application cannot write to the recreated file, the create line has the wrong owner or mode. Match it to the user the service runs as, visible in ps aux | grep myapp or the service unit file.

Conclusion

A well-tuned logrotate setup is quiet infrastructure: it prevents disk-full outages, keeps recent logs instantly readable, and archives history in a predictable place. Start with a dedicated file per application, test with -d and -f before trusting it, and revisit retention whenever an application’s log volume changes. For a broader view of Linux logging, see our guides on reading logs with journalctl on Ubuntu 24.04 and centralizing logs with rsyslog. The official logrotate manual page documents every directive used here.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.