Linux logrotate Guide - ryzendew/Linux-Tips-and-Tricks GitHub Wiki
Linux logrotate Guide
Complete beginner-friendly guide to logrotate on Linux, covering Arch Linux, CachyOS, and other distributions including log rotation, configuration, and log management.
Table of Contents
Understanding logrotate
What is logrotate?
logrotate rotates, compresses, and manages log files.
Functions:
- Prevent disk full: Rotate logs before they grow too large
- Compress logs: Save disk space
- Archive logs: Keep old logs for reference
- Delete old logs: Remove logs after retention period
Why it matters:
- Disk space: Prevents logs from filling disk
- Organization: Keeps logs manageable
- Compliance: Maintains log history
logrotate Installation
Install logrotate
Arch/CachyOS:
# Install logrotate
sudo pacman -S logrotate
# Enable service (if using systemd)
sudo systemctl enable logrotate.timer
Debian/Ubuntu:
sudo apt install logrotate
Fedora:
sudo dnf install logrotate
Verify Installation
Check logrotate:
# Check version
logrotate --version
# Check status
systemctl status logrotate.timer
Configuration
Main Configuration
Edit config:
# Main config
sudo vim /etc/logrotate.conf
Common settings:
# Rotate weekly
weekly
# Keep 4 weeks of logs
rotate 4
# Create new empty log
create
# Compress old logs
compress
# Delay compression
delaycompress
Configuration Directory
Per-application configs:
# Application configs
/etc/logrotate.d/
# List configs
ls /etc/logrotate.d/
Creating Rules
Basic Rule
Create rule:
# Create rule file
sudo vim /etc/logrotate.d/myapp
Basic rule:
/var/log/myapp.log {
daily
rotate 7
compress
missingok
notifempty
}
Advanced Rule
Advanced options:
/var/log/myapp/*.log {
daily
rotate 30
compress
delaycompress
missingok
notifempty
create 0640 user group
sharedscripts
postrotate
/usr/bin/systemctl reload myapp
endscript
}
Testing
Test Configuration
Test logrotate:
# Test configuration
sudo logrotate -d /etc/logrotate.conf
# Test specific config
sudo logrotate -d /etc/logrotate.d/myapp
# Force rotation (dry run)
sudo logrotate -f /etc/logrotate.conf
Manual Rotation
Rotate manually:
# Force rotation
sudo logrotate -f /etc/logrotate.d/myapp
# Verbose output
sudo logrotate -v /etc/logrotate.d/myapp
Troubleshooting
Logs Not Rotating
Check configuration:
# Test config
sudo logrotate -d /etc/logrotate.conf
# Check for errors
sudo logrotate -v /etc/logrotate.conf
# Check state file
cat /var/lib/logrotate/status
Permission Issues
Fix permissions:
# Check log file permissions
ls -la /var/log/myapp.log
# Fix ownership
sudo chown user:group /var/log/myapp.log
Summary
This guide covered logrotate installation, configuration, and log management for Arch Linux, CachyOS, and other distributions.
Next Steps
- Log Management - System logs
- System Monitoring - System monitoring
- System Configuration - System setup
- logrotate Documentation:
man logrotate
This guide covers Arch Linux, CachyOS, and other Linux distributions. For distribution-specific details, refer to your distribution's documentation.