One of the most interesting and important things about the Linux system is var/log. All the activities of running services on your system are written into a file inside this directory. These files are called logs, through which you can examine how your system is operating. By using these logs, administrators can troubleshoot the system. If these log files kept on your system forever, they would finally reside the whole space on the filesystem. To prevent these problems, we need a tool for the management of all log files. So, administrators can use a ‘logrotate’ tool that cleans up the logs files on some periodical basis.Logrotate is a Linux based command-line tool used to manage the log files which are made by system processes. It automatically removes the old logs and compresses the logs into the more convenient logs form to conserve your system resources. Using this tool, users have full control over when and how log rotation is processed.In this article, you will see by using the Logrotate tool how you can manage log files on your Linux system. We have executed all steps on the terminal application on Ubuntu 20.04 system here. You should have ‘sudo’ command privileges. Launch the command line ‘Terminal’ application by pressing the ‘Ctrl+Alt+t’ keys and start the implementation.
Install Logrotate on Ubuntu 20.4
On Ubuntu 20.04 the Logrotate utility is preinstalled, but if you don’t have this utility on your system, then update the system apt repository, and using the following command, you can install the Logrotate utility:
By using the following command, you can list all your system log files on the terminal:
Understanding about the Configuration file (logrotate.conf)
All the configurations of the logrotate utility are placed in the logrotate.conf file that you can find in /etc/logrotate.conf directory. If you open the ‘logrotate.conf’ file in your favorite editor, then you will see the following content of the configuration file:
All the configuration settings for Logrotate files are placed inside the /etc/logrotate.d directory.
Logrotate Options
Logrotate utility provides several directives that help us to configure logs, how they will be rotated, and what should be done right afterward. For example, open the syslog file in the editor by using the following command:
Once you access its content, you will see the following options at the top of this file, which is highlighted in the following snapshot:
- Rotate represents that this tool should be kept on how many logs files.
- Daily represents that the tool will rotate logs daily. There are also other possible terms you can see here, such as weekly or monthly.
- In the above image, missingok represents the logrotate to skip rotate, and if the log file is unfound, it will not give an error.
- The notifempty represents that if the log file is empty, then skip to rotate. If an empty directive is found, then it will force to rotate all empty log files.
- Compress and delaycompress means old logs should be compressed with gzip, except for the most recent log file.
Example # 01
Let’s take an example. We would like to run a log named ‘syslog’ with the help of the logrotate utility. It will read the rsyslog configuration file. In the example below, we are handling two logs using the logrotate tool. One is ‘syslog’ that will run for 7 rotations after 1 day, and the other one is ‘mail.info’ that will rotate on a weekly basis for 4 rotations. By using the following command, run the logs and analyze the output on the terminal:
Example # 02
Another example for logs rotation analysis is given below:
In the screenshot below, you can read the configuration file of ‘samba’. You can analyze the output of the logrotate tool handling 3 different logs here. It rotates on a weekly basis for 7 rotations.
Conclusion
This is a comprehensive article about the logrotate tool. We have seen in detail how to use this utility on Ubuntu 20.04 system. Moreover, we have elaborated on how to read configuration files with the help of logrotate. From the above-mentioned details, I hope you get a clearer idea about the logrotate tool options. However, if you have any problem, then let us know via comments.