Introduction to Log Rotation
Log rotation - it’s one of those tasks that’s easy to overlook, but can quickly become a major headache if you don’t stay on top of it. I’ve seen this go wrong when log files consume all the available disk space, bringing a system to its knees. With many Linux distributions now using systemd-journald as the default logging mechanism, understanding how to manage log rotation is more important than ever.
Understanding systemd-journald
systemd-journald is a powerful tool that collects and stores log messages from various sources, including system services, kernel messages, and user applications. The real trick is learning how to use it effectively. To manage log rotation with systemd-journald, you’ll want to use the journalctl command. For example, you can display the disk usage of journal logs with:
# Display disk usage of journal logs
journalctl --disk-usage
And if you need to free up some disk space, you can vacuum the journal logs like this:
# Vacuum journal logs to free up disk space
journalctl --vacuum-size=1G
Don’t bother with trying to manually edit the journal logs - it’s not worth the hassle.
Traditional Log File Management
In addition to systemd-journald, many Linux systems still use traditional log files, such as /var/log/syslog and /var/log/auth.log. These log files are typically rotated using the logrotate utility, which is configured through the /etc/logrotate.conf file and various configuration files in the /etc/logrotate.d/ directory. You can rotate log files manually with:
# Rotate log files manually
logrotate -f /etc/logrotate.conf
And check the logrotate configuration with:
# Check logrotate configuration
logrotate -d /etc/logrotate.conf
This is where people usually get burned - they don’t realize that logrotate needs to be configured properly to work effectively.
Configuring Log Rotation
To configure log rotation, you’ll need to modify the /etc/logrotate.conf file and add custom configuration files in the /etc/logrotate.d/ directory. I usually start with a basic configuration and then add more complexity as needed. For example, you can create a custom configuration file to rotate the /var/log/apache2/access.log file daily and keep the last 7 days of logs:
# Create a custom logrotate configuration file
sudo nano /etc/logrotate.d/apache2
# Example configuration
/var/log/apache2/*.log {
daily
missingok
notifempty
delaycompress
compress
maxsize 100M
maxage 7
postrotate
invoke-rc.d apache2 reload > /dev/null
endscript
}
In practice, this configuration will work well for most use cases, but you may need to adjust it depending on your specific needs.
Security Considerations
When configuring log rotation, it’s essential to consider security implications. For example, you should ensure that log files are stored in a secure location, such as /var/log/, and that only authorized users have access to them. You can set permissions for log files with:
# Set permissions for log files
sudo chmod 640 /var/log/syslog
And encrypt log files that contain sensitive information using OpenSSL:
# Encrypt log files using OpenSSL
sudo openssl enc -aes-256-cbc -in /var/log/syslog -out /var/log/syslog.enc
This is where security matters - you don’t want unauthorized users accessing your log files.
Troubleshooting Log Rotation Issues
If you encounter issues with log rotation, you can use the journalctl and logrotate commands to troubleshoot the problem. For example, you can use journalctl to check the journal log size:
# Check journal log size
journalctl --disk-usage
And logrotate to check the log rotation configuration:
# Check logrotate configuration
logrotate -d /etc/logrotate.conf
This should help you identify any issues with your log rotation configuration.
Best Practices
To ensure effective log rotation, follow these best practices:
- Regularly check log file sizes and rotate them as needed.
- Configure log rotation to keep a reasonable number of log files.
- Ensure that log files are stored in a secure location.
- Consider encrypting log files that contain sensitive information.
- Monitor log files for security-related issues.
For more information on systemd-journald and log rotation, visit the systemd.io website or the debian.org website.
See also
- Taming Log Rotation: Strategies for Preventing /var/log Overflow on Busy Systems
- Taming systemd Service Restart Behavior: When to Use Restart, Retry, and Timeout Options
- Troubleshooting Broken Permissions on Shared Directories with setgid and ACLs
- Using systemd-resolved with Multiple DNS Servers and Split Horizon DNS
- Taming Dependency Chaos: Using apt-mark to Pin Packages in Debian-Based Systems