12 C
Texas
Muhammad Nabeel
Network and System Administrator

How to Install and Configure logrotate on Linux

Logrotate utility is designed to used in system administration in which dated log files are archived. Servers which run large applications such as LAMP stacks, often log every activity and request in the face of bulky logs which may use high disk space on the server. Log rotation provides a way to limit the total size of the logs retained while still allowing analysis of recent events.

The primary configuration file for logrotate is /etc/logrotate.conf and it has the default settings. So, if you want additional application-specific configuration then you have to create a separate file for that application in /etc/logrotate.d directory.

In this article you will learn that how to configure logrotate in Linux

Step 1: Install logrotate on Linux

Install logrotate on Ubuntu and Debian

- Advertisement -
apt install logrotate 

Install logrotate on CentOS, RHEL and Fedora

yum install  -y logrotate


OR

dnf install  -y logrotate

Step 2: Configure logrotate

We will configure logrotate for Apache web server in CentOS server. Let’s say we want to rotate the logs of a service “apache” that is creating logfiles under /var/log/httpd directory. So we will create a file “apache” under /etc/logrotate.d directory and make appropriate configuration in this file to rorate all the logs of apache service.

Run following command and add below lines in that file.

vi /etc/logrotate.d/apache
/var/log/httpd/* {
daily
rotate 50
size 2M
compress
delaycompress
}


/var/log/httpd It means rotating all logs from httpd directory.
daily means rotate the logs on a daily basis. you can also use weekly or monthly.
rotate 50 It means last 50 rotated logs should be kept.
size=2M means log will not be rotated until it reaches 2MB. sets the minimum size for the logs rotation.
compress It compress the old log files to save disk space.
delaycompress so it means rotated logs with the exception of the most recent one should be compressed.

now save changes to file and exit

You can also set many other options, for details run following command.

man logrotate

By default, logrotate automatically configures a cron job scheduled to run daily. but we want also run our desired log rotation file, Tells logrotate to force the rotation.

logrotate -f /etc/logrotate.d/httpd

Now run below command to see that what will logrotate do reading /etc/logrotate.d/httpd file.

logrotate -d /etc/logrotate.d/httpd

That’s it, so you can use logrotate and configure it for your desired service logs like it do for Apache web server.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article