Log Monitoring & Rotation

πŸš€ Log Monitoring & Rotation in Linux (Complete Guide)

In this guide, we'll cover real-time log monitoring, log rotation, and troubleshooting common log issues with explanations of what happens when you run each command.


πŸ” Step 1: Checking Logs in Linux

1.1 Check System Logs

πŸ”Ή View system logs (recent logs & errors):

journalctl -xe

What Happens?

  • Shows extended logs with errors, warnings, and critical messages.

  • Useful for debugging service failures and system crashes.

πŸ”Ή View logs from a specific service (e.g., SSH):

journalctl -u sshd --since "1 hour ago"

What Happens?

  • Filters logs for the sshd service in the last 1 hour.

πŸ”Ή Check kernel logs (boot & hardware issues):

dmesg | tail -20

What Happens?

  • Displays last 20 kernel logs.

  • Useful for troubleshooting boot issues.


1.2 Real-Time Log Monitoring

πŸ”Ή Monitor logs in real-time (live updates):

What Happens?

  • Continuously displays new log entries.

  • Press CTRL + C to stop.

πŸ”Ή Monitor authentication logs (failed logins, SSH attempts):

What Happens?

  • Shows login attempts and authentication failures.

πŸ”Ή Monitor specific service logs (e.g., Nginx):

What Happens?

  • Displays live access logs for Nginx.

βœ… Common Issue: Logs Not Updating in Real-Time πŸ”Ή Restart logging service:

What Happens?

  • Restarts system logging service to fix log display issues.


πŸ› οΈ Step 2: Log Rotation & Management

Linux systems generate a lot of logs. If logs are not rotated, they can fill up disk space and cause system slowdowns.

2.1 Checking Log Rotation Configuration

πŸ”Ή Check log rotation status:

What Happens?

  • Runs a dry-run of log rotation (shows what it would do without actually rotating logs).

πŸ”Ή Check log rotation for a specific service (e.g., Apache):

What Happens?

  • Displays log rotation settings specific to Apache logs.


2.2 Manually Rotate Logs

πŸ”Ή Force log rotation immediately:

What Happens?

  • Forces immediate rotation of logs based on the current config.

πŸ”Ή Compress logs after rotation:

What Happens?

  • Compresses rotated logs to save space.


2.3 Common Log Rotation Issues & Fixes

βœ… Issue: Logs Are Not Rotating πŸ”Ή Check if logrotate service is running:

Solution: If not running, start it:

βœ… Issue: Disk Full Due to Logs πŸ”Ή Find large log files:

πŸ”Ή Delete old logs manually (if needed):

βœ… Issue: Log Rotation Not Happening Automatically πŸ”Ή Ensure logrotate is enabled in cron jobs:

πŸ”Ή Manually trigger cron jobs:


πŸ”Ž Step 3: Advanced Log Monitoring (Bonus)

3.1 Using grep to Filter Logs

πŸ”Ή Find errors in logs:

What Happens?

  • Searches for "error" (case-insensitive) in syslog.

πŸ”Ή Check failed SSH logins:

3.2 Using journalctl for Persistent Logs

πŸ”Ή View logs after reboot:

πŸ”Ή Clear old logs to free space:

What Happens?

  • Deletes logs older than 7 days.


πŸš€ Summary

βœ” Checked system logs (journalctl, dmesg) βœ” Monitored logs in real-time (tail -f) βœ” Ensured log rotation (logrotate -d, gzip) βœ” Fixed common issues (disk full, log not rotating)


Next: Do you want to set up log monitoring with Prometheus + Loki + Grafana? πŸš€

Last updated