Skip to content

Configure Logrotate Linux: Rotation & Retention Guide

Configure Logrotate Linux: Complete Log Rotation and Retention Guide

To configure logrotate Linux servers correctly, you need a reliable way to rotate log files, compress older logs, and remove outdated records without interrupting running applications. Linux servers generate logs continuously, and without proper log management, those files can consume disk space and make troubleshooting more difficult.

Logrotate is a standard log management utility available on many Linux distributions. It automates log rotation based on file size, time intervals, and retention policies. It can also compress old logs, create new log files with specific permissions, and execute scripts when rotation occurs.

In this guide, you will learn how to configure Logrotate on Linux servers, create custom rotation rules, manage application logs, test configurations with logrotate -d, troubleshoot common errors, and verify that your log rotation policy works as expected.

What Is Logrotate in Linux?

Logrotate is a Linux utility that manages log files automatically. Instead of allowing a single log file to grow indefinitely, Logrotate renames or replaces the current file and keeps older versions according to a defined retention policy.

For example, a web server might generate an access log that grows several gigabytes over time. Logrotate can rotate that file daily, compress older copies, and delete versions after a specified number of rotations.

Typical Logrotate capabilities include:

  • Log rotation: Creates a new active log file while preserving older logs.
  • Compression: Compresses rotated files to reduce disk usage.
  • Retention: Keeps a defined number of historical log files.
  • Scheduled rotation: Rotates logs daily, weekly, monthly, or according to other rules.
  • Size-based rotation: Rotates logs when they exceed a configured size.
  • Permission management: Creates new log files with specified ownership and permissions.
  • Application integration: Runs scripts when log files are rotated.

Logrotate is particularly useful for Linux VPS hosting, web servers, database services, and applications that write large amounts of log data.

Step 1: Check Whether Logrotate Is Installed

Before creating a configuration, check whether Logrotate is available on your Linux server.

logrotate --version

If the command returns a version number, Logrotate is installed.

If the command is unavailable, install it using your distribution’s package manager.

Ubuntu and Debian

sudo apt update
sudo apt install logrotate

AlmaLinux, Rocky Linux, and other DNF-based distributions

sudo dnf install logrotate

On some distributions, Logrotate is already installed as part of the base system.

Next, check the main configuration file:

sudo cat /etc/logrotate.conf

List the available configuration files:

ls -l /etc/logrotate.d/

The main configuration is usually stored in /etc/logrotate.conf, while service-specific rules are commonly placed in /etc/logrotate.d/.

Step 2: Understand the Logrotate Configuration Structure

The main Logrotate configuration file contains global settings and can include additional configuration files.

A typical configuration might contain:

weekly
rotate 4
create
dateext
include /etc/logrotate.d

Here is what these directives mean:

Directive Purpose
daily Requests daily rotation when Logrotate runs
weekly Requests weekly rotation when Logrotate runs
monthly Requests monthly rotation when Logrotate runs
rotate 7 Retains up to seven rotated copies
compress Compresses rotated log files
delaycompress Delays compression of the most recently rotated file
missingok Does not report an error if the log file is missing
notifempty Skips rotation when the log file is empty
create Creates a replacement log file with specified attributes
dateext Uses dates in rotated filenames
maxsize Allows rotation when a file exceeds a size threshold, subject to Logrotate being invoked
size Uses file size as a rotation criterion instead of a time interval

The exact behavior depends on the combination and order of directives. For example, size and time-based directives can interact differently from minsize and maxsize.

Important: Logrotate does not continuously monitor files. It checks the configured rules when its command or scheduled job runs. A size threshold therefore does not guarantee immediate rotation as soon as a log reaches that size.

Step 3: Create a Custom Logrotate Configuration

For custom applications, create a separate configuration file instead of modifying the global configuration unnecessarily.

For example, suppose your application writes logs to:

/var/log/myapp/application.log

Create a dedicated configuration:

sudo nano /etc/logrotate.d/myapp

Add the following rules:

/var/log/myapp/application.log {
    daily
    rotate 7
    missingok
    notifempty
    compress
    delaycompress
    dateext
    create 0640 root adm
}

Save the file and exit the editor.

This configuration requests daily rotation, retains up to seven rotated copies, compresses older logs, skips missing or empty files, and creates a replacement log with mode 0640, owned by root and group adm.

Adjust the ownership and permissions to match your application. If the application runs as a dedicated service user, the new log file must be writable by that user or by the application’s logging mechanism. Incorrect ownership can cause the application to stop writing logs.

Also, adm is not necessarily the appropriate group on every distribution. Confirm that the group exists and that your logging setup requires it before using this example.

Use size-based log rotation

If an application generates a large volume of logs, you may want rotation to be based on file size rather than only on a daily schedule.

Example:

/var/log/myapp/application.log {
    size 100M
    rotate 5
    missingok
    notifempty
    compress
    delaycompress
    create 0640 myapp myapp
}

Replace myapp with the actual service account and group.

This configuration requests rotation when Logrotate runs and finds the file above the configured size threshold. It does not trigger rotation by itself when the threshold is reached.

For a configuration that retains daily rotation but also allows rotation when the log becomes large, use maxsize:

/var/log/myapp/application.log {
    daily
    maxsize 100M
    rotate 7
    missingok
    notifempty
    compress
    delaycompress
    create 0640 myapp myapp
}

The actual rotation frequency depends on how often Logrotate is executed.

Step 4: Configure Compression and Log Retention

Old log files can consume substantial storage, especially on busy web servers and application servers. Compression and retention policies help control this growth.

Enable compression

Use:

compress

Logrotate typically compresses rotated logs using gzip, producing files with the .gz extension.

For example:

application.log.1.gz
application.log.2.gz

Compression can significantly reduce the size of text-based logs, although the amount saved depends on the content.

Delay compression of the newest rotated log

Use:

delaycompress

This delays compression of the most recently rotated file until a later rotation. It can be useful when an application or administrator may still need access to the previous log file.

Set a retention period

For seven retained rotations:

rotate 7

This keeps up to seven rotated copies per log file. It does not necessarily mean exactly seven calendar days of history, because the actual period depends on how often rotation occurs and whether a rotation is skipped.

To retain up to 30 rotated copies:

rotate 30

Choose a retention policy based on disk capacity, operational requirements, and any applicable security or compliance obligations.

Use date-based filenames

Add:

dateext

This produces date-based rotated filenames instead of relying only on numbered suffixes. Date formats and filename behavior can vary depending on additional settings.

Step 5: Test Logrotate with logrotate -d

Before applying a new configuration to production logs, test it in debug mode.

Run:

sudo logrotate -d /etc/logrotate.conf

The -d option enables debug mode. Logrotate displays the actions it would take without actually rotating the files or updating its state file.

To test one custom configuration:

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

Review the output for problems such as:

  • Invalid directives or syntax.
  • Missing log files.
  • Unexpected rotation decisions.
  • Incorrect file paths.
  • Conflicting configuration rules.

Debug mode is particularly useful when a rotation rule appears correct but does not behave as expected.

Validate the configuration without rotating logs

You can also use the verbose option in debug mode:

sudo logrotate -dv /etc/logrotate.conf

This provides more detailed output while keeping the test non-destructive.

Do not confuse debug mode with a real rotation. It is designed to simulate the decisions Logrotate would make.

Force a rotation for testing

If you intentionally want to execute a rotation immediately, you can use:

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

The -f option forces rotation even when Logrotate would normally decide that it is not yet due.

Use this carefully on production servers. Forced rotation can affect active logs, create extra rotated files, and trigger configured scripts. Prefer testing with a disposable log file in a controlled environment before forcing rotation on critical services.

Step 6: Configure Logrotate for Application Logs

Custom applications often store logs outside the standard system log directories. A dedicated configuration makes it easier to control retention and permissions for each application.

For example, a web application might use:

/var/log/myapp/access.log
/var/log/myapp/error.log

You can manage both files in a single configuration block:

/var/log/myapp/access.log /var/log/myapp/error.log {
    daily
    rotate 14
    missingok
    notifempty
    compress
    delaycompress
    dateext
    create 0640 myapp myapp
}

This configuration applies the same rules to both logs. Confirm that both files should use the same owner, group, rotation frequency, and retention policy before combining them.

What if the application does not reopen its log file?

Some applications keep a file descriptor open after a log is renamed. If the application continues writing to the old file descriptor, the new log file may remain empty while the renamed file continues growing.

For applications that support reopening log files, Logrotate can run a post-rotation command.

Example:

/var/log/myapp/application.log {
    daily
    rotate 7
    missingok
    notifempty
    compress
    delaycompress
    create 0640 myapp myapp
    postrotate
        /usr/bin/systemctl kill -s HUP myapp.service >/dev/null 2>&1 || true
    endscript
}

This is only an example. Use a signal or reload command that the application explicitly supports. Some services require a reload command, a specific signal, or a different method to reopen logs. Do not assume that sending HUP is safe for every application.

If the application supports logging to standard output and standard error under systemd, consider using the journal instead of maintaining separate application log files. That may simplify retention and collection.

Step 7: Check Whether Logrotate Runs Automatically

Most Linux distributions configure Logrotate to run through a scheduled task or systemd timer. The exact mechanism depends on the distribution and installed packages.

Check for a systemd timer:

systemctl status logrotate.timer

If the timer exists, inspect its schedule:

systemctl list-timers logrotate.timer

Check the related service:

systemctl status logrotate.service

On systems that use cron, inspect the relevant scheduled tasks:

ls -l /etc/cron.daily/logrotate

You can also review system logs for execution errors:

sudo journalctl -u logrotate.service --no-pager -n 100

If your system uses cron rather than systemd, inspect the applicable cron logs for your distribution.

Do not enable a second scheduler without first checking how Logrotate is already being executed. Multiple scheduled invocations can cause confusing behavior and unnecessary checks.

Step 8: Troubleshoot Common Logrotate Problems

Logrotate is not rotating the log file

First, test the configuration:

sudo logrotate -d /etc/logrotate.conf

Then inspect the state file:

sudo cat /var/lib/logrotate/status

The state file records the last rotation dates for tracked logs. Its location can differ when Logrotate is run with custom options or a different configuration.

A log may not rotate because its interval has not elapsed, the file is empty, it is missing, or a size threshold has not been met.

Permission denied errors

Check the directory and log file permissions:

ls -ld /var/log/myapp
ls -l /var/log/myapp/

Confirm that the directory is accessible to the relevant service and that the application can write to its active log.

Review the create directive. A configuration that creates files owned by the wrong user may prevent the application from logging after rotation.

Duplicate or conflicting configuration rules

Inspect the main configuration and included files:

sudo cat /etc/logrotate.conf
ls -l /etc/logrotate.d/

Look for overlapping rules that target the same log file. Multiple definitions can cause errors or unexpected behavior, so keep each log’s configuration clear and avoid duplicate entries.

Log files keep growing after rotation

If a renamed file continues to grow, the application may still be writing through an open file descriptor. Check open files with lsof, if installed:

sudo lsof | grep '/var/log/myapp'

Use an application-supported reopen or reload mechanism. Do not delete an actively written log file as a substitute for proper rotation.

Logrotate reports that a log is insecure

Logrotate may reject a configuration when the log file or its parent directory has unsafe ownership or permissions, depending on the configuration and security checks involved.

Inspect the relevant paths and correct their ownership or permissions according to the application’s requirements. Avoid disabling security checks globally merely to silence an error.

Best Practices for Linux Log Management

Follow these recommendations when configuring Logrotate on production servers:

  1. Test before deploying: Use logrotate -d to review proposed actions without rotating files.
  2. Choose sensible retention: Keep enough history for troubleshooting without allowing unlimited disk growth.
  3. Use compression: Compress older logs when appropriate to save storage.
  4. Set correct permissions: Ensure applications can continue writing to replacement log files.
  5. Keep configurations separated: Use /etc/logrotate.d/ for service-specific rules.
  6. Avoid duplicate rules: Make sure the same log is not accidentally managed by conflicting configurations.
  7. Monitor disk usage: Check log directories and filesystem capacity regularly.
  8. Test application integration: Confirm that services reopen log files correctly after rotation.
  9. Protect sensitive logs: Apply appropriate access controls and retention policies to logs containing personal or authentication-related data.
  10. Verify scheduling: Confirm that Logrotate is actually running through the configured timer or cron job.

Frequently Asked Questions

How do I configure Logrotate on Linux?

Install Logrotate if necessary, inspect /etc/logrotate.conf, and create a service-specific configuration under /etc/logrotate.d/. Define the rotation frequency, retention, compression, and permissions, then validate the configuration with logrotate -d.

What does logrotate -d do?

The logrotate -d command runs Logrotate in debug mode. It displays the decisions and actions that would be performed without actually rotating log files or updating the state file.

How do I rotate logs based on file size?

Use the size directive for size-based rotation or maxsize when you also want a time-based schedule. Logrotate must run for the size condition to be checked, so the threshold is not an immediate trigger.

How many rotated log files should I keep?

The appropriate retention count depends on log volume, available disk space, and operational or compliance requirements. For example, rotate 7 retains up to seven rotated copies, but does not guarantee exactly seven days of history.

Does Logrotate restart applications?

No. Logrotate can execute scripts during rotation, but it does not automatically know how each application manages its log files. If a service must reopen a file, configure an application-supported post-rotation action.

Where are Logrotate configuration files stored?

The primary configuration is usually /etc/logrotate.conf, with additional service-specific configurations in /etc/logrotate.d/. The state file is commonly /var/lib/logrotate/status.

Conclusion

Knowing how to configure Logrotate Linux servers helps prevent uncontrolled log growth, reduce storage usage, and preserve useful historical records for troubleshooting. A well-designed policy combines appropriate rotation intervals, compression, retention, file permissions, and application-specific handling.

Start by reviewing the existing configuration, create a separate rule for each custom application, and test it with logrotate -d before making changes to production logs. Finally, verify that the scheduler runs successfully and that applications continue writing to their active log files after rotation.

Need a reliable environment for your Linux workloads? Explore ServerNext VPS solutions or visit ServerNext to learn more about available hosting options.