File Management

How to Manage Log Files with Logrotate?

How to Manage Log Files with Logrotate?

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:

$ sudo apt update

$ sudo apt install logrotate


By using the following command, you can list all your system log files on the terminal:

$ ls /var/log

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.

Include /etc/logrotate.d

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:

$ vi /etc/logroate.d/syslog


Once you access its content, you will see the following options at the top of this file, which is highlighted in the following snapshot:

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:

$ logrotate -d /etc/logrotate.d/syslog


Example # 02

Another example for logs rotation analysis is given below:

$ logrotate -d /etc/logrotate.d/samba


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.

Gry Top 10 Games to Play on Ubuntu
Top 10 Games to Play on Ubuntu
Windows platform has been one of the dominating platforms for gaming because of the huge percentage of games that are developing today to natively sup...
Gry 5 najlepszych gier zręcznościowych dla systemu Linux
5 najlepszych gier zręcznościowych dla systemu Linux
W dzisiejszych czasach komputery to poważne maszyny używane do gier. Jeśli nie możesz uzyskać nowego wysokiego wyniku, będziesz wiedział, o co mi chod...
Gry Bitwa o Wesnoth 1.13.6 Wydanie rozwojowe
Bitwa o Wesnoth 1.13.6 Wydanie rozwojowe
Bitwa o Wesnoth 1.13.6 wydana w zeszłym miesiącu jest szóstą wersją rozwojową w 1.13.Seria x i zapewnia szereg ulepszeń, w szczególności w interfejsie...