Linux Commands

How To Set Logrotate on Linux

The Logrotate utility simplifies the process of administering log files. It relocates and replaces log files to manage their size and organize them while maintaining the information present inside them. For example, it will maintain seven log files to keep daily records for seven days.

While rotating the log files, Logrotate deletes irrelevant old logs, preventing them from consuming excessive disk space. It runs periodically in the background to keep your systems organized and clean. So, if you want to learn about Logrotate, this blog is for you. Here, we have included in-depth information about how to set Logrotate on Linux.

How To Set Logrotate on Linux

Although many Linux distributions have Logrotate as the pre-installed utility. However, if your system does not have Logrotate, please use the following command to install it:

sudo apt install logrotate

command-to-install-logrotate-in-linux

Now, let’s move to the configuration part. There are two kinds of logrotate configurations– global and system-specific. Open the ‘/etc/logrotate.conf’ file using a text editor. It is Logrotate’s primary configuration file, and any changes made to it will affect the whole system.

sudo nano /etc/logrotate.conf

information-in-the-logrotate-config-file

This file has three key sections:

  1. To specify the rotation frequency, i.e., the time it should rotate the logs. It is set to weekly by default, but you can change it to daily, weekly, or monthly.
  2. To define the number of rotated files it should keep, adjust the value based on how much historical data you want to retain. For instance, ‘rotate 4’ guides it to keep the latest four rotated log files and delete the earlier ones to free up disk space.
  3. The third is to specify the permissions and ownership of the new log files it’ll create.

You can tweak these settings according to what suits your system best. For instance, to maintain weekly records for one month(28 days), you must enter:

weekly
rotate 4
create 0644 root root

This way, it will rotate one file weekly and keep four such files. Further, it creates a new log file for currently occurring events while giving the root user and group the read-and-write permissions and read-only for others.

If you have to monitor a specific application’s logs for underlying issues. In that case, you can tailor log rotation settings for that application by creating its separate logrotate configuration file. Let’s take an example of conda. First, create its file using:

sudo nano /etc/logrotate.d/conda

In this file, add configurations specific to the conda logs:

/var/log/conda/*.log {
weekly
rotate 4
compress
delaycompress
missingok
notifempty
create 0644 root root
}

information-for-the-logrotate

Here, the compress command guides to compress the files so that resulting files take up less space. With the delaycompress command, you can hold the latest rotated file uncompressed to make it convenient for the users to refer to it.

The missingok option tells logrotate to ignore the absence of a log file and continue its operations without any error. At last, with notifempty, logrotate won’t rotate any empty log file. The logrotate should run automatically as per the default settings. However, you must confirm it using:

nano /etc/cron.daily/logrotate

A Quick Wrap-up

Knowing the configuration process of the logrotate utility is crucial for system administrators and is also essential for disk management in Linux devices. Hence, this blog explains the approaches used to set logrotate on Linux. You can modify configurations globally and simultaneously change them for specific applications. Moreover, system-specific configurations should be used responsibly because they always override global settings.

About the author

Prateek Jangid