Introduction to Logrotate

The logrotate package allows automatic rotation, compression, removal, and mailing of log files.

This package is known to build and work properly using an LFS-8.2 platform.

Package Information

Logrotate Dependencies





An MTA (runtime)

User Notes: http://wiki.linuxfromscratch.org/blfs/wiki/logrotate

Installation of Logrotate

Install logrotate by running the following command:

sed -i '/exit 5/s/^/echo uncompress failed -- skipping #/' test/test &&

sed 's/-Werror//' Makefile.am &&
./autogen.sh                  &&
./configure --prefix=/usr     &&

To test the results, issue: make test.

Now, as the root user:

make install

Command Explanations

sed ... test/test: Prevents the tests from failing due to a problem in the test script.

ed 's/-Werror//' Makefile.am: Prevents the compiler stopping with an error when a warning is issued, since newer versions of GCC have introduced a lot more warnings, which are triggered by the code.

Configuring Logrotate

Logrotate needs a configuration file, which must be passed as an argument to the command when executed. Create the file as the root user:

cat > /etc/logrotate.conf << EOF
# Begin of /etc/logrotate.conf

# Rotate log files weekly

# Don't mail logs to anybody

# If the log file is empty, it will not be rotated

# Number of backups that will be kept
# This will keep the 2 newest backups only
rotate 2

# Create new empty files after rotating old ones
# This will create empty log files, with owner
# set to root, group set to sys, and permissions 644
create 0664 root sys

# Compress the backups with gzip

# No packages own lastlog or wtmp -- rotate them here
/var/log/wtmp {
    create 0664 root utmp
    rotate 1

/var/log/lastlog {
    rotate 1

# Some packages drop log rotation info in this directory
# so we include any file in it.
include /etc/logrotate.d

# End of /etc/logrotate.conf

chmod -v 0644 /etc/logrotate.conf

Now create the /etc/logrotate.d directory as the root user:

 mkdir -p /etc/logrotate.d

At this point additional log rotation commands can be entered, typically in the /etc/logrotate.d directory. For example:

cat > /etc/logrotate.d/sys.log << EOF
/var/log/sys.log {
   # If the log file is larger than 100kb, rotate it
   size   100k
   rotate 5
      /bin/killall -HUP syslogd

chmod -v 0644 /etc/logrotate.d/sys.log

You can designate multiple files in one entry:

cat > /etc/logrotate.d/example.log << EOF
file3 {

chmod -v 0644 /etc/logrotate.d/example.log

You can use in the same line the list of files: file1 file2 file3. See the logrotate man page or http://www.techrepublic.com/article/manage-linux-log-files-with-logrotate/ for more examples.

The command logrotate /etc/logrotate.conf can be run manually, however, the command should be run daily. Other useful commands are logrotate -d /etc/logrotate.conf for debugging purposes and logrotate -f /etc/logrotate.conf forcing the logrotate commands to be run immediately. Combining the previous options -df, you can debug the effect of the force command. When debugging, the command is only simulated, not really run, thus, eventual non-existing errors appear, when some intermediate files are expected, because they are not actually created.

To set up Fcron-3.2.0 to run logrotate ... at 3AM daily, root's crontab should be edited to add:

0 3 * * *   /usr/sbin/logrotate /etc/logrotate.conf


Installed Programs: logrotate
Installed Library: None
Installed Directories: None

Short Descriptions


performs the log maintenance functions defined in the configuration files.

Last updated on 2018-06-20 08:13:12 -0500