Log Management With Logrotate On Ubuntu

The performance of a computer or server environment is highly dependent on system memory and disk usage. If something is consuming more disk space then it will lead to system failure. Likewise, increasing log file sizes must be controlled to reduce that risk.

Logrotate was introduced as a system utility that rotates, compresses log files, and sends system logs by mail. Such log file management reduces disk space usage and prevents system failure.

In this article, we will discuss the process of installing and configuring logrotate on Ubuntu 20.04 LTS server.

Install Logrotate on Ubuntu Server 20.04

On Ubuntu, logrotate is installed by default but in case it is not already installed you can install it using the command as shown below.

$ sudo apt update 
$ sudo apt install logrotate

You can confirm the installation with the command as shown below.

$ logrotate --version

Logrotate configuration files

The configuration file for logrotate is generated by the logrotate daemon. There are two paths for configuration as shown below.

/etc/logrotate.conf

This is the configuration file normally created for the logrotate utility

/etc/logrotate.d/

It is the directory that contains the application-specific rotations. By default, /etc/logrotate.conf is used but for each application to have a different configuration you can set it on /etc/logrotate.d/.

Default Logrotate Configuration File

As you know that /etc/logrotate.conf is the default configuration file. Please check the configuration file as shown on the screenshot below.

Check the configuration file with the command as shown below.

$ cat /etc/logrotate.conf

Output:

logrotate.conf
Advertisement

Configuration for a specific application

On the screenshot above, there is a configuration like included /etc/logrotate.d which means profiles for specific applications can be set on this directory. Here, we will show the configuration for dpkg as shown below.

$ cd /etc/logrotate.d/
$ cat dpkg

pkg logrotate configuration

For details on each configuration line, check the points discussed below. These configurations will replace the default configuration of /etc/logrotate.conf for specific applications such as dpkg.

  • Monthly: Rotate once a month. You can replace with daily, weekly according to your requirement.
  • rotate 12: Twelve old log files will be kept. It replaces the default 4 on /etc/logrotate.conf
  • compression: the log files for this application will be compressed using gzip
  • delaycompress: compression of the previous log file to the next spin cycle is postponed because it is still used by some programs.
  • missok: Do not write any error message if the log file is missing
  • notifempty: Do not rotate the log file if it is empty
  • create 644 root root: The log file is created with permission 644, with user and group as root

Create a Logrotate configuration file

Let’s say you installed an application like nginx and its log file is generated on /var/log/nginx/ then you can setup the logrotate configuration file for this specific application with the command as shown below.

Navigate to the logrotate . directory

$ cd /etc/logorate.d/

Create a logrotate file with an editor

$ vim nginx

Nginx logrotate configuration

/var/log/nginx/*.log {
   daily
   missingok
   rotate 14
   compress
   delaycompress
   notifempty
   create 0640 www-data adm
   sharedscripts
   prerotate
   if [ -d /etc/logrotate.d/httpd-prerotate ]; then 
      run-parts /etc/logrotate.d/httpd-prerotate; 
   fi 
   endscript
   postrotate
   invoke-rc.d nginx rotate >/dev/null 2>&1
   endscript
}

In the above configuration we have set rotation for 14 so the old 14 log files will be kept and the log files are compressed using gzip. Another configuration used in the above file is almost explained in the logrotate configuration file section. You can run the newly created configuration with a sudo privileged user as shown below.

$ sudo logrotate -d /etc/logrotate.d/nginx

Logrotate is running

Here, the log files are executed as shown in the screenshot below.

log processing

Logrotate with cron

While installing the logrotate package, a crontab file is also created in the internal process /etc/cron.daily with the name logrotate. Check the screenshot as shown below for more details.

$ cat /etc/cron.daily/logrotate

Logrotate cronjob

Inference

In this article, you learned how to install the logrotate package and test the default and logrotate-specific application configuration files with the crontab implementation. Thank you!

Leave a Comment