How to Manage Log Files with Logrotate on Ubuntu 20.04

Manage log files with Logrotate on Ubuntu 20.04 and promisingly administer the rotation and compression of the log files that sit on your system. By doing so, you can effortlessly control the available disk space.

With Logrotate, you’re looking at a system utility that helps administer the automated rotation of log files. Apart from rotation, it helps in managing compression as well. In this article, I’ll uncover the best ways to install and start with the Logrotate configuration. Follow along and learn how to manage log files with Logrotate on Ubuntu 20.04

Prerequisites

To prepare your system and manage log files with Logrotate on Ubuntu, you’ll need access to the following:

  • Basic working knowledge of Linux command line.
  • Ubuntu 20.04 distribution.
  • Sudo access.

Install Logrotate on Ubuntu 20.04

Before you start managing log files on Ubuntu 20.04, you must get the Logrotate utility ready. At first, launch the Terminal using the Ctrl+Alt+T key combination and invoke the $ logrotate version to check if it is preinstalled. If not, proceed as follows.

As usual, get the system apt repository updated. Doing this is pretty straightforward. Launch the Terminal and run the following command:

$ sudo apt update

Once done, execute the install logrotate command in the following manner

$ sudo apt install logrotate
install logrotate

Verify the installation status by using the $ logrotate version command

How to Manage Log Files with Logrotate on Ubuntu 20.04

Now that you know how to install the Logrotate utility on your system, we can learn how to manage log files with Logrotate.

Listing the System Log Files

Once you’re done installing the Logrotate utility, your system is ready to list all your system log files right on the Terminal. Just run the command as follows:

$ ls /var/log
manage log files with logrotate on ubuntu 20.04

Understanding logrotate.conf

The logrotate.conf is a common thing that you’ll encounter while operating Logrotate. As you can understand from the .conf extension, it is a file that carries all the configurations of the Logrotate utility. The directory that defines logrotate.conf in /etc/logrotate.conf while the configuration settings sits inside the /etc/logrotate.d directory.

/etc/logrotate.conf: It is the most general configuration file with the default setup.

/etc/logrotate.d: it represents a directory that includes files for a certain application rotation.

Viewing Logrotate Configuration: Application Specific

As already discussed, the logrotate utility helps provide a number of directives that eventually configure logs. In other words, it will help you control the mode of the rotation alongside the actions required afterward. 

To understand better, invoke the following command and launch the syslog file in the editor:

$ vi /etc/logrotate.d/syslog
viewing logrotate configuration

or,

To view the /etc/logrotate.d directory that shelters application-specific logrotate configurations, you can pass the ls flag

$ ls /etc/logrotate.d/

Once you find yourself inside, you’ll find the options named rotate, daily, missingok, and more sitting at the top of this file. Here is what these try to convey:

setting up logrotate

Rotate: It usually represents the number of log files a user needs to keep this tool.

Daily: As the name suggests, Daily represents that the period for log rotation of the tool is set to daily. Other possible terms, including weekly or monthly, signify the respective rotation period.

missingok: The term missingok signifies that the logrotate is about to skip rotation. However, in case the log file is missing, the system will respond with an error.

notifempty: It represents a condition for the log to skip to rotation. Using this will ensure that the log file skips to the rotation part whenever the log file is empty. In simple words, when your system finds an empty directive, it will force rotation of all the void log files.

Finally, compress and delaycompress define the old logs that your system should compress with gzip.

Viewing Logrotate Configuration: General 

For viewing more general configuration file /etc/logrotate.conf. what we’ll need to do is get content of file /etc/logrotate.conf printed employing the cat utility in the following way:

$ cat /etc/rsyslog.conf
general way of viewing configuration

From the output, you can see the global configuration specifying the default setup as:

Weekly: It represents that the logs are rotated every week. You can specify another time interval; however, it is crucial to keep in mind that the logrotate daemon runs every day. For that matter of fact, if you’re willing to bring alterations and want the rotation to take place hourly, you’ll have to change the logrotate cron job interval.

su root adm: It ensures that the log rotation is performed with the root user and the admin group.

rotate 4: As you can clearly understand from the term, the log files are rotated 4 times before eventually getting removed. When you set the value to 0, the older versions get directly removed without even rotating. 

create: It helps in creating a new log file with the same name immediately after the rotation is concluded.

compress: Just like the one we’ve previously discussed, old log files will get compressed when it is turned on. 

include: It defines the directory for an application rotation and its corresponding configurations. 

Creating A Fictional Log File For Logrotate

To manage log files with Logrotate on Ubuntu 20.04, we first create a fictional application and a log file corresponding to it. Here is how you can do it

Use the mkdir command to build a new directory in /var/log mkdir /var/log/[app-name]

Once you’ve got the dedicated directory set, you’re ready to create the custom log file. Use the nano command in the following way:

nano /var/log/[app-name]/backup.log

Working with the Standard Logrotate Configuration

Here we’ll create and discuss the standard logrotate job for the application (the custom one we’ve created, for instance). Now, after we create and set up a new logrotate configuration file, the same will usually get executed every day alongside other standard system logrotate jobs. 

Start by crafting the created app as a root in directory /etc/logrotate.d. You can utilize your text editor for this.

sudo nano /etc/logrotate.d/[app-name]

Add the following code and set up the logrotate configuration for the custom. It is crucial to make sure you manage log files with Logrotate on Ubuntu 20.04 in the most promising manner

File /etc/logrotate.d/[app-name]
/var/log/[app-name]/*.log {
    daily
    missingok
    rotate 8
    compress
    notifempty
}

Next, test the new configuration by simply executing logrotate. Keep in mind; you’ll require sudo privileges. 

sudo logrotate /etc/logrotate.conf --debug
logrotate standard configuration

Creating Logrotate Configuration: System Independent

Although you now know how to work with standard logrotate configuration to manage log files with Logrotate on Ubuntu 20.04, it is critical to understand the mechanism of creating system-independent configurations.

If you wonder why, well, there are two reasons. First, the default scenario restricts the logrotate from executing rotation once per day, so you’ll need to have a system-independent configuration ready by your side to bring alterations. Also, the standard configuration cannot rotate logs as a non-root user.

Check the owner of with ls command:

ls -l /var/log/[app-name]/backup.log
logrotate ubuntu 20.04

Create a logrotate config file with your text editor:

nano /home/arbas/logrotate.conf

Add the following codes:

File /home/arbas/logrotate.conf
/var/log/[app-name]/*.log {
    hourly
    missingok
    rotate 7
    compress
    notifempty
}

Create a Custom log file in the Home Directory

Run the following command:

$ logrotate /home/arbas/logrotate.conf --state /home/arbas/custom-state
getting logrotate ready for ubuntu

After that, you can view the content utilizing cat command:

$ cat /home/arbas/custom-state
manage log files with logrotate on ubuntu 20.04

Now, you’re ready to launch the cron jobs configuration file. For that, invoke the command as follows:

$ crontab -e
crontab command linux

And with that, I’m done guiding you on how to manage log files with Logrotate on Ubuntu 20.04. In this article, I’ve discussed every aspect of Logrotate utility that will help you understand the operational overview in a better way.

If this guide helped you, please share it.

Leave a Reply
Related Posts