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:
- It activates when log file size becomes large and reaches to a particular limit.
- It creates new log file after rotating old files.
- It compresses log files.
- It executes shell script after log rotation.
- It removes older rotated log files to save disk space.
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 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.
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.
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.
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.
Run the following command to create a log file named mylog.log and initially log file is empty.
Run logrotate command to check the log entries are created or not.
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.
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.
Many other logrotate options are available to configure log files. You will get the list of logrotate options after executing the following command:
Some others mostly used logrotate options are, size, copytruncate, postrotate, maxage and compresscmd. These options are shortly explained here.
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.
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.
This option is used to run custom shell script after completing log file rotation.
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.
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.