Home→Blog→How to Clean and Manage Journalctl Logs
πŸ“… March 15, 2022⏱️ 7 minutes min read

How to Clean and Manage Journalctl Logs

Learn how to clean, manage, and optimize systemd journal logs using journalctl. Complete guide with practical examples for log rotation, cleanup, and maintenance.

How to Clean and Manage Journalctl Logs

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

bash
# 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

bash
# Check total journal size
journalctl --disk-usage

# Expected output:
# Archived and active journals take up 1.2G in the file system.

List Journal Files

bash
# List all journal files
ls -lh /var/log/journal/

# Check journal directory permissions
ls -ld /var/log/journal/

Monitor Journal Growth

bash
# 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

bash
# 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

bash
# Check journal size after cleanup
journalctl --disk-usage

# List remaining journal files
ls -lh /var/log/journal/

Method 2: Manual Cleanup

Stop Journal Service

bash
# Stop the journal service
sudo systemctl stop systemd-journald

# Verify service status
sudo systemctl status systemd-journald

Remove Journal Files

bash
# 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

bash
# 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

bash
# Edit journal configuration
sudo nano /etc/systemd/journald.conf

journald.conf:

ini
[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

bash
# Restart journal service
sudo systemctl restart systemd-journald

# Verify configuration
sudo systemctl status systemd-journald

Managing Journal Logs

Viewing Logs

Basic Log Viewing

bash
# 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

bash
# 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

bash
# 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

bash
# Create cleanup script
sudo nano /usr/local/bin/clean-journal.sh

clean-journal.sh:

bash
#!/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
bash
# Make script executable
sudo chmod +x /usr/local/bin/clean-journal.sh

# Test script
sudo /usr/local/bin/clean-journal.sh

Schedule Regular Cleanup

bash
# Create systemd timer
sudo nano /etc/systemd/system/journal-cleanup.timer

journal-cleanup.timer:

ini
[Unit]
Description=Run journal cleanup daily

[Timer]
OnCalendar=daily
AccuracySec=1h
Persistent=true

[Install]
WantedBy=timers.target
bash
# Create service file
sudo nano /etc/systemd/system/journal-cleanup.service

journal-cleanup.service:

ini
[Unit]
Description=Journal Cleanup Service

[Service]
Type=oneshot
ExecStart=/usr/local/bin/clean-journal.sh

[Install]
WantedBy=multi-user.target
bash
# 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

  1. Monitor Journal Size: Regularly check journal size using journalctl --disk-usage
  2. Set Size Limits: Configure appropriate size limits in journald.conf
  3. Schedule Cleanup: Implement automated cleanup using systemd timers
  4. Archive Important Logs: Export and archive critical logs before cleanup
  5. Monitor Disk Space: Keep track of available disk space

Performance Optimization

ini
# 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

  1. File Permissions: Ensure proper permissions on journal files
  2. Access Control: Limit access to journal files
  3. Sensitive Data: Be cautious with logs containing sensitive information
  4. Backup Strategy: Implement proper backup procedures for important logs

Troubleshooting

Common Issues

Issue 1: Journal Service Not Starting

Symptoms:

text
Failed to start Journal Service

Solutions:

bash
# 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:

text
No space left on device

Solutions:

bash
# Check disk usage
df -h

# Clean journal logs
sudo journalctl --vacuum-size=100M

# Verify cleanup
journalctl --disk-usage

Issue 3: Corrupted Journal Files

Symptoms:

text
Failed to read journal file

Solutions:

bash
# 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

bash
# 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

bash
# 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

bash
# 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

  1. Regular Maintenance: Implement automated cleanup procedures
  2. Size Management: Set appropriate size limits for journal files
  3. Monitoring: Regularly check journal size and disk usage
  4. Backup Strategy: Archive important logs before cleanup
  5. Security: Maintain proper permissions and access controls

Next Steps

  1. Implement Cleanup Schedule: Set up automated cleanup using systemd timers
  2. Configure Size Limits: Adjust journal size limits based on your needs
  3. Monitor Usage: Regularly check journal size and disk space
  4. Document Procedures: Maintain documentation of your log management procedures
  5. 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.

Tags:

Related Posts