Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Set Up and Manage Log Rotation on Linux

Log rotation is a crucial process in system administration, especially on Linux systems, where logs can grow quickly and consume significant disk space. Proper log rotation ensures that log files are archived and compressed, preventing them from becoming excessively large and unmanageable. This article will guide you through the process of setting up and managing log rotation on a Linux system using the logrotate utility.


Examples:


1. Installing Logrotate:
Most Linux distributions come with logrotate pre-installed. However, if it is not installed, you can do so using your package manager.


For Debian-based systems (like Ubuntu):


   sudo apt-get update
sudo apt-get install logrotate

For Red Hat-based systems (like CentOS):


   sudo yum install logrotate

2. Basic Configuration:
The main configuration file for logrotate is usually located at /etc/logrotate.conf. This file includes global settings and can include other configuration files.


Example of a basic /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

3. Creating a Logrotate Configuration for a Specific Log File:
You can create specific log rotation rules for individual applications by creating a configuration file in /etc/logrotate.d/.


Example configuration for Apache logs (/etc/logrotate.d/apache2):


   /var/log/apache2/*.log {
daily
missingok
rotate 14
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
}

4. Manual Log Rotation:
You can manually trigger log rotation to test your configuration using the following command:


   sudo logrotate -f /etc/logrotate.conf

5. Debugging Logrotate:
If you encounter issues, you can run logrotate in debug mode to see detailed output:


   sudo logrotate -d /etc/logrotate.conf

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.