How to Use Logrotate to Manage Logs on Ubuntu

How to Use Logrotate to Manage Logs on Ubuntu

We’ll show you, How to Use Logrotate to Manage Logs on Ubuntu. Logrotate is a tool used by the system administrators to ease the management of large log files generated by the system services. The most important features of logrotate are automatic log rotation, log compression, log removal and mailing the log files. In this tutorial, we are going to show you how to use logrotate to manage the log files generated by the services running on your Linux VPS.

1. Connect via SSH and check if Logrotate is installed

Before we start, make sure that logrotate is installed on your server. Connect to your server via SSH and check if logrotate is installed by using the following command:

sudo dpkg -l | grep logrotate

2. Resynchronize the package index and upgrade all packages

If it is not installed on your Ubuntu VPS, we will help you to install it. First, resynchronize the package index and upgrade all packages currently installed on the system to the newest version using the commands below:

sudo apt-get update
sudo apt-get upgrade

3. Install Logrotate

Once the package manager completes the upgrade, go ahead and install logrotate:

sudo apt-get install logrotate

4. Verify the installation

Verify that the installation was successful:

sudo logrotate

You should get a message like the following:

logrotate 3.8.7 - Copyright (C) 1995-2001 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU Public License

Usage: logrotate [-dfv?] [-d|--debug] [-f|--force] [-m|--mail=command] [-s|--state=statefile] [-v|--verbose] [--version] [-?|--help] [--usage] [OPTION...] 

The main configuration file for logrotate is /etc/logrotate.conf while application specific configuration files are stored in the /etc/logrotate.d directory.

5. Configuration Options

Depending on what services are installed on your Ubuntu VPS, the /etc/logrotate.d directory may contain more or less configuration files. If you haven’t checked already, now is time to list the content of that directory and get familiar with the services for which logrotate is configured to work. As an example, we will use the /etc/logrotate.d/dpkg file to show you some of the most important configuration options. Open the file using a text editor:

sudo nano /etc/logrotate.d/dpkg

The content of the configuration file should be similar to the one below:

/var/log/dpkg.log {
        monthly
        rotate 12
        compress
        delaycompress
        missingok
        notifempty
        create 644 root root
}
/var/log/alternatives.log {
        monthly
        rotate 12
        compress
        delaycompress
        missingok
        notifempty
        create 644 root root
}

Logrotate checks /var/log/dpkg.log and /var/log/alternatives.log on a monthly basis. The rotation time is specified with the monthly option. Other rotation options are daily, hourly, weekly and yearly.

rotate tells how many times the log files should be rotated before being removed or mailed to a specific address. In this example, the log files will be rotated 12 times. In a case when rotate is set to 0, old log files are removed rather than rotated.

The next option is compress which specifies that the old version of the log files should be compressed. Alternative option is nocompress and in can be used in cases when the old versions of the log files should not be compressed.

delaycompress specifies that the compression of the previous log file should be postponed to the next rotation cycle.

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS

missingok and notifempty are used to skip the rotation without issuing an error message if the log file is missing and to skip the rotation if the log file is empty.

create 644 root root is used to create a log file immediately after the rotation using specific mode, owner and group.

Another option which is not mentioned in this example and you may find useful in your custom configurations is size. This option allows you to configure logrotate to rotate the log files only if they get bigger than a specific size. The size can be specified in bytes, kilobytes, megabytes and gigabytes. Therefore, size 100, size 100k, size 100M and size 100G are all valid.

For more datails and configuration options you can check the logrotate man page.

man logrotate

By default, logrotate is run as a daily cron job. For details about the cron job you can check /etc/cron.daily/logrotate. You can freely set up the cron job for logrotate to run hourly, weekly, yearly or at a specific time and/or day.

Of course, you don’t have to Use Logrotate to Manage Logs on Ubuntu, if you use one of our Managed Ubuntu Hosting Services, in which case you can simply ask our expert Linux admins to configure logrotate for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post. on Use Logrotate to Manage Logs on Ubuntu,  please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Leave a Comment