Cyberithub

6 Easy Steps to Setup and Manage Log Rotation Using logrotate in Linux

Advertisements

In this article, I will take you through 6 Easy Steps to Setup and Manage Log Rotation Using logrotate in Linux (RHEL/CentOS 7/8). As you might be aware that Linux based Servers has a reserved location called /var/log for all the log files that applications and processes generates. So to make sure that log files does not just consume the entire disk space it is mandatory to have some kind of log management to delete old log files after it crosses some threshold size. This functionality can be easily implemented using log rotation feature.

Logrotate is the tool which can be used to setup log rotation feature for different applications based on log size and other important parameters. It can also compress the logs based on certain conditions and can also be used to delete old log files. General configuration can be put in /etc/logrotate.conf file but if needed some specific configuration for certain application logs, then it can be placed through .conf file or application file name under /etc/logrotate.d path.

6 Easy Steps to Setup and Manage Log Rotation Using logrotate in Linux

Easy Steps to Setup and Manage Log Rotation Using logrotate in Linux

Also Read: Migrate CentOS 8 to CentOS Stream 8 in 6 Easy Steps

Step 1: Prerequisites

a) You need to have a running RHEL/CentOS 7/8 System.

b) You should have yum tool installed in your Server. You can check Top 22 YUM command examples in RedHat/CentOS 7 to know more about yum command.

c) You need to have root or sudo access to run privileged commands. Please Check How to Add User to Sudoers to know more about providing sudo access to the User.

Step 2: Update Your Server

If you have not updated your Linux server recently then you need to first update your server using yum update -y command as shown below. Since in this case Linux server already has the latest updates installed so it does not find any new updates to install.

[root@localhost ~]# yum update -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.piconets.webwerks.in
* extras: mirrors.piconets.webwerks.in
* updates: mirrors.piconets.webwerks.in
No packages marked for update

Step 3: Install Logrotate in Linux

To install logrotate package from enabled Repository, you need to use yum install logrotate -y command as shown below. This will download and install logrotate package along with its dependencies.

[root@localhost ~]# yum install logrotate -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.piconets.webwerks.in
* extras: mirrors.piconets.webwerks.in
* updates: mirrors.piconets.webwerks.in
Resolving Dependencies
--> Running transaction check
---> Package logrotate.x86_64 0:3.8.6-19.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================================================
Package Arch Version Repository Size
========================================================================================================================================================================
Installing:
logrotate x86_64 3.8.6-19.el7 base 70 k

Transaction Summary
========================================================================================================================================================================
Install 1 Package

Total download size: 70 k
Installed size: 105 k
Downloading packages:
logrotate-3.8.6-19.el7.x86_64.rpm | 70 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : logrotate-3.8.6-19.el7.x86_64 1/1
Verifying : logrotate-3.8.6-19.el7.x86_64 1/1

Installed:
logrotate.x86_64 0:3.8.6-19.el7

Complete!

Step 4: Check Logrotate Version

You can check the current logrotate version by using logrotate --version command as shown below. Here the current version is 3.8.6

[root@localhost ~]# logrotate --version
logrotate 3.8.6

Step 5: Setup Logrotate in Linux

Log rotation configuration can be checked and set through /etc/logrotate.conf file as you can see below.

[root@localhost ~]# cat /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
monthly
create 0664 root utmp
minsize 1M
rotate 1
}

/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}

# system-specific logs may be also be configured here.

Let's take an example of Apache Server Logrotate file(/etc/logrotate.d/httpd) where we have set the log rotation schedule, number of times log file should rotate, size limit of the log file and other important parameters as shown below.

[root@localhost ~]# cat /etc/logrotate.d/httpd
/var/log/httpd/*log {
daily
rotate 3
size 5M
missingok
notifempty
sharedscripts
delaycompress
}

daily : log files are rotated every day. More on logrotate Man Page.

rotate : Log files are rotated count times before being removed or mailed to the address specified in a mail directive.

size : Log files are rotated only if they grow bigger then size bytes. More on logrotate Man Page.

missingok : If the log file is missing, go on to the next one without issuing an error message.

notifempty : Do not rotate the log if it is empty (this overrides the ifempty option). More on logrotate Man Page.

sharedscripts : this option makes scripts run only once, no matter how many logs match the wildcarded pattern, and whole pattern is passed to them. More on logrotate Man Page.

delaycompress : Postpone compression of the previous log file to the next rotation cycle. More on logrotate Man Page.

You can even test the logrotation feature for Apache logs using logrotate -d /etc/logrotate.d/httpd command as shown below.

[root@localhost ~]# logrotate -d /etc/logrotate.d/httpd
reading config file /etc/logrotate.d/httpd
Allocating hash table for state file, size 15360 B

Handling 1 logs

rotating pattern: /var/log/httpd/*log 5242880 bytes (3 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/httpd/access_log
log does not need rotating (log size is below the 'size' threshold)
considering log /var/log/httpd/error_log
log does not need rotating (log size is below the 'size' threshold)
not running postrotate script, since no logs were rotated

-d : Turns on debug mode and implies -v.

Step 6: Uninstall Logrotate in Linux

Once you are done with the logrotate package you can uninstall it by simply using yum remove logrotate -y command as shown below.

[root@localhost ~]# yum remove logrotate -y
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package logrotate.x86_64 0:3.8.6-19.el7 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================================================
Package Arch Version Repository Size
========================================================================================================================================================================
Removing:
logrotate x86_64 3.8.6-19.el7 @base 105 k

Transaction Summary
========================================================================================================================================================================
Remove 1 Package

Installed size: 105 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Erasing : logrotate-3.8.6-19.el7.x86_64 1/1
Verifying : logrotate-3.8.6-19.el7.x86_64 1/1

Removed:
logrotate.x86_64 0:3.8.6-19.el7

Complete!

 

 

 

Popular Recommendations:-

26 iostat, vmstat and mpstat command examples to Monitor Linux Performance

Practical Steps to Install iostat and mpstat command on Linux(RHEL/CentOS 7/8)

16 Fdisk command examples to Manage Disk Partitions in Linux

How to Convert/Change time to epoch time using date utility on Linux/Unix Server

How to Install jq(JSON Processor) on RHEL/CentOS 7/8 

7 Simple Steps to Install MTR(My Traceroute) on Linux(RHEL/CentOS 7/8)

Python3: ModuleNotFoundError: No Module Named "prettytable" in Linux 

How to List all the Installed Python Modules in Linux{2 Easy Methods}

Leave a Comment