VOOZH about

URL: https://www.geeksforgeeks.org/techtips/how-to-monitor-logs-in-linux/

โ‡ฑ Linux Log Monitoring - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Linux Log Monitoring

Last Updated : 15 Nov, 2025

Log monitoring in Linux involves tracking, viewing, and analyzing system log files in real time to detect unusual behavior, troubleshoot problems, or ensure system stability. It helps administrators proactively identify issues before they impact system performance or security.

  • Detects and alerts on system failures or performance issues early.
  • Helps trace user activities and maintain accountability.
  • Ensures compliance and auditing by maintaining event history.
  • Supports security monitoring to identify unauthorized access or attacks.
  • Simplifies debugging and troubleshooting system errors.

Key Log Files in Linux

The following log files are commonly monitored in Linux systems and are located in the /var/log directory:

Log FileDescription
/var/log/syslogRecords general system activity logs.
/var/log/auth.logContains authentication and login attempt information.
/var/log/kern.logLogs kernel-related messages.
/var/log/boot.logStores boot-time events and errors.
/var/log/dmesgContains hardware and driver initialization messages.
/var/log/cron.logStores scheduled task execution details.
/var/log/secureTracks security-related messages and sudo activities.
/var/log/messagesGeneral system messages (used in Red Hat-based systems).

Basic Log Monitoring Commands

Linux provides several built-in commands to view and monitor logs effectively. Below are the most commonly used tools and examples.

1. Viewing Logs with cat Command

Displays the full contents of a log file at once.

Command:

cat /var/log/syslog

Output:

๐Ÿ‘ log

Explanation:

  • Outputs the entire contents of the log file to the terminal.
  • Useful for reviewing logs quickly in smaller files.
  • Can be combined with grep for filtering specific entries.

2. Viewing Logs Page-Wise with less

Used to read large log files conveniently, one screen at a time.

Command:

sudo less /var/log/auth.log

Output:

๐Ÿ‘ auth

Explanation:

  • Enables scrolling through logs line by line or page by page.
  • Use /keyword to search within the file.
  • Press q to quit and return to the terminal.

3. Real-Time Log Monitoring using tail -f

Displays live log updates as new entries are written.

Command:

sudo tail -f /var/log/syslog

Output:

๐Ÿ‘ syslog

Explanation:

  • Continuously monitors log files for new events.
  • Automatically updates the terminal when new logs appear.
  • Useful for watching application or service behavior in real time.

4. Filtering Logs using grep

Searches for specific keywords or patterns in log files.

Command:

sudo grep "error" /var/log/syslog

Output:

๐Ÿ‘ tcp

Explanation:

  • Finds all log entries containing the word โ€œerror.โ€
  • Helps pinpoint problems in large log files.
  • Use -i for case-insensitive searches and -r for recursive directory search.

5. Combining tail and grep for Real-Time Filtering

Monitors logs in real time while filtering for specific events.

Command:

sudo tail -f /var/log/syslog | grep "failed"

Output:

๐Ÿ‘ grep

Explanation:

  • Shows only log entries containing the word โ€œfailedโ€ as they occur.
  • Ideal for monitoring failed login attempts or service errors.
  • Provides a continuous filtered view of log data.

6. Monitoring Kernel Messages using dmesg

Displays kernel-related messages such as hardware events or driver issues.

Command:

sudo dmesg --follow

Output:

๐Ÿ‘ dmesg

Explanation:

  • Monitors live kernel logs as new hardware or driver messages appear.
  • Helps in debugging hardware initialization or kernel-level issues.
  • Similar to tail -f but focuses on kernel ring buffer logs.

7. Viewing System Logs using journalctl

Accesses system logs managed by systemdโ€™s journal.

Command:

sudo journalctl -f

Output:

๐Ÿ‘ ctf

Explanation:

  • Displays live logs from systemd services and system events.
  • Consolidates all service logs in one place.
  • Useful for analyzing boot logs, authentication, and network events.

Advanced Log Monitoring Tools

For more advanced and automated monitoring, Linux provides several powerful tools and utilities.

1. logrotate

Manages and rotates log files to prevent them from consuming too much disk space.

Command:

sudo logrotate /etc/logrotate.conf

Explanation:

  • Automatically compresses and archives old log files.
  • Ensures continuous logging without manual cleanup.
  • Essential for production servers.

2. logger

Creates custom log entries from the command line or scripts.

Command:

logger "Backup completed successfully"

Explanation:

  • Sends custom messages to the system log.
  • Useful for adding application-specific or script-based logs.
  • Logs are stored in /var/log/syslog or /var/log/messages.

3. rsyslog

An enhanced system logging service that forwards or stores logs centrally.

Command:

sudo systemctl status rsyslog

Output:

๐Ÿ‘ ryslog

Explanation:

  • Handles the logging framework on most Linux systems.
  • Can forward logs to a remote log server for centralized monitoring.
  • Used in enterprise environments for audit compliance.

4. journalctl

Systemd-based log viewer with extensive filtering and query options.

Command:

sudo journalctl --since "2025-11-11" --until "2025-11-12"

Output:

๐Ÿ‘ sudo

Explanation:

  • Filters logs within a specific time range.
  • Allows advanced querying by unit, priority, or user.
  • Integrates with modern logging and monitoring systems.

Difference Between Static and Real-Time Monitoring

AspectStatic MonitoringReal-Time Monitoring
DescriptionView stored logs after events occurView logs as they happen in real time
Tools Usedcat, less, greptail -f, journalctl -f, dmesg --follow
Use CasePost-incident analysisLive system or application monitoring
Performance ImpactMinimalSlightly higher (continuous updates)

Best Practices for Log Monitoring

  • Regularly rotate and archive log files using logrotate.
  • Use filters (grep, journalctl) to focus on relevant data.
  • Implement real-time monitoring for critical services like SSH or Apache.
  • Protect log files with proper permissions (chmod, chown).
  • Set up automated alerts for important events or failures.
Comment
Article Tags:
Article Tags: