Linux logrotate Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux logrotate command to manage log files on your system. Logrotate is a utility that automatically rotates, compresses, and deletes old log files, helping to maintain the health and performance of your system. You will first explore the basic usage of the logrotate command, including checking the version installed and understanding the default configuration. Then, you will configure logrotate to manage the Apache web server logs, customizing the rotation settings to meet your specific needs.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/less("`File Paging`") linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") linux/VersionControlandTextEditorsGroup -.-> linux/nano("`Simple Text Editing`") linux/SystemInformationandMonitoringGroup -.-> linux/service("`Service Managing`") subgraph Lab Skills linux/cat -.-> lab-422769{{"`Linux logrotate Command with Practical Examples`"}} linux/less -.-> lab-422769{{"`Linux logrotate Command with Practical Examples`"}} linux/apt -.-> lab-422769{{"`Linux logrotate Command with Practical Examples`"}} linux/sudo -.-> lab-422769{{"`Linux logrotate Command with Practical Examples`"}} linux/touch -.-> lab-422769{{"`Linux logrotate Command with Practical Examples`"}} linux/chmod -.-> lab-422769{{"`Linux logrotate Command with Practical Examples`"}} linux/nano -.-> lab-422769{{"`Linux logrotate Command with Practical Examples`"}} linux/service -.-> lab-422769{{"`Linux logrotate Command with Practical Examples`"}} end

Introduction to Logrotate Command

In this step, you will learn about the logrotate command in Linux. Logrotate is a utility that helps manage log files by automatically rotating, compressing, and deleting old log files. This is important for maintaining the health and performance of your system, as log files can quickly consume a large amount of disk space if not properly managed.

First, let's check the version of logrotate installed on your system:

sudo logrotate --version

Example output:

logrotate 3.18.1

Next, let's explore the basic usage of the logrotate command. The logrotate command reads the configuration file /etc/logrotate.conf and any configuration files found in the /etc/logrotate.d/ directory to determine how to rotate log files.

To see the default configuration, you can run:

sudo cat /etc/logrotate.conf

This will show you the default settings for log rotation, such as the frequency of rotation, the number of old log files to keep, and the compression method used.

You can also manually rotate log files using the logrotate command. For example, to rotate the Apache access log, you can run:

sudo logrotate /etc/logrotate.d/apache2

This will immediately rotate the Apache access log file, regardless of the configured schedule.

Configure Logrotate for Apache Web Server Logs

In this step, you will learn how to configure logrotate to manage the Apache web server logs.

First, let's install the Apache web server:

sudo apt-get update
sudo apt-get install -y apache2

Once the installation is complete, you can check the default Apache log files:

ls -l /var/log/apache2/

Example output:

-rw-r--r-- 1 root root   0 Apr 26 12:00 access.log
-rw-r--r-- 1 root root   0 Apr 26 12:00 error.log

Now, let's create a custom logrotate configuration file for the Apache logs:

sudo nano /etc/logrotate.d/apache2

Add the following configuration:

/var/log/apache2/*.log {
    daily
    missingok
    rotate 7
    compress
    delaycompress
    notifempty
    create 640 root adm
    sharedscripts
    postrotate
        if /etc/init.d/apache2 status > /dev/null; then
            /etc/init.d/apache2 reload > /dev/null
        fi
    endscript
}

This configuration will:

  • Rotate the Apache access and error logs daily
  • Keep 7 days of log files
  • Compress the rotated log files
  • Create new log files with the specified permissions
  • Reload the Apache service after rotation to ensure the new log files are used

To test the configuration, you can manually rotate the logs:

sudo logrotate /etc/logrotate.d/apache2

Check the log directory again to see the rotated files:

ls -l /var/log/apache2/

Example output:

-rw-r--r-- 1 root root   0 Apr 26 12:00 access.log
-rw-r--r-- 1 root root   0 Apr 26 12:00 error.log
-rw-r--r-- 1 root root   0 Apr 26 12:01 access.log.1.gz
-rw-r--r-- 1 root root   0 Apr 26 12:01 error.log.1.gz

Customize Logrotate Configuration for Specific Log Files

In this step, you will learn how to customize the logrotate configuration for specific log files on your system.

Let's create a custom log file and configure logrotate to manage it.

First, create a sample log file:

sudo touch /var/log/custom.log
sudo chmod 644 /var/log/custom.log

Now, create a new logrotate configuration file for the custom log:

sudo nano /etc/logrotate.d/custom-logs

Add the following configuration:

/var/log/custom.log {
    weekly
    rotate 4
    compress
    delaycompress
    notifempty
    create 644 root adm
}

This configuration will:

  • Rotate the custom.log file weekly
  • Keep 4 weeks of log files
  • Compress the rotated log files
  • Create new log files with the specified permissions

To test the configuration, you can manually rotate the logs:

sudo logrotate /etc/logrotate.d/custom-logs

Check the log directory to see the rotated files:

ls -l /var/log/

Example output:

-rw-r--r-- 1 root adm     0 Apr 26 12:00 custom.log
-rw-r--r-- 1 root adm     0 Apr 19 12:00 custom.log.1.gz
-rw-r--r-- 1 root adm     0 Apr 12 12:00 custom.log.2.gz
-rw-r--r-- 1 root adm     0 Apr  5 12:00 custom.log.3.gz
-rw-r--r-- 1 root adm     0 Mar 29 12:00 custom.log.4.gz

You can also customize the logrotate configuration for other log files on your system, such as application-specific logs or system logs, by creating additional configuration files in the /etc/logrotate.d/ directory.

Summary

In this lab, you learned about the logrotate command in Linux, which is a utility that helps manage log files by automatically rotating, compressing, and deleting old log files. You explored the basic usage of the logrotate command, including checking the version, viewing the default configuration, and manually rotating log files. Additionally, you configured logrotate to manage the Apache web server logs, creating a custom logrotate configuration file and understanding the various options available for log rotation.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like