4 ways To Check System Logs in Linux: Troubleshoot Issues Like a Pro

Troubleshooting a Linux system often feels like detective work, but the system itself provides the clues you need through its logging mechanisms.

Linux provides built-in commands and standardized directories to make log management straightforward. Master these approaches to maintain effective control over your system’s health.

Key Takeaways: How to Check System Logs in Linux

  • journalctl command → The modern standard for querying systemd-based logs. It allows for advanced filtering by service name or time.
  • /var/log directory → The central hub for traditional log files. It contains persistent data like general system messages and application-specific logs.
  • tail command → Essential for real-time debugging. The -f flag lets you watch new log entries as they are written to the disk.
  • less and cat commands → Fundamental tools for viewing static log files. Use less for longer logs to enable paging and scrolling.

Method 1: Using the journalctl Command

Modern Linux distributions use systemd to manage logs through a binary format known as the journal. The journalctl command is the primary tool used to query this data.

Run this command to view the most recent logs: journalctl -xe

The -e flag jumps to the end of the log, while -x adds explanatory text to help diagnose common errors. For a deeper dive into filtering by time or priority, you can refer to a complete guide to using journalctl.

Method 2: Inspecting /var/log/messages

While many logs are handled by systemd, the file /var/log/messages remains a vital destination for general system events that should persist between boots.

Use this syntax to view the end of the file: sudo tail -n 5 /var/log/messages

Because system logs often contain sensitive data, you must use administrative privileges to view them; otherwise, you will receive a “Permission denied” error. You can learn more about managing these rights in our guide to understanding the Linux sudo command.

Method 3: Monitoring Logs in Real-Time with tail

The tail command is indispensable when you need to watch a process execute or catch an error as it happens.

Run this to “follow” a log file: tail -f /var/log/secure

See also: Mastering the Linux Command Line — Your Complete Free Training Guide

The -f flag keeps the file open and updates the screen whenever a new line is written. This is particularly useful for verifying authentication attempts or if you need to check system reboot logs during troubleshooting.

Method 4: Viewing Static Logs with less

For older logs or specific configuration records, the less command provides a more controlled viewing experience than cat because it allows you to page through the data.

Run this to open a log file for reading: less /var/log/dmesg

Use the UpArrow and DownArrow keys to scroll, and press q to exit. This method is much more efficient than dumping large files to your terminal, similar to how you might check file size in Linux before auditing storage usage.


Summary Tables

TaskRecommended CommandPrimary Purpose
Check Modern Journaljournalctl -xeView latest systemd service logs.
View Static System Logless /var/log/messagesInspect persistent system-wide events.
Follow Active Logstail -f <filename>Watch errors occur in real-time.
Verify Boot Messagesjournalctl -bReview logs from the current boot session.
Audit Security Logstail /var/log/secureCheck for login failures and sudo usage.
Important Log LocationType of Data Stored
/var/log/messagesGeneral system messages and daemon info.
/var/log/secureAuthentication and authorization records.
/var/log/boot.logMessages generated during system startup.
/var/log/dmesgKernel ring buffer and hardware-related info.
/var/log/cronLog data for scheduled background tasks.

FAQs

What is the quickest way to check logs in Linux? For a quick glance at the most recent activity, use sudo tail -n 20 /var/log/messages. This displays the last 20 lines of the main system log.

Why do I get “Permission denied” when viewing logs? System logs are restricted to protect security boundaries between users. You must use sudo to gain the necessary access.

How can I search for a specific error in my logs? You can pipe log output to grep. For example, journalctl | grep -i "error" will filter the journal for any mention of an error regardless of case.

Can I view logs from a previous boot? Yes, if persistent logging is enabled, use journalctl -b -1 to view logs from the previous boot session.


Related Posts

David Cao
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 615

Leave a Reply

Your email address will not be published. Required fields are marked *