The performance of a computer or server environment very much depends upon the system memory and disk usage. If something is consuming more disk space then it will lead to a system error. Likewise, increasing log file size must be controlled to reduce such risk.

Logrotate was introduced as a system utility that rotates, compresses the log files, and mails system logs. Such management of log files reduces disk space usage and prevents system errors.

In this article, we are going to discuss the installation process and the configuration of logrotate on Ubuntu 20.04 LTS server.

Installation of Logrotate on Ubuntu 20.04 server

On Ubuntu, logrotate is installed by default but in case it is not installed, you can install it with 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

The Logrotate configuration files

Configuration file for logrotate is created by logrotate daemon. There are two paths for such configuration as shown below.

/etc/logrotate.conf

It is the the configuration file generally created for the logrotate utility

/etc/logrotate.d/

It is the directory consisting of the specific rotation of the applications. By default, /etc/logrotate.conf is used but for each application to have different configuration, it can be set on /etc/logrotate.d/.

The Default Logrotate configuration file

As you are aware that /etc/logrotate.conf is the default configuration file. Let’s check the configuration file as shown on the screenshot below.

Check the config file with the command as shown below.

$ cat /etc/logrotate.conf

Output:

Managing logs with Logrotate on Ubuntu linux shell ubuntu

Configuration for a specific application

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

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

Managing logs with Logrotate on Ubuntu linux shell ubuntu

To get details of each line of configuration, check the points discussed below. These configurations will replace the default configuration of /etc/logrotate.conf for specific applications like dpkg.

  • monthly: Rotate once a month. You can replace it with daily, weekly as per your requirements.
  • rotate 12: Twelve old log files will be kept. It replaces the default 4 on /etc/logrotate.conf
  • compress: log files for this application will be compressed by using gzip
  • delaycompress: compression of the previous log file to the next rotation cycle is postponed as it is still used by some program.
  • missingok: Don’t write any error message if the log file is missing
  • notifempty: Don’t rotate the log file if it is empty
  • create 644 root root: Log file is created with permission 644, with user and group as root

Creating Logrotate configuration file

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

Navigate to the logrotate directory

$ cd /etc/logorate.d/

Create a logrotate file with editor

$ vim nginx

Managing logs with Logrotate on Ubuntu linux shell ubuntu

/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 the rotation for 14 so 14 old log files will be kept, and the log file is compressed with the use of 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 the sudo privilege user as shown below.

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

Managing logs with Logrotate on Ubuntu linux shell ubuntu

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

Managing logs with Logrotate on Ubuntu linux shell ubuntu

Logrotate with cron

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

$ cat /etc/cron.daily/logrotate

Managing logs with Logrotate on Ubuntu linux shell ubuntu

Conclusion

In this article, you have learned how to install the logrotate package and check the default and specific application configuration file for logrotate with the implementation of crontab. Thank you!