In the realm of server management, configuring logging and implementing regular log rotation are pivotal for efficient and secure operation. Logging allows administrators to monitor server activities, identify potential issues, and respond accordingly. Log rotation, on the other hand, prevents disk space from being consumed by old log entries. This article will focus on the fundamental steps to set up logging and log rotation on CentOS 7.
Logging Basics on CentOS 7
CentOS 7, along with many other Linux distributions, utilizes the rsyslog
system daemon for managing system logging. Rsyslog offers flexible configuration options, including defining where logs should be stored, how frequently they should be rotated, and which events should be logged.
For basic rsyslog
configuration, modify the configuration file located at /etc/rsyslog.conf
. Here, you can define logging rules such as target files for logs from various services. For instance, to log all critical events to the file /var/log/critical.log
, add the following line:
*.crit /var/log/critical.log
Log Rotation with logrotate
For managing log file sizes and archiving, CentOS 7 employs the logrotate
tool. Logrotate
allows administrators to define how often logs should be archived, how many archives the system should retain, and additional actions such as restarting services after log rotation.
Configuration of logrotate
is done through files in the /etc/logrotate.d/
directory. To add or modify log rotation rules, create or edit a configuration file for the respective service. For example, a configuration for rotating critical.log
might look like this:
/var/log/critical.log {
weekly
rotate 4
compress
missingok
notifempty
create 0600 root root
}
This configuration instructs logrotate
to rotate critical.log
on a weekly basis, keep the last four rotations, compress old logs, ignore missing logs, avoid rotating empty logs, and create new logs with 0600
permissions owned by the root
user and group.
Properly configuring logging and log rotation is essential for maintaining the health and security of servers running on CentOS 7. By customizing the rsyslog
and logrotate
configurations, you can ensure that your logs are stored, rotated, and archived appropriately, facilitating server management and aiding in problem diagnosis.