logrotate — Managing and Rotating Log Files¶
logrotate is a Linux utility that automatically manages log files by rotating, compressing, archiving, and removing old logs. Without log rotation, log files can grow indefinitely, consuming disk space and eventually causing applications or the operating system to fail. Every Linux administrator, DevOps engineer, Cloud Architect, Platform Engineer, and Site Reliability Engineer (SRE) should understand how to configure
logrotateto maintain healthy production systems and ensure long-term log management.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand log rotation
- Learn how
logrotateworks - Configure rotation policies
- Compress archived logs
- Configure log retention
- Create custom rotation rules
- Test logrotate configurations
- Apply production logging best practices
Prerequisites¶
Complete:
- Modules 1–11
- Module 12 Lessons 1–3
Why Learn logrotate?¶
Imagine a busy production web server.
Without log rotation:
With logrotate:
Proper log rotation prevents disk space issues while preserving valuable historical logs.
What is logrotate?¶
logrotate is a utility that automatically manages log files.
It can:
- Rotate logs
- Compress old logs
- Archive logs
- Remove old logs
- Create new log files
- Execute scripts before or after rotation
How logrotate Works¶
Applications continue writing to the newly created log file.
Default Configuration¶
Main configuration file:
Application-specific configurations:
Each application typically has its own configuration file.
View Configuration¶
Display the main configuration.
List application configurations.
Basic Configuration Example¶
Meaning:
- Rotate weekly
- Keep four archived logs
- Compress old logs
- Ignore missing logs
- Skip empty logs
Rotation Frequency¶
Common options:
Example:
Rotate once every week.
Number of Rotations¶
Keep:
Example:
Compression¶
Enable compression.
Compressed logs:
Compression saves disk space.
Delay Compression¶
The most recently rotated log remains uncompressed until the next rotation.
Useful for applications that may continue writing briefly after rotation.
Missing Logs¶
Ignore missing files.
No error is generated if the log file does not exist.
Skip Empty Logs¶
Do not rotate empty log files.
Create New Log File¶
Automatically create a new log.
Meaning:
- Permissions:
640 - Owner:
root - Group:
adm
Rotate Based on Size¶
Rotate when a log reaches a specified size.
Rotate when the file reaches 100 MB.
Maximum Size¶
Example:
The log is rotated if it exceeds the specified maximum size.
Execute Commands After Rotation¶
Example:
The application reloads after log rotation.
Execute Commands Before Rotation¶
Example:
Test Configuration¶
Validate configuration without rotating logs.
Debug mode shows what would happen.
Force Rotation¶
Immediately rotate logs.
Useful for testing.
Status File¶
logrotate tracks previous rotations.
Default:
Automatic Scheduling¶
Most Linux distributions run logrotate automatically using:
cronsystemdtimers
Administrators typically do not need to execute it manually.
Common Commands¶
View configuration.
Debug configuration.
Force rotation.
View application rules.
Real Production Examples¶
Rotate web server logs.
Rotate logs larger than 100 MB.
Reload service after rotation.
Production Perspective¶
logrotate is essential for:
- Web servers
- Database servers
- Kubernetes nodes
- Cloud virtual machines
- CI/CD servers
- Security appliances
- Enterprise Linux systems
- Long-term log retention
Proper log management prevents disk exhaustion and preserves historical logs for troubleshooting and compliance.
Hands-on Lab¶
Task 1¶
View the main configuration.
Task 2¶
List application configurations.
Task 3¶
Review the configuration for an application such as Nginx or Apache.
Task 4¶
Run logrotate in debug mode.
Task 5¶
Force log rotation.
Task 6¶
Inspect the status file.
Task 7¶
Locate compressed log files.
Task 8¶
Create a custom logrotate configuration for a test application and verify it using debug mode.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
logrotate -d | Debug configuration | Configuration testing |
logrotate -f | Force rotation | Manual rotation |
cat /etc/logrotate.conf | View global configuration | Administration |
ls /etc/logrotate.d/ | View application rules | Configuration review |
cat /var/lib/logrotate/status | View rotation history | Troubleshooting |
gzip | Compress archived logs | Storage optimization |
Common logrotate Mistakes¶
| Mistake | Solution |
|---|---|
| Never rotating logs | Configure automatic rotation |
| Keeping logs forever | Define an appropriate retention policy |
| Forgetting compression | Enable compress |
| Never testing configurations | Use debug mode before deployment |
| Forgetting to reload services after rotation | Use postrotate scripts where required |
Production Troubleshooting Scenario¶
Scenario
A production server reports:
Investigation:
One application log has grown to 40 GB because log rotation was not configured.
Solution:
- Configure
logrotate. - Set weekly rotation.
- Compress archived logs.
- Retain only the required number of log files.
- Verify automatic scheduling.
The server returns to normal operation with controlled log growth.
Best Practices¶
- Rotate logs automatically.
- Compress archived logs to save disk space.
- Define an appropriate retention policy.
- Test new configurations using debug mode.
- Reload applications after rotation when necessary.
- Monitor disk usage regularly.
- Archive logs according to organizational retention requirements.
- Integrate log rotation with centralized logging solutions.
Common Mistakes¶
❌ Allowing log files to grow without limits.
✅ Do not allow log files to grow without limits.
❌ Never compressing archived logs.
✅ Always compressing archived logs.
❌ Retaining logs indefinitely without a business requirement.
✅ Avoid this mistake: retaining logs indefinitely without a business requirement.
❌ Editing production configurations without testing.
✅ Edit production configurations without testing only when appropriate and with a backup.
❌ Forgetting to reload services after log rotation.
✅ Remember to to reload services after log rotation.
Interview Questions¶
Beginner¶
- What is
logrotate? - Why is log rotation important?
- Where is the main
logrotateconfiguration stored? - Which directive enables log compression?
Intermediate¶
- What is the difference between
compressanddelaycompress? - What does the
rotatedirective control? - How do you test a
logrotateconfiguration? - What is the purpose of the
postrotatesection?
Architect Level¶
- How would you design log retention policies for enterprise applications?
- How would you manage log rotation across thousands of Linux servers?
- How would you integrate
logrotatewith centralized logging platforms?
Summary¶
In this lesson, you learned:
- Log rotation fundamentals
- The
logrotateutility - Rotation schedules
- Compression and retention
- Custom rotation rules
- Testing configurations
- Automatic scheduling
- Production log management best practices
logrotate is an essential Linux utility for maintaining healthy systems by preventing log files from consuming excessive disk space. Proper log rotation ensures continuous application logging, preserves historical records for troubleshooting and compliance, and supports reliable long-term system operations.
Key Takeaways¶
logrotateautomatically manages log files.- Rotate logs regularly based on time or size.
- Compress archived logs to reduce disk usage.
- Use
logrotate -dto validate configurations safely. - Reload applications after rotation when necessary.
- Combine log rotation with centralized logging and monitoring for production environments.
What's Next?¶
Disk Monitoring — Monitoring Storage Usage and Disk Health
You'll explore:
- Monitoring disk usage
- Understanding disk space and inode usage
- Using
df,du, and related tools - Identifying large files and directories
- Monitoring disk I/O
- Troubleshooting storage issues
- Production storage monitoring best practices
By the end of the lesson, you'll be able to monitor disk health, identify storage bottlenecks, manage disk capacity, and prevent storage-related outages in production Linux environments.