The journalctl command is a powerful utility in Red Hat Enterprise Linux 8 for querying and displaying logs from the systemd journal. It allows administrators to troubleshoot system issues, monitor service status, and review historical event data in a structured way.
Example 1: Basic Log Viewing
# View all journal entries, with the oldest logs shown first.
journalctl
Explanation
This is the most fundamental use of journalctl. It displays the entire systemd journal, allowing you to see all logged events from services and system processes. The output is paged, so you can scroll through the history.
Example 2: Real-time Log Monitoring
# Follow the journal in real-time to see new log entries as they happen.
journalctl -f
Explanation
The -f or --follow option provides a live view of the log files. This is invaluable for monitoring the impact of system changes or troubleshooting active issues as they occur.
Example 3: Filtering by a Specific Service
# Show all journal entries for the sshd service.
journalctl -u sshd.service
Explanation
Using the -u or --unit flag, you can isolate logs generated by a specific systemd unit, such as a service or target. This helps in diagnosing problems with a particular application or daemon.
Example 4: Displaying Kernel-specific Messages
# Display only messages originating from the kernel.
journalctl -k
Explanation
The -k or --dmesg option filters the journal to show only kernel ring buffer messages. This is crucial for diagnosing hardware, driver, or kernel-level issues.
Example 5: Viewing Logs Since a Specific Time
# Retrieve all logs generated since yesterday.
journalctl --since "yesterday"
Explanation
The --since option allows for time-based filtering, which is highly effective for investigating incidents that occurred within a known timeframe. You can use various time formats like "2025-06-24 14:00:00" or relative terms like "1 hour ago".
Example 6: Filtering by Message Priority
# Show all messages with a priority of error or higher.
journalctl -p err
Explanation
The -p or --priority flag filters logs by their severity level, ranging from emerg (0) to debug (7). This command isolates critical errors, warnings, and emergency messages, helping you focus on the most urgent system issues.
Example 7: Displaying Logs for a Specific User ID
# View all journal entries associated with user ID 1000.
journalctl _UID=1000
Explanation
You can filter journal entries by specific fields, such as _UID. This example shows all system activities and logs initiated by processes running under the user with UID 1000, which is useful for auditing user actions.
Example 8: Changing Output Format to JSON
# Display the last 5 journal entries in JSON format for parsing.
journalctl -n 5 -o json-pretty
Explanation
The -o or --output option changes the log format, and json-pretty provides a readable JSON output. This is extremely useful for exporting logs to be processed by other scripts or log analysis tools. The -n 5 option limits the output to the five most recent entries.