Managing log files is an essential task for every system administrator. Log files can grow rapidly, consuming valuable disk space and making it difficult to search for relevant information. Logrotate is a utility that simplifies the management of log files on Linux systems by automatically rotating, compressing, and removing them as needed. In this comprehensive guide, we will cover how to install and configure logrotate on Rocky Linux.
Table of Contents
- Introduction to Logrotate
- Installing Logrotate
- Configuring Logrotate
- Creating Custom Logrotate Configurations
- Testing Logrotate Configuration
- Automating Logrotate with Cron
- Conclusion
How to Install and Configure Logrotate on Rocky Linux
Introduction to Logrotate
Logrotate is a Linux utility designed to help system administrators manage log files generated by various applications and services. It offers automatic rotation, compression, removal, and mailing of log files. By using logrotate, you can ensure that your log files are organized, up-to-date, and easy to manage. Logrotate is used in conjunction with other log management tools, such as Rsyslog and Fail2ban.
Installing Logrotate on Rocky Linux
Rocky Linux comes with logrotate pre-installed, but in case it’s missing, you can install it using the following command:
sudo dnf install logrotate
Configuring Logrotate on Rocky Linux
Logrotate’s main configuration file is located at /etc/logrotate.conf
. This file contains global settings and includes additional configuration files from the /etc/logrotate.d
directory.
To edit the main configuration file, open it with a text editor of your choice:
sudo nano /etc/logrotate.conf
The default configuration should look similar to this:
# see "man logrotate" for details
# rotate log files weekly
weekly
# use the syslog group by default, since this is the owning group
# of /var/log/syslog.
su root syslog
# 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
# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
monthly
minsize 1M
create 0664 root utmp
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}
# system-specific logs may be also be configured here.
Feel free to modify the settings according to your requirements. For example, you can uncomment the compress
line to enable log file compression.
Creating Custom Logrotate Configurations in Linux
You can create custom logrotate configurations for specific applications by creating a new file in the /etc/logrotate.d
directory. This is useful when you want to manage logs for applications that don’t have a default logrotate configuration, or if you want to override the default settings.
For example, let’s create a custom logrotate configuration for an application called my_app
:
- Create a new configuration file for
my_app
:
sudo nano /etc/logrotate.d/my_app
Add the following content to the configuration file, adjusting the settings according to your requirements:
/var/log/my_app/*.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 640 root root
postrotate
systemctl restart my_app.service
endscript
}
This configuration will rotate logs for my_app
daily, keeping 7 days of compressed log files. It will also restart the my_app
service after rotating the logs.
Save the file and exit the text editor.
Test the new logrotate configuration:
sudo logrotate -d -f /etc/logrotate.d/my_app
This command will display the actions logrotate would perform, without actually executing them.
If the test is successful, the custom logrotate configuration for my_app
is now in place.
Monitoring Logrotate on Rocky Linux
To ensure that logrotate is running as expected, you can check its status by examining the /var/lib/logrotate/status
file:
cat /var/lib/logrotate/status
This file contains information about the last rotation date for each log file managed by logrotate. If you notice any issues or inconsistencies, you can check the logrotate script for errors by running:
sudo logrotate -d /etc/logrotate.conf
This command will display the debug information for logrotate, helping you identify any issues with your configuration.
Conclusion
In this tutorial, we’ve covered how to install and configure logrotate on Rocky Linux. Logrotate is an essential tool for managing log files on your server, and by following these steps, you can ensure that your logs are properly rotated, compressed, and archived to save disk space and maintain system stability. For more tutorials on managing your Rocky Linux server, check out the following articles: