How to Install and Configure logrotate on Linux

Windows Articles

How to update apps on Android

Hi folks! In this post, we will show you how to update your Android apps. Updating the apps on your device is...

How to install Apache Ant on Ubuntu 20.04?

Hello, friends. Apache Ant is a very important tool for the development of applications in Java. So in this post, we will...

How to use the ss command

For Linux users, there will come a time when you need to know about the network. Several tools help in this process,...

How to create user template in Windows Server 2019/2016

Hello! Windows Server has many features that support system administration. For example, through the users it is possible to manage many administrative...

Debian 10.6 available

Several volunteers have developed the free software distributed under the name of Debian. Debian does not sell its software directly, the developers...
Muhammad Nabeel
Muhammad Nabeel
Network and System Administrator

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

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.

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

How to update apps on Android

Hi folks! In this post, we will show you how to update your Android apps. Updating the apps on your device is...

How to install Apache Ant on Ubuntu 20.04?

Hello, friends. Apache Ant is a very important tool for the development of applications in Java. So in this post, we will...

How to use the ss command

For Linux users, there will come a time when you need to know about the network. Several tools help in this process,...

How to create user template in Windows Server 2019/2016

Hello! Windows Server has many features that support system administration. For example, through the users it is possible to manage many administrative...

Debian 10.6 available

Several volunteers have developed the free software distributed under the name of Debian. Debian does not sell its software directly, the developers...