---
title: "How to Configure Logrotate to Manage Logfiles (Step by Step)"
description: "Learn how to effectively manage, control, and tame logfiles with Logrotate in Linux with this step-by-step tutorial!"
canonical: "https://adamtheautomator.com/logrotate-linux/"
---

# How to Configure Logrotate to Manage Logfiles (Step by Step)

> Learn how to effectively manage, control, and tame logfiles with Logrotate in Linux with this step-by-step tutorial!

Source: https://adamtheautomator.com/logrotate-linux/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Configure Logrotate to Manage Logfiles (Step by Step)](https://adamtheautomator.com/wp-content/uploads/2022/01/How-To-Manage-Logfiles-with-Logrotate-in-Linux.jpg)

# How to Configure Logrotate to Manage Logfiles (Step by Step)

[![](https://secure.gravatar.com/avatar/52e3f1a34a77644ff236f2487c6353f6778e552d3a690724a6afcc122951e714?s=192&d=mm&r=g)Nalty Lam](https://adamtheautomator.com/author/nalty-lam/)10 January 20226 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Command Line](/tag/command-line/)[Logging](/tag/logging/)[Ubuntu Linux](/tag/ubuntu-linux/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Viewing Logrotate Configuration Files for Packages](#viewing-logrotate-configuration-files-for-packages)
*   [Setting up Custom Rules to Rotate Logs](#setting-up-custom-rules-to-rotate-logs)
*   [Setting up Custom Rules Outside Logrotate Installation Directory](#setting-up-custom-rules-outside-logrotate-installation-directory)
*   [Conclusion](#conclusion)

Log files are an essential part of Linux system administration and monitoring. But log files also quickly grow large and take up a lot of storage space. The good news is that a tool called [Logrotate](https://github.com/logrotate/logrotate) in Linux lets you manage log files effectively.

In this tutorial, you’ll learn how to configure Logrotate so you can manage and prevent log files size from getting out of hand in your machine.

Ready? Dig in to get started!

## Prerequisites

This tutorial comprises step-by-step instructions. If you’d like to follow along, be sure you have the following in place:

*   An Ubuntu machine – This tutorial uses [Ubuntu](https://adamtheautomator.com/install-ubuntu/) 20.04 LTS, but other Linux distros will work.

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

*   A user account with [sudo](https://www.liquidweb.com/kb/how-to-set-up-and-manage-sudo-permissions/) privileges
*   [Logrotate](https://github.com/logrotate/logrotate#logrotate) – This tutorial uses Logrotate 3.14.0.

## Viewing Logrotate Configuration Files for Packages

Log rotation can be a bit confusing at first, but you’ll get the hang of it once you know what the various parameters mean. Logrotate uses configuration files to set rules for the rotation’s behavior, such as the maximum log files to keep, how often the rotation will run, and the log file’s size to compress.

Logrotate’s configuration files are located in _/etc/logrotate.conf_ file and _/etc/logrotate.d/_ directory:

*   The _/etc/logrotate.conf_ configuration file – Contains default options for log rotation but does not perform any actions by itself. You can use this configuration file to specify default parameters for all of your log rotations which does not exist in the _/etc/logrotate.d/_ directory.
*   The _/etc/logrotate.d/_ directory – This directory stores individual log rotation config files, such as alternatives, apt, syslog, udev, and so on. You can use these example config files as a starting point for creating your own log rotation rules.

Take a look at the Logrotate configuration file for the apt package so that you can understand the parameters in more detail.

Run the [`cat`](https://www.geeksforgeeks.org/cat-command-in-linux-with-examples/) command below to view the contents of Logrorate’s configuration file for the apt package (`/etc/logrotate.d/apt`).

```bash
sudo cat /etc/logrotate.d/apt
```

In the screenshot below, you can see two main sections, _/var/log/apt/term.log_ and _/var/log/apt/history.log._ Both sections have the same set of options that override the default Logrotate settings in the _/etc/logrotate.conf_ file.

![Previewing the /etc/logrotate.d/apt file with cat command](https://adamtheautomator.com/wp-content/uploads/2022/01/image-85.png)

Previewing the /etc/logrotate.d/apt file with cat command

> _This tutorial can’t cover all the options you can set for your log rotation file. But you can run the `sudo man logrotate` command to get the list of available options._

![Previewing the logrotate man page](https://adamtheautomator.com/wp-content/uploads/2022/01/image-86.png)

Previewing the logrotate man page

## Setting up Custom Rules to Rotate Logs

One of the great things about Logrotate is that it is customizable. You can set up your own rules to rotate your logs the way you want.

There are two ways to create custom Logrotate rules:

*   Add a config file to the /_etc/logrotate.d_ directory.
*   Create a script outside Logrotate’s installation directory to run Logrotate for you.

But for this example, you’ll create a custom config file in the _/etc/logrotate.d_ directory.

> _Creating Logrotate rules in the /etc/logrotate.d directory rotates your logs only daily, weekly, and monthly by default._

1\. Open the _/etc/logrotate.d/apache2.conf_ file in your preferred text editor.

> _From this point throughout the tutorial, you need `sudo` privileges to edit a configuration file’s content._

2\. Copy and paste the following code (logrotate rule) to the _apache2.conf_ file and save the changes.

The code below rotates logs every week. Based on the specified options, the rules keep the three most recent rotated logs and compress them on a disk if they are 10 megabytes in size or less.

> _When the options are not defined in /etc/logrotate.d/ directory, the default settings specified in /etc/logrotate.conf will be used._

```bash
/var/log/apache2/* {
# Sets the logs to rotate every week
weekly
# Tells the system to remove old logs and only keep the three most recent rotated logs
rotate 3
# Rotated logs will be compressed and kept on disk if they are 10 MB or less.
size 10M
# compress and delaycompress: These two options are used together and indicate that 
# rotated logs should be compressed (gzip) except for the most recent one.
compress

delaycompress
}
```

3\. Finally, run the `logrotate` command below to perform a `logrotate` dry run. This command turns on a dry run (`-d`) for the new rule you previously set (step two) in the `apache2.conf` file.

The dry run will simulate the rotation without actually rotating the logs. A dry run is a great way to test your new `logrotate` rule to ensure it’s working correctly.

```bash
sudo logrotate -d /etc/logrotate.d/apache2.conf
```

![Executing a dry run of the custom logrotate rule ](https://adamtheautomator.com/wp-content/uploads/2022/01/image-87.png)

Executing a dry run of the custom logrotate rule

You might get an error, like the one below, which is normal since you are testing the rules by doing a dry run on a fictional apache web server.

But at this point, your standard Logrotate job is set and will run once a week with your new custom rules.

![Getting an error on the dry run](https://adamtheautomator.com/wp-content/uploads/2022/01/image-88.png)

Getting an error on the dry run

## Setting up Custom Rules Outside Logrotate Installation Directory

You’ve just created your custom Logrotate rules in the _/etc/logrotate.d/_ directory. But perhaps you prefer to create the Logrotate rules in a different location. If so, for this demo, you’ll create custom rules in your home directory that rotate and compress logs files in a specific directory.

Unlike creating Logorate rules in the _/etc/logrotate.d_ directory, custom rules set up outside the Logrotate installation directory let you set a rule to rotate the logs hourly. Hourly logs help more with debugging your system than daily logs, which may not provide enough information.

In this example, you’ll run a fictional app that generates logs stored in _~/logs/_ directory.

1\. Run the following command in your home directory to create a directory named `logs`.

```bash
mkdir ~/logs
```

2\. Next, open your text editor and create a configuration file named _logrotate.conf._ Populate the _logrotate.conf_ file with the following contents.

The code below will rotate your logs every hour and compresses them, keeping 24 rotation logs. At the same time, the old logs are removed when the new logs are created in the _~/logs_ directory.

```bash
home/demo2/logs/*.log {
# The logs will be rotated every hour.
hourly
# If the log file is missing, go on to the next one without issuing an error message.
missingok
# keeping 24 newest logs
rotate 24
# compressing old logs
compress
# creating a new log file to replace the rotated one
create
}
```

3\. Run the following command to create a new log file named `access.log` in your _~/logs_ directory to test the Logrotate settings. This log file is the sample file to rotate and compress later (step four).

```bash
sudo touch logs/access.log
```

4\. Now run the command below to test the custom rule (_~/logrotate.conf_) you set up (step three). The `--state` flag specifies the name of the Logrotate state file (`logrotate-state`). While the `--verbose` flag prints out detailed information about the Logrorate run.

The `logrotate-state` file stores the last run time of the Logrotate rules. As a result, Logrotate will not run the rules if it has already run them before this new instance.

```bash
logrotate /home/demo2/logrotate.conf --state /home/demo2/logrotate-state --verbose
```

Below, you can see that Logrotate ran successfully but did not rotate the logs since the log file is less than one hour old.

![](https://adamtheautomator.com/wp-content/uploads/2022/01/image-89-1024x260.png)

Testing the new rules

Perhaps you want to see the current state of the `logrotate-state` file. If so, run the command below to view the `logrotate-state` file’s content.

```bash
sudo cat /home/demo2/logrotate-state
```

As you can see below, the _logrotate-state_ file contains the following information, which states the last time Logrotate ran the custom rules.

![Previewing the logrotate-state file](https://adamtheautomator.com/wp-content/uploads/2022/01/image-90.png)

Previewing the logrotate-state file

5\. Run the `crontab` command to open the crontab file in your default text editor so you can add a cron job.

```bash
crontab -e
```

6\. In the crontab editor, add the following line to the bottom of the editor and save the changes. This line will run the `logrotate` command every hour using the `/home/demo2/logrotate.conf` and the `/home/demo2/logrotate-state` files.

The cronjob will run the `logrotate` command every hour at `14` minutes past an hour, daily.

```bash
14 * * * * /usr/sbin/logrotate /home/demo2/logrotate.conf --state /home/demo2/logrotate-state
```

![Adding Custom Rule as a Cron Job](https://adamtheautomator.com/wp-content/uploads/2022/01/image-91.png)

Adding Custom Rule as a Cron Job

Related:[How To Execute and List Cron Jobs for a Linux System via PHP](https://adamtheautomator.com/list-cron-jobs/)

7\. Finally, run the `ls` command one hour after creating the _access.log_ file to revisit the ~/_logs_ directory.

```bash
ls logs
```

Below, you’ll see a new rotated and compressed _access.log_ file (**access.log.1.gz**), which indicates Logrotate executed the custom rules successfully.

![ Logrotate in Linux : Checking log files ](https://adamtheautomator.com/wp-content/uploads/2022/01/image-92.png)

Logrotate in Linux : Checking log files

## Conclusion

Throughout this tutorial, you’ve learned to configure Logrotate to run every hour without the need for a sudo privilege. You’ve also touched on adding your custom Logrotate rule as a cronjob to automate the Logrotate process.

At this point, you can now rotate and compress your logs files regularly, which will help you to keep your log files organized and manageable. Additionally, you can free up storage space in the /var/log directory by compressing your log files.

Now how will you build up with this newfound knowledge? Perhaps configure the Logrotate software to automatically [send a daily summary of the rotated logs files to your email](https://serverfault.com/questions/592406/how-can-i-send-access-log-files-automatically-to-a-specific-email-address-after)?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Flogrotate-linux%2F&text=How%20to%20Configure%20Logrotate%20to%20Manage%20Logfiles%20\(Step%20by%20Step\))[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Flogrotate-linux%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Flogrotate-linux%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/09/Comprehensive-Guide-to-Installing-Deb-Packages-on-Ubuntu.jpg)

### [Comprehensive Guide to Install Deb Packages on Ubuntu](/install-deb-packages-on-ubuntu/)

Learn all the ways to install deb packages on Ubuntu, fix problems, and save your sanity with this comprehensive guide!

![](https://adamtheautomator.com/wp-content/uploads/2022/08/How-to-Install-Metasploit-on-Ubuntu.jpg)

### [How to Install Metasploit on Ubuntu](/install-metasploit-on-ubuntu/)

Learning to install Metasploit on Ubuntu isn’t just for malicious actors, learn why in this tutorial on getting started with Metasploit!

![](https://adamtheautomator.com/wp-content/uploads/2022/08/Install-Oh-My-Zsh-on-Ubuntu-For-a-Next-Level-Command-Line-1.jpg)

### [Install Oh My Zsh on Ubuntu For a Next Level Command-Line](/install-oh-my-zsh-on-ubuntu/)

Learn how to install Oh My Zsh on Ubuntu and take your command-line to the next level in this ATA Learning tutorial!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
