Ubuntu

Logrotate Ubuntu Tutorial

Logrotate Ubuntu Tutorial

How to use Logrotate on Ubuntu

Logrotate is a a system utility tool that is used to manage log files on Ubuntu. When a large number of servers are handled by any system, then a large volume of log files are generated which consumes huge disk space. Logrotate is used to rotate, compress or remove log files automatically to save the disk space. Some functions of Logrotate are mentioned here:

In this tutorial, you will learn how to use this tool on Ubuntu 17.10.

Checking Logrotate Version

Logrotate is installed on Ubuntu by default. Run the following command to check it is installed or not. Here, the version of Logrotate is 3.11.0.

$ logrotate

Logrotate Configuration Files

The configuration information of Logrotate is stored in two places. One of them is the main configuration file which is located in /etc/logrotate.conf. It contains default settings and uses include statement to retrieve configuration information from another location.  Another place is a directory which contains all service and application configuration files and located in /etc/logrotate.d. When any new package installs in the system then the log rotation information for that package is stored in this location.

Open logrotate.conf from the terminal.

$ nano /etc/logrotate.conf

The following content will be shown. You can rotate log files hourly, daily, weekly, monthly and yearly. Here, weekly means that log files will be configured weekly. The next line indicates that log files are owned by root and syslog group users. Rotate 4 indicates that Logrotate will keep 4 weeks of log files backup and empty log files will be created after rotating old log files. If you set rotate 0 then all old log files will be removed.  If you want to compress log files then just uncomment the line of compress by removing hash symbol.

If you open the directory of /etc/logrotate.d then the following list will appear which contains others logrotate configuration files.

$ ls /etc/logrotate.d

Run the command to open apache2 configuration file. There are many types of rules which are used for configuration settings. Some of them are explained here. missingok indicates that no error message will be written if log file is missing and notifyempty indicates that if log file is empty then it will not rotate. Create 644 root root is used create log files immediately after the rotation, as root user and user group root with specific permission mode.

$ nano /etc/logrotate.d/apache2

Creating a sample configuration file

Suppose you want to create a configuration file in the location  /home/ubuntu/logrotate.conf where the settings for logrotate will be set as: log files will rotate monthly, rotate for 10 times, compress, omit error message if any log file is missing and create log file after removing old ones. Open any editor, add the following settings options and save the file. Here, ubuntu is logged in user's name.

/home/Ubuntu/logs/*.log
monthly
missingok
rotate 10
compress
create

Run the following command to create a log file named mylog.log and initially log file is empty.

$ nano mylog.log

Run logrotate command to check the log entries are created or not.

$ logrotate /home/ubuntu/logrotate.conf --state /home/ubuntu/logrotate-state --verbose

The following output will appear after executing the above command for the first time.

Now, open logrotate-state file to examine which is added in the file. Since no log files are generated yet, so the following output will appear.

$ nano home/ubuntu/logrotate-state

Run the following command to rotate log file forcefully. But no change will appear here according to the previous output because rotation interval is set as monthly and the time period is not exceeded here.

$ logrotate /home/ubuntu/logrotate.conf --state /home/ubuntu/logrotate-state
--verbose --force

Many other logrotate options are available to configure log files. You will get the list of logrotate options after executing the following command:

$ man logrotate

Some others mostly used logrotate options are, size, copytruncate, postrotate, maxage and compresscmd. These options are shortly explained here.

size:

This option is used to set the limit of log file size and logrotate will rotate log files when the file size reaches or exceed the limit.

copytruncate:

This option is used to create a copy of the original log files and truncate the original log file size to 0. So that the service related to that particular log file can write log properly in the original file.

postrotate:

This option is used to run custom shell script after completing log file rotation.

maxage:

This option is used to set time duration for storing old log file information. It sets the maximum values in days to keep log files and all log files will be removed when the maxage values exceeds.

compresscmd:

You can compress log files by using different commands. This option is used to specify the type of compression command.

After reading this tutorial, you will be able to use Logrotate with different options for managing your log files properly.

More Information

Logrotate Man Page

OpenTTD Tutorial
OpenTTD is one of the most popular business simulation games out there. In this game, you need to create a wonderful transportation business. However,...
SuperTuxKart for Linux
SuperTuxKart is a great title designed to bring you the Mario Kart experience free of charge on your Linux system. It is pretty challenging and fun to...
Battle for Wesnoth Tutorial
The Battle for Wesnoth is one of the most popular open source strategy games that you can play at this time. Not only has this game been in developmen...