How To install Logrotate to Manage Logfiles in Ubuntu Linux

Learn how to install and Logrotate on Linux Ubuntu 22.04 Jammy JellyFish or Ubuntu 20.04 Focal Fossa to manage log files more efficiently.

“Logrotate” is a tool to manage log files. Log files, if no attention is paid, they get bigger and bigger and end up occupying the space total available disk space. Furthermore, searching many/big Log files is time-consuming. To prevent this and disk space to save, “Logrotate” has been developed.

With “Logrotate” you can log files from a certain size (e.g. 1 MByte) and/or a specific age (e.g. 1 day, 1 week, 1 month, 1 year). to let. By “rotate” it is meant that the current log file and previous Versions of it are renamed/moved and possibly compressed in the process. The current log file is emptied. Earlier versions of the log file are included numbered and possibly also deleted as soon as they reach a certain age or age reach a certain number.

Most of the services come with their log rotation configuration that automatically tells the Logrotate what to do with the old log files. Furthermore, system administrators can use it to manage the logs for their scripts.

Steps to install Logrotate on Ubuntu Linux

In this tutorial, we will learn how the service of Logrotate works for the log files generated by different applications running on your Ubuntu Linux. Well, this guide can be followed for other Linux systems such as Debian, Linux Mint, POP OS, and more…

1. Update your Ubuntu system

To install applications on Debian-based operating systems including Ubuntu, we use APT package manager, however, to make sure it can recognize the latest available packages through the available repository and also our system has newer security updates, run the system update command periodically.

sudo apt update

2. Install Logrotate on Ubuntu 22.04 or 20.04

The next step is to download and install the packages of Logrotate on Ubuntu 22.04 or 20.04 using the APT. The best part is we don’t need to add any third-party repository because this command line utility tool is available through the default system repository of Ubuntu.

Note: In most cases, Logrotate will already be installed on your system.

sudo apt install logrotate

3. Check its Version

To confirm Logrotate is installed or already on your system simply type its name in your terminal. This will not only print its version details but also show the available options to use with it.

logrotate
Logrotate command usage

4. Logrotate Configuration file and directory

Logrotate is usually started periodically via “cron” (e.g. once a day) and controlled via the configuration file  /etc/logrotate.conf.

In this file GLOBAL settings are stored. Usually, this file also contains an entry that tells “logrotate” to apply its configured settings to all configuration files available in the Logrotate configuration directory /etc/logrotate.d/.

/etc/logrotate.d/ is the directory where by default applications on your system create configuration files for Logrotate. For example Apache, MySQL, and more…

Logrotate Configuration file and directory

The configuration files contain directory and file paths (also via shell pattern “*”, “?” and “[…]”) that are to be rotated. Either apply for them the global definitions (from “etc/logrotate.conf“) or LOCAL definitions given to them. Lay the definitions define how the log rotation of the files should take place.

For example, we have an Apache Logroate configuration file in the /etc/logrotate.d/let’s see its content to know the configuration parameters used in it.

cat /etc/logrotate.d/apache2

Output:

var/log/apache2/*.log {
    daily
    missingok
    rotate 14
    compress
    delaycompress
    notifempty
    create 640 root adm
    sharedscripts
    prerotate
	if [ -d /etc/logrotate.d/httpd-prerotate ]; then
	    run-parts /etc/logrotate.d/httpd-prerotate
	fi
    endscript
    postrotate
	if pgrep -f ^/usr/sbin/apache2 > /dev/null; then
	    invoke-rc.d apache2 reload 2>&1 | logger -t apache2.logrotate
	fi
    endscript
}
Apache Logroate Ubuntu Linux configuration file

Possible GLOBAL and LOCAL configuration parameters are:

  +---------------------+------------------------------------------------------+
  | directive | meaning |
  +---------------------+------------------------------------------------------+
  | include FILE/DIR | Specified configuration file/directory read |
  | | (Dir. --> Read ALL files in it) |
  +---------------------+------------------------------------------------------+
  | daily | Daily rotation of the log files. |
  | weekly | Weekly rotation of log files |
  | monthly | Monthly rotation of log files |
  | yearly | Annual rotation of log files |
  +---------------------+------------------------------------------------------+
  | size SIZE | Rotate log file if bigger (byte, k=kilo, M=mega) |
  | maxage DAYS | Delete rotated log files after number of days |
  | rotate CNT | Unclog Max CNT Rotated Logs |
  +---------------------+------------------------------------------------------+
  | missingok no | Missing log file is ignored (STD) |
  | if empty not | Also rotate empty log files (STD) |
  | copytruncate no | Truncate log file after copying (STD: Original |
  | | renamed + new created). If application does not |
  | | can be forced to close log file. |
  | | (Data loss possible, "create" has no effect) |
  +---------------------+------------------------------------------------------+
  | compress | Pack rotated log files |
  | delaycompress | Do not compress last rotated version immediately |
  | | (only in the next but one rotation cycle) |
  | compressext EXT | File name extension compr. Files (STD: .gz) |
  | compresscmd cmd | Compression command (STD: gzip) |
  | uncompresscmd cmd | decompression command (STD: gunzip) |
  | compressoptions OPT | Compression Command Options (STD:LEER) |
  +---------------------+------------------------------------------------------+
  | create MODE USR GRP | Rights + owner (group) red. set files |
  | datetext | Append date stamp instead of sequential number |
  | extension EXT | Filename extension of rotated files (STD: ?) |
  | | (possibly a compression extension is attached) |
  | olddir DIR | Rot. Store log files in DIR (identical disk) |
  +---------------------+------------------------------------------------------+
  | mail ADDRESS | Email the log file to be deleted beforehand |
  | mailfirst | Newly created red. Email log file |
  | maillast | Red to delete. Mail log file (STD) |
  +---------------------+----+-------------------------------------------------+
  | postrotate ... endscript | Commands ... Execute AFTER log file rotation |
  | prerotate ... endscript | Commands ... Execute BEFORE log file rotation |
  | sharedscript | Execute script ONLY 1x for ALL log files |
  +---------------------+----+-------------------------------------------------+
  | nocompress | DO NOT compress rotated log files |
  | nocopytruncate | DO NOT truncate log file after copy |
  | nocreate | Do not create a new log file after moving |
  | nodelaycompress | Compress log file immediately |
  | nomail | Do not send log file as email |
  | nomissingok | Missing log file generates error message (STD) |
  | noolddir | Place rotated log files in the same directory |
  | nosharedscripts     | "pre/postrotate"-Skripte PRO Logdatei (STD)          |
  | notifempty | Do not rotate empty log file |
  | missingok  | If the log file is missing, go on to the next one
  |            | without issuing an error message.
  +---------------------+------------------------------------------------------+

NOTE: The "postrotate", "prerotate" and "endscript" directives must be on
a line stand alone, the lines in between must be valid
contain command lines.

5. How to check the last time the log files are rotated?

With the first “logrotate” call, this file is created and all edited log files get the current date as the last processing time. After that, log files are no longer rotated if “logrotate” is called again on the same day. We can check the date when the last time a log file of some service had been rotated using the Logrotae status file “/var/lib/logrotate/status“.

sudo cat /var/lib/logrotate/status

Here is an excerpt from the Status file. In the screenshot, you can see the rotation of the log file along with a date.

Log file last time rotation date linux Ubuntu

You can forcefully rotate a log file for some service if you need it using the -f option. For example:

sudo logrotate -f path-to-log-configuration-file

Let’s say you want to forcefully rotate the log file of the unattended-upgrades service of Ubuntu then the command will be:

sudo logrotate -f /etc/logrotate.d/unattended-upgrades

This will force a re-rotation for the particular defined service on the same day.

To check the Log file status for a single services:

To get details of some particular service’s Log rotation done by using its Logrotate confirmation file, we can use the state-file parameter along with -v -s options.

logrotate -vs state-file path-to-config-file

For example

logrotate -vs state-file /etc/logrotate.d/apache2

6. How to create a Logrotate configuration for your own services

Let’s say you have created some service or installed any app that didn’t create a configuration file for Logrotate then you can do that manually.

For example, you have a service named – myservice that generates a log file name called “my.log” in a directory lets’ say /var/log/myservice/my.log. Now to manage it using Logrotate we need to create a configuration file for this that will be saved in /etc/logrotate.d/ the directory.

sudo nano /etc/logrotate.d/myservice

Add the following code:

var/log/myservice/my.log {
        rotate 4
        daily
        compress
        delaycompress
        missingok
        notifempty
        create 660 h2s h2s }

To save the file use Ctrl+O, hit the Enter key, and then exit using Ctrl+X.

In the above lines, var/log/myservice/my.log is the path that tells the Logrotate where the Log file for the service is situated. Whereas the parameters inside the brackets {} tell what to do with the log file. For instance, in the above-given example rotate 4 will tell the Logrotate to create 4 archives maximum owned by a user named h2s and group h2s with 660 permission. In the same way daily means the configuration file will run daily to rotate the log file, compress to reduce the size by compressing the file; delaycompress implies – do not compress the last rotated version immediately, notifempty tells the system not to rotate if the log file is empty; missingok means – If the log file is missing, go on to the next one without issuing an error message.

Once you have created the Logrotate configuration file for your service, copy it to the Logrotate configuration directory so that it can be readable by this log management tool and change its permission as well.

cp myservice /etc/logrotate.d/
chmod 644 /etc/logrotate.d/myservice
chown root.root /etc/logrotate.d/myservice

7. Logrotate Command on Ubuntu Linux

In the future, if you are facing any problem with any configuration file even the one you have created then there are some options to use. That would prove helpful.

1. Forcefully rotate Log files if necessary

We already have discussed this option above, it will be helpful, if you want to manually rotate some log files.

sudo logrotate -f path-to-log-configuration-file

example:

sudo logrotate -f etc/logrotate.d/myservice

2.  Debug

Without changing anything in your Logrotate configuration file if you want to test it for any potential error, use debug option.

logrotate -d /etc/logrotate.d/myservice

3.  -v –verbose | Generate more output

Verbose is used to provide more information about the command you are using with Logrotate. For example, if you are rotating some logs manually, then can use the -v option to know what exactly is happening (process) which is otherwise hidden in the background only.

logrotate -f -v /etc/logrotate.d/myservice

This was a quick introduction to the Logrotate command tool that is available on our Ubuntu systems to manage logs efficiently.  We have also learned here how to use it for our custom services. The source code of this tool is available on GitHub while those who want to learn more about its command can check out the man page. For that in your terminal run:

man logrotate

FAQ

What is Linux logrotate?

“Logrotate” is a tool to manage log files, it is open source and most of the time comes by default installed on Linux operating systems. og files, if no attention is paid, they get bigger and bigger and end up occupying the space total available disk space. Furthermore, searching many/big Log files is time-consuming. To prevent this and disk space to save, “Logrotate” has been developed.

What is logrotate used for?

Using logrotate system administrators can manage the logs generated by different applications easily. With “Logrotate” you can log files from a certain size (e.g. 1 MByte) and/or a specific age (e.g. 1 day, 1 week, 1 month, 1 year). to let. By “rotate” it is meant that the current log file and previous Versions of it are renamed/moved and possibly compressed in the process. The current log file is emptied. Earlier versions of the log file are included numbered and possibly also deleted as soon as they reach a certain age or age reach a certain number.

What is rotation in logrotate?

By “logrotate rotate” it is meant comperssing of the current log file and remaining or moving and possibly deleting the older ones as soon as they reach a certain age or age reach a certain number.

Does logrotate create new file?

A new empty log files are being created by the Logrotate process after the current one is rotated.

How do I check logrotate configuration?

To check the newly create logrotate configuration file or any existing one, we can use the Debug option. Here is the command syntax for that:

logrotate -d path-to-conifguration-file

For example:

logrotate -d /etc/logrotate.d/myservice

What is Notifempty in logrotate?

Notifempty parameter in Logrotate configuration file means or tells the system do not rotate the Log files, if they are empty.

What is Delaycompress in logrotate?

Delaycompress is also a parameter to use in logrotate configuration file. It tell the Logrotate that do not compress last rotated version immediately

Other Articles:

Commands to Remove a package in Ubuntu
Linux Zip Commands to Archive or Unarchive files
Common Linux Shell commands to start using terminal
List of Commands to get Linux system info using terminal
48 Basic Linux commands and questions

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.