How To Configure Logrotate in Ubuntu: A Step-by-Step Guide

In this guide you will learn How To Configure Logrotate in Ubuntu.
Log files are essential for monitoring system events, troubleshooting issues, and analyzing system performance. However, if left unchecked, log files can consume valuable disk space over time. This is where Logrotate, a powerful log rotation tool, comes into play. In this comprehensive guide, we will walk you through the process of configuring Logrotate in Ubuntu, ensuring efficient log file management and optimal system performance.

Logrotate and Its Benefits

Logrotate is a utility designed to simplify log file management by automating the process of rotation, compression, and removal of log files. It helps prevent log files from growing indefinitely, which can lead to disk space issues. Additionally, Logrotate ensures that older logs are compressed or deleted, optimizing disk usage and facilitating easy log analysis.

Benefits of using Logrotate include:

a. Efficient disk space utilization: By rotating logs at regular intervals, Logrotate prevents log files from filling up disk space, ensuring sufficient storage for other system processes.

b. Easy log file management: Logrotate simplifies log file management by automating the process of compression, removal, and archiving, reducing the manual effort required.

c. Improved system performance: Large log files can impact system performance. Logrotate keeps log files within manageable sizes, allowing for faster log analysis and system maintenance.

Installing Logrotate in Ubuntu

Before we dive into configuring Logrotate, let’s make sure it is installed on your Ubuntu system. To install Logrotate, follow these steps:

Open the terminal and run the following command to install Logrotate:

sudo apt-get install logrotate

Configure Logrotate in Ubuntu

Now that Logrotate is installed, we can proceed with the configuration process. This involves creating a Logrotate configuration file, defining rotation parameters, specifying log file locations, setting rotation frequency, and customizing log rotation actions.

Creating a Logrotate Configuration File

The Logrotate configuration file is where you define how Logrotate should handle log files. Here’s how to create a new configuration file:

Open the terminal.

Navigate to the Logrotate configuration directory using the following command:

cd /etc/logrotate.d/

Create a new configuration file using a text editor:

sudo nano logrotate.conf

In the configuration file, you can define log rotation rules for individual log files. Each rule follows a specific format, consisting of the log file path, rotation parameters, and actions.

Defining Log Rotation Parameters

Log rotation parameters determine how Logrotate behaves when rotating log files. Some commonly used parameters include:

rotate: Specifies the number of rotated log files to keep.
size: Defines the maximum file size before rotation occurs.
compress: Enables compression of rotated log files.
dateext: Appends the date of rotation to the log file name.
postrotate: Specifies the actions to be taken after log rotation.

Logrotate Linux Examples

Rotating a Log File Based on Size:

/var/log/application.log {
    rotate 5
    size 100M
    compress
    dateext
    postrotate
        /bin/systemctl restart application
    endscript
}

Explanation: This rule rotates the “application.log” file when it reaches 100MB in size, keeping a maximum of 5 rotated copies. The rotated files are compressed, and the date of rotation is appended to their names. After rotation, the “application” service is restarted.

Rotating Multiple Log Files:

/var/log/application/*.log {
    rotate 10
    weekly
    compress
    dateext
    postrotate
        /bin/systemctl restart application
    endscript
}

Explanation: This rule rotates all the log files with the “.log” extension in the “/var/log/application/” directory. It keeps a maximum of 10 rotated copies, performs rotation on a weekly basis, compresses the rotated files, and appends the rotation date to their names. After rotation, the “application” service is restarted.

Rotating Logs with Date-Stamped Names:

/var/log/application/*.log {
    rotate 12
    monthly
    dateext
    dateformat -%Y-%m-%d
    missingok
    notifempty
    create 0644 root root
    postrotate
        /usr/sbin/service application restart >/dev/null 2>&1 || true
    endscript
}

Explanation: This rule rotates all log files with the “.log” extension in the “/var/log/application/” directory. It keeps a maximum of 12 rotated copies, performs rotation on a monthly basis, and appends the date of rotation to the log file name. The “dateformat” directive specifies the date format to be used in the rotated file names. The “missingok” directive allows Logrotate to skip rotation if the log file is missing. The “notifempty” directive ensures that empty log files are not rotated. A new log file with permissions 0644, owned by the “root” user and group, is created after rotation. After log rotation, the “application” service is restarted using the service command.

These are just a few examples of Logrotate configuration rules. You can customize them based on your specific log file locations, rotation requirements, and post-rotation actions. Make sure to adjust the paths and commands according to your system setup.

Remember to save the logrotate.conf file after making any changes, and always test the configuration using the logrotate -d /etc/logrotate.conf command before relying on it for automated log rotation.

Conclusion

In conclusion, this comprehensive guide has provided you with a step-by-step approach on how to configure Logrotate in Ubuntu. By following the instructions outlined here, you can effectively manage log files and ensure optimal system performance. Implementing Logrotate is essential for maintaining disk space, automating log rotation, and simplifying log file management.

Throughout this guide, we have covered the necessary steps for configuring Logrotate in Ubuntu. From installing Logrotate to creating a Logrotate configuration file, defining rotation parameters, specifying log file locations, setting rotation frequency, and customizing log rotation actions, you have learned the key aspects of Logrotate configuration.

   

If you like what you are reading, please:

Buy me a coffeeBuy me a coffee

arstech

Start the discussion at forum.arstech.net