Systemd journal logs are essential for system monitoring and troubleshooting, but they can consume significant disk space over time. This comprehensive guide covers everything you need to know about managing and cleaning journalctl logs effectively.
Understanding Systemd Journal
What is Journalctl?
Journalctl is a command-line utility that provides access to the systemd journal logs. The systemd journal is a centralized logging system that collects and stores log data from various system services and applications.
Key Features of Systemd Journal
- Structured Logging: Binary format for efficient storage and querying
- Persistent Storage: Logs are stored in
/var/log/journal/
- Metadata Support: Includes timestamps, service names, and priority levels
- Real-time Logging: Live log monitoring capabilities
- Log Rotation: Built-in mechanisms for log management
Journal Storage Location
# Default journal storage location
/var/log/journal/
# Check current journal size
du -sh /var/log/journal/
Checking Journal Status and Size
View Current Journal Size
# Check total journal size
journalctl --disk-usage
# Expected output:
# Archived and active journals take up 1.2G in the file system.
List Journal Files
# List all journal files
ls -lh /var/log/journal/
# Check journal directory permissions
ls -ld /var/log/journal/
Monitor Journal Growth
# Watch journal size in real-time
watch -n 1 'journalctl --disk-usage'
# Check journal entries count
journalctl --no-pager | wc -l
Cleaning Journal Logs
Method 1: Using journalctl Commands
Clean Old Logs
# Remove logs older than specified time
sudo journalctl --vacuum-time=3d # Keep last 3 days
sudo journalctl --vacuum-time=1w # Keep last week
sudo journalctl --vacuum-time=1m # Keep last month
# Remove logs exceeding size limit
sudo journalctl --vacuum-size=500M # Keep last 500MB
sudo journalctl --vacuum-size=1G # Keep last 1GB
# Remove logs exceeding number of files
sudo journalctl --vacuum-files=5 # Keep last 5 files
Verify Cleanup Results
# Check journal size after cleanup
journalctl --disk-usage
# List remaining journal files
ls -lh /var/log/journal/
Method 2: Manual Cleanup
Stop Journal Service
# Stop the journal service
sudo systemctl stop systemd-journald
# Verify service status
sudo systemctl status systemd-journald
Remove Journal Files
# Remove all journal files
sudo rm -rf /var/log/journal/*
# Or remove specific journal files
sudo rm -f /var/log/journal/*/system@*.journal
Restart Journal Service
# Start the journal service
sudo systemctl start systemd-journald
# Verify service status
sudo systemctl status systemd-journald
Method 3: Configure Journal Size Limits
Edit Journal Configuration
# Edit journal configuration
sudo nano /etc/systemd/journald.conf
journald.conf:
[Journal]
# Maximum size of journal files
SystemMaxUse=500M
SystemMaxFileSize=100M
# Maximum number of journal files
SystemMaxFiles=5
# Maximum age of journal files
MaxRetentionSec=1week
# Compress journal files
Compress=yes
Apply Configuration
# Restart journal service
sudo systemctl restart systemd-journald
# Verify configuration
sudo systemctl status systemd-journald
Managing Journal Logs
Viewing Logs
Basic Log Viewing
# View all logs
journalctl
# View logs with timestamps
journalctl -o short-precise
# View logs in real-time
journalctl -f
# View logs since boot
journalctl -b
Filtering Logs
# View logs for specific service
journalctl -u nginx
journalctl -u apache2
# View logs by priority
journalctl -p err
journalctl -p warning
# View logs by time
journalctl --since "2022-03-01"
journalctl --until "2022-03-15"
journalctl --since "1 hour ago"
Exporting Logs
# Export logs to file
journalctl > system-logs.txt
# Export logs in JSON format
journalctl -o json > system-logs.json
# Export logs for specific service
journalctl -u nginx > nginx-logs.txt
Automating Journal Maintenance
Create Cleanup Script
# Create cleanup script
sudo nano /usr/local/bin/clean-journal.sh
clean-journal.sh:
#!/bin/bash
# Set maximum journal size (in MB)
MAX_SIZE=500
# Clean journal logs
journalctl --vacuum-size=${MAX_SIZE}M
# Verify cleanup
journalctl --disk-usage
# Log cleanup results
echo "Journal cleanup completed at $(date)" >> /var/log/journal-cleanup.log
# Make script executable
sudo chmod +x /usr/local/bin/clean-journal.sh
# Test script
sudo /usr/local/bin/clean-journal.sh
Schedule Regular Cleanup
# Create systemd timer
sudo nano /etc/systemd/system/journal-cleanup.timer
journal-cleanup.timer:
[Unit]
Description=Run journal cleanup daily
[Timer]
OnCalendar=daily
AccuracySec=1h
Persistent=true
[Install]
WantedBy=timers.target
# Create service file
sudo nano /etc/systemd/system/journal-cleanup.service
journal-cleanup.service:
[Unit]
Description=Journal Cleanup Service
[Service]
Type=oneshot
ExecStart=/usr/local/bin/clean-journal.sh
[Install]
WantedBy=multi-user.target
# Enable and start timer
sudo systemctl enable journal-cleanup.timer
sudo systemctl start journal-cleanup.timer
# Check timer status
sudo systemctl status journal-cleanup.timer
Best Practices
Regular Maintenance
- Monitor Journal Size: Regularly check journal size using
journalctl --disk-usage
- Set Size Limits: Configure appropriate size limits in
journald.conf
- Schedule Cleanup: Implement automated cleanup using systemd timers
- Archive Important Logs: Export and archive critical logs before cleanup
- Monitor Disk Space: Keep track of available disk space
Performance Optimization
# Optimize journal configuration
[Journal]
# Enable compression
Compress=yes
# Set appropriate size limits
SystemMaxUse=500M
SystemMaxFileSize=100M
# Enable persistent storage
Storage=persistent
# Set maximum number of files
SystemMaxFiles=5
Security Considerations
- File Permissions: Ensure proper permissions on journal files
- Access Control: Limit access to journal files
- Sensitive Data: Be cautious with logs containing sensitive information
- Backup Strategy: Implement proper backup procedures for important logs
Troubleshooting
Common Issues
Issue 1: Journal Service Not Starting
Symptoms:
Failed to start Journal Service
Solutions:
# Check journal service status
sudo systemctl status systemd-journald
# Check journal directory permissions
ls -ld /var/log/journal/
# Fix permissions if needed
sudo chown -R root:systemd-journal /var/log/journal/
sudo chmod 2755 /var/log/journal/
Issue 2: Excessive Disk Usage
Symptoms:
No space left on device
Solutions:
# Check disk usage
df -h
# Clean journal logs
sudo journalctl --vacuum-size=100M
# Verify cleanup
journalctl --disk-usage
Issue 3: Corrupted Journal Files
Symptoms:
Failed to read journal file
Solutions:
# Stop journal service
sudo systemctl stop systemd-journald
# Remove corrupted files
sudo rm -f /var/log/journal/*/system@*.journal-*
# Start journal service
sudo systemctl start systemd-journald
Advanced Usage
Custom Journal Views
# View logs in JSON format
journalctl -o json
# View logs in JSON format with specific fields
journalctl -o json-pretty
# View logs in verbose format
journalctl -o verbose
# View logs in export format
journalctl -o export
Journal Analysis
# Count log entries by priority
journalctl -o json | jq '.PRIORITY' | sort | uniq -c
# Find most frequent error messages
journalctl -p err | grep -v "^\s*$" | sort | uniq -c | sort -nr | head -n 10
# Analyze log patterns
journalctl | grep -i "error" | awk '{print $5}' | sort | uniq -c | sort -nr
Remote Journal Access
# View logs from remote system
journalctl -H user@remote-server
# Export logs from remote system
ssh user@remote-server 'journalctl' > remote-logs.txt
Conclusion
Proper management of systemd journal logs is crucial for maintaining system performance and disk space. By implementing regular cleanup procedures and following best practices, you can ensure efficient log management while preserving important system information.
Key Takeaways
- Regular Maintenance: Implement automated cleanup procedures
- Size Management: Set appropriate size limits for journal files
- Monitoring: Regularly check journal size and disk usage
- Backup Strategy: Archive important logs before cleanup
- Security: Maintain proper permissions and access controls
Next Steps
- Implement Cleanup Schedule: Set up automated cleanup using systemd timers
- Configure Size Limits: Adjust journal size limits based on your needs
- Monitor Usage: Regularly check journal size and disk space
- Document Procedures: Maintain documentation of your log management procedures
- Review Logs: Periodically review logs for system issues and security events
Remember that while cleaning journal logs is important for system maintenance, it's equally important to ensure you're not deleting logs that might be needed for troubleshooting or compliance purposes. Always implement a balanced approach to log management that considers both system performance and operational requirements.