How To Set Up Logrotate on RedHat Linux

Article By jack



1. Introduction

Logrotate is a utility designed for administrators who manage servers producing a high volume of log files to help them save some disk space as well as to avoid a potential risk making a system unresponsive due to the lack of disk space.

Normally, a solution to avoid this kind of problem is to setup a separate partition or logical volume for a /var mount point. However, it may also be a viable solution to this problem especially if it is too late to move all logs under different partition. In this article we will talk about usage and configuration on RedHat / CentOS Linux server.

Set Up Logrotate on RedHat Linux

2. What is Logrotate

it provides an ability for a system administrator to systematically rotate and archive any log files produced by the system and thus reducing a operating system's disk space requirement. By default it's invoked once a day using a cron scheduler from location

# ls /etc/cron.daily/
cups  logrotate  makewhatis.cron  mlocate.cron  prelink  readahead.cron  rhsmd  tmpwatch

3. Configuring

Logrotate's configuration is done by editing two separate configuration files:

  • /etc/logrotate.conf
  • service specific configuration files stored in /etc/logrotate.d/.

The main .conf file contains a generic configuration. Here is a default configuration file logrotate.conf:

     1  weekly
     2  rotate 4
     3  create
     4  dateext
     5  include /etc/logrotate.d
     6  /var/log/wtmp {
     7      monthly
     8      create 0664 root utmp
     9          minsize 1M
    10      rotate 1
    11  }
  • Line 1 - weekly configuration option ensures a weekly rotation of all log-files defined in main configuration file and in /etc/logrotate.d/ directory.
  • Line 2 - rotate 4 ensures that it keeps a 4 weeks backup of all log files
  • Line 3 - create option instructs it to create new empty log files after each rotation
  • Line 4 - dateext appends an extension to all rotated log files in form of date when each particular log file was processed
  • Line 5 - include all other configuration from directory /etc/logrotate.d
  • Line 6 - 11 contains a specific service log rotate configuration

As opposed to logrotate.conf a directory /etc/logrotate.d/ contains a specific service configuration files used. In the next section we will create a sample skeleton configuration.

3.1. Including new service logs

In this section we will add new log file into a existing configuration. Let's say that we have a log file called:


sitting in our /var/log directory that needs to be rotated on daily basis. First we need to create a new configuration file to accommodate for our new log file:

$ vi /etc/logrotate.d/linuxcareer

Insert a following text into /etc/logrotate.d/linuxcareer:

/var/log/linuxcareer.log {
    size 20k
    create 0600 root root

Here is a line by line explanation of the above configura file:

TIP: If you wish to include multiple log files in a single configuration file use wildcard. For example /var/log/mylogs/*.log will instruct it to rotate all log files located in /var/log/mylogs/ with extension .log.

  • missingok - do not output error if logfile is missing
  • notifempty - donot rotate log file if it is empty
  • compress - Old versions of log files are compressed with gzip(1) by default
  • size - Log file is rotated only if it grow bigger than 20k
  • daily - ensures daily rotation
  • create - creates a new log file wit permissions 600 where owner and group is root user

The log rotate utility as quite versatile as it provides many more configuration options. Below, I will list few other configuration options for log rotate. To get a complete list, consult the man manual page:

$ man logrotate
  • copy - Make a copy of the log file, but don’t change the original at all.
  • mail - When a log is rotated out-of-existence, it is mailed to address.
  • olddir - Logs are moved into for rotation.
  • postrotate/endscript - The lines between postrotate and endscript are executed after the log file is rotated.

3.2. Testing a new configuration

Once you have created a new configuration file within /etc/logrotate.d:

# cat /etc/logrotate.d/linuxcareer 
/var/log/linuxcareer.log {
    size 20k
    create 0600 root root

create some sample log file ( if not existent ! ):

# echo "rotate my log file" > /var/log/linuxcareer.log

Once your log file is in place force log rotate to rotate all logs with -f option.

# logrotate -f /etc/logrotate.conf
Warning: The above command will rotate all your logs defined in /etc/logrotate.d directory.

Now visit again your /var/log/directory and confirm that your log file was rotated and new log file was created:

# cat /var/log/linuxcareer.log
rotate my log file
# logrotate -f /etc/logrotate.conf 
# cat /var/log/linuxcareer.log
file /var/log/linuxcareer.log-20130409.gz 
/var/log/linuxcareer.log-20130409.gz: gzip compressed data, from Unix, last modified: Tue Apr  9 12:43:50 2013
# zcat /var/log/linuxcareer.log-20130409.gz 
rotate my log file

As you can see the new empty log file linuxcareer.log was created and old linuxcareer.log file was compressed with gzip and renamed with date extension.

TIP: In order to see a content of your compressed log file you do not need to decompress it first. Use zcat or zless commands which will decompress your log file on fly.

4. Conclusion

As it was already mentioned previously, the best way to avoid your system being clogged by log files is to create a separate partition/logical volume for your /var/ or even better /var/log directory. However, even then log rotate can help you to save some disk space by compressing your log files. Log rotate may also help you to archive your log files for a future reference by creating an extra copy or by emailing you any newly rotated log files. For more information see man manual page:


Tags: , , , , , , , , , , , , , , ,

Spin Up A VPS Server In No Time Flat

Simple Setup

Full Root Access

Straightforward Pricing


Leave a Reply