In the realm of production server management, disk space exhaustion is one of the most frequent and critical operational incidents. Unlike a sudden hardware failure, which often triggers immediate alarms, a slow-creeping disk fill caused by unmanaged application logs can go unnoticed until services crash, websites become unreachable, or database writes fail silently. The root cause is almost invariably a lack of robust log rotation strategies. Applications generate gigabytes of data daily; without intervention, these files consume the partition until the filesystem reaches 100% capacity. Mastering log rotation is not merely an administrative task; it is a fundamental requirement for system stability and security hygiene. This guide provides a comprehensive approach to configuring log rotation on Linux systems, ensuring that your production environment remains resilient against storage saturation.
Understanding the Mechanics of Log Growth
Before diving into configuration, it is essential to understand why logs grow unchecked by default. Most logging frameworks, such as Java’s java.util.logging, Python’s standard library, or Nginx/Apache defaults, write continuously to files without truncation. When a process writes to a file, the operating system extends the file size on the disk. If no external mechanism intervenes, this growth is linear and unbounded relative to the log buffer size.
The danger lies in the “silent failure” scenario. A server might have 80% free space today but hit 95% tomorrow due to a spike in error logging caused by a code deployment or a security attack. Once the filesystem hits 100%, critical system processes like systemd-journald, rsync, or database backups may fail because they cannot create temporary files. This leads to cascading failures where non-critical services stop, but the core application remains stuck trying to write logs that can no longer be written.
The Role of Logrotate and Systemd Journal
Linux provides a built-in utility called logrotate specifically designed to manage this lifecycle. It is configured via a file located at /etc/logrotate.conf, with specific rules stored in files under /etc/logrotate.d/. This tool automates the rotation process based on time (daily, weekly) or size thresholds. When triggered, it performs three key actions: it cuts the current log file to create a new one for active writing, compresses the old file into a .gz format, and optionally deletes files older than a defined retention period.
For modern systems using systemd, logs are often directed to the journal rather than plain text files. While journalctl handles rotation internally, relying solely on it can be risky if disk usage is not monitored closely, as the journal stores everything by default. Therefore, even in a systemd environment, configuring /etc/logrotate.d/journal with specific size limits (e.g., rotating when the system partition reaches 10GB) is a best practice to prevent the entire disk from filling up.
Configuring Rotation Policies for Different Log Types
Different applications require different rotation strategies. A generic policy applied to all logs may lead to either premature deletion of valuable debugging data or excessive storage consumption. You must tailor your configuration files in /etc/logrotate.d/ based on the log source.
For web servers like Nginx, a common pattern involves rotating access and error logs daily if they exceed 100MB. The configuration should specify copytruncate if you cannot restart the service to switch file handles, though restarting the service is generally preferred for performance. For database logs, such as MySQL or PostgreSQL, rotation policies must be more aggressive regarding retention periods but careful about compression overhead, as these logs can become massive during high-load transactions.
When configuring a rule, consider the maxsize directive. Setting rotate to occur when a log file hits 50MB ensures that no single log file ever becomes a threat to system stability. You should also utilize the missingok option to prevent errors if a log file is deleted manually between rotations, and notifempty to skip rotation if the log has not captured any new entries since the last run, saving I/O resources.
Handling Compression and Retention Periods
Compression is the most effective way to save disk space after rotation. The compress directive in logrotate utilizes gzip or bzip2 to shrink old log files. However, compression adds CPU overhead during the rotation process. For high-throughput servers with limited CPU headroom, you might opt for lighter compression or delay it until a later maintenance window using the delaycompress option.
Retention periods are equally critical. The rotate 7 directive keeps seven rotated logs before deleting them. However, this is a default and often insufficient for compliance requirements or forensic investigations. You should define custom retention windows based on your organizational needs. For example, security audit logs might need to be retained for 90 days, while application debug logs might only need 7 days. Ensure that the dateext option is used so that rotated files are named with dates (e.g., app.log.2023-10-27) rather than just sequence numbers, making it easier to identify and manage files manually if necessary.
Real-World Example: Securing a Production Nginx Server
Consider a production Nginx server handling a high-traffic e-commerce site. The default configuration might allow error logs to grow indefinitely during a DDoS attack or a buggy deployment. To prevent disk exhaustion, an administrator would create or edit /etc/logrotate.d/nginx.
A robust configuration snippet would look like this:
/var/log/nginx/*.log {
daily
missingok
notifempty
rotate 14
compress
delaycompress
copytruncate
dateext
maxsize 50M
postrotate
/usr/bin/systemctl reload nginx > /dev/null 2>&1 || true
endscript
}
In this example, logs rotate daily but also if they exceed 50MB. The copytruncate command copies the current log to a backup and truncates the original, allowing Nginx to continue writing without needing a restart (though restarting is often safer). The postrotate script sends a reload signal to Nginx to ensure it picks up any new configuration changes if they were made during the rotation cycle. This setup balances immediate safety with operational continuity.
Common Pitfalls and Verification Steps
Even with a well-configured policy, mistakes can occur. A common error is forgetting to update the maxsize or rotate count after changing disk capacity requirements, leading to unexpected deletions of data. Another frequent issue is ignoring permission errors; if the log file permissions change (e.g., from 640 to 644), logrotate might fail silently or with permission denied errors, leaving logs unrotated. Always run logrotate -d /etc/logrotate.conf in debug mode to test configurations without making changes.
It is also vital to monitor the rotation process. After a rotation event, check disk usage immediately using df -h. If space does not decrease as expected, inspect the compressed files to ensure compression succeeded. Regularly reviewing /var/log/logrotate.log reveals any skipped rotations or errors that need attention. Finally, integrate log rotation checks into your monitoring stack (like Prometheus or Nagios) to alert on disk usage trends before they become critical, ensuring that manual intervention is only needed in edge cases rather than routine failures.
Conclusion
Mastering log rotation transforms a potential point of failure into a controlled, automated process. By understanding the mechanics of log growth, configuring tailored policies for specific applications, leveraging compression and retention strategies, and rigorously testing your setup, you can prevent disk space issues before they impact production services. Remember that terms, availability, eligibility, laws, and prices depend on insurer, policy and jurisdiction; similarly, system behavior depends on OS versions and specific configurations. Always verify your rules against your current environment. Implementing these practices ensures that your server infrastructure remains stable, secure, and ready to handle the unpredictable nature of production workloads.



