Home How ToHow to Configure logrotate on Linux for Log Rotation

How to Configure logrotate on Linux for Log Rotation

by Knowledge Base Linux
How to Configure logrotate on Linux — How to Configure logrotate on Linux for Log Rotation

Unchecked log files are one of the most common reasons a healthy server suddenly reports “no space left on device”. The solution is a proper logrotate configuration: logrotate is the standard Linux utility that rotates, compresses and deletes old logs automatically. In this guide you will learn how logrotate works, read a real configuration, and write a custom logrotate configuration for your own applications. This article covers How to Configure logrotate on Linux in detail.

How logrotate Works – How to Configure logrotate on Linux

logrotate runs once a day, triggered by a systemd timer or cron. It reads its main configuration from /etc/logrotate.conf and per-service files from /etc/logrotate.d/ — packages like Nginx, Apache and MySQL drop their own rules there during installation. For each log file, logrotate decides whether rotation is due based on size or age, then renames the file, optionally compresses it, and keeps only the number of old copies you configured.

Check the global defaults on your system:

cat /etc/logrotate.conf
ls /etc/logrotate.d/

Most distributions rotate weekly, keep four copies and compress old logs. That is sensible for system logs, but busy application logs often need custom rules — which is where your own configuration comes in.

Anatomy of a logrotate Configuration – How to Configure logrotate on Linux

A typical block, like the one Nginx installs, looks like this:

/var/log/nginx/*.log {
    daily
    missingok
    rotate 14
    compress
    delaycompress
    notifempty
    create 0640 www-data adm
    sharedscripts
    postrotate
        [ -f /var/run/nginx.pid ] && kill -USR1 $(cat /var/run/nginx.pid)
    endscript
}

The directives you will use most:

  • daily / weekly / monthly — rotation schedule
  • size 100M — rotate when the file exceeds this size (overrides the schedule)
  • rotate 14 — keep 14 old copies, delete the rest
  • compress — gzip old logs; delaycompress waits one cycle, useful when a process keeps writing to the rotated file
  • missingok and notifempty — do not error on missing or empty logs
  • create 0640 user group — recreate the empty log with these permissions; alternatively use copytruncate for applications that cannot reopen their log file
  • su root adm — required on Ubuntu when the log directory is owned by a non-root user
  • postrotate ... endscript — command run after rotation, usually telling the daemon to reopen its logs
Why a logrotate configuration prevents disks filling up with logs

Step 1: Write a Custom logrotate Configuration

Suppose your application writes to /var/log/myapp/app.log and grows by gigabytes per week. Create /etc/logrotate.d/myapp:

/var/log/myapp/*.log {
    daily
    size 200M
    rotate 7
    compress
    missingok
    notifempty
    copytruncate
    su root root
    create 0644 root root
}

Using both daily and size 200M means the log rotates at least daily and immediately whenever it passes 200 MB — whichever condition hits first, protecting your disk around the clock. If you have ever had to clear the journal log in an emergency, you know why this safety net matters.

Step 2: Test Your Configuration Safely

Never deploy a logrotate configuration untested. Debug mode simulates a full run and prints every decision without touching a single file:

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

When the output looks right, force one real rotation to prove it works end to end:

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

logrotate records its last-run state in /var/lib/logrotate/status — check this file when a log mysteriously stops rotating.

Step 3: Make Sure logrotate Runs

On systemd systems, verify the timer:

systemctl list-timers logrotate.timer
systemctl status logrotate.timer

On older systems it is a daily cron script at /etc/cron.daily/logrotate. Either way, if a server’s /var keeps filling up, check disk usage patterns with the commands from our guide on how to check disk space on Linux to confirm logs are really the culprit. The complete directive list lives in the logrotate man page.

Troubleshooting

“error setting owner of … : Operation not permitted”: the log directory has different ownership — add the correct su user group directive to the block.

Rotated but the disk is still full: the application kept writing to the renamed file because it never reopened its log. Use copytruncate, or add a postrotate script that signals the service.

Configuration ignored: check file ownership and permissions — logrotate refuses world-writable configs — and confirm your file is actually inside /etc/logrotate.d/.

Rotating Logs for Containers and Custom Services

Two modern cases deserve attention. First, Docker’s default json-file logging driver is not handled by the host’s logrotate; cap it in /etc/docker/daemon.json with max-size and max-file options, or container logs will grow forever. Second, systemd services write to the journal rather than files, and the journal has its own size limits in /etc/systemd/journald.conf (SystemMaxUse=) — logrotate never sees those entries. For files that your own daemons write directly, prefer copytruncate only when the application cannot reopen its log; truncation has a small race where lines written during the copy can be lost, which is why a proper postrotate signal is the better long-term pattern.

Conclusion

A thoughtful logrotate configuration is cheap insurance against the 3 a.m. full-disk outage. Review the defaults in /etc/logrotate.d/, add explicit rules for every application that writes its own logs, test with debug mode, and your logs will stay useful instead of becoming a liability.

You may also like

KbLinux

Typically replies within a day

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