su command (and providing the root password) or with sudo --login:
Keep in mind that switching to the root user helps you access all log files, but also demands careful handling to avoid unintended modifications.
Analyzing Specific Log Files
Suppose you need to determine where SSH login events are recorded. SSH-related entries are typically found in the/var/log/secure file. You can search the entire /var/log directory for the keyword “ssh” as follows:
/var/log/secure, you can view this file using a pager like less:
/var/log/messages. Additionally, you may come across rotated logs that include date information (for example, boot.log-20211026), which are archives of older log entries.
Below is an example showing how to view both secure and messages logs:
boot.log-20211026, indicates that the logs in the current /var/log/boot.log were archived on October 26, 2021, preserving past log history.
Following Live Log Outputs
When troubleshooting an application or monitoring system activity, you might want to view log entries as they are recorded. Thetail command with the -F flag enables follow mode, which displays new log entries in real time. To exit follow mode, simply press Control+C.
Using journalctl for Enhanced Log Analysis
Modern Linux systems use the systemd journal to manage structured logs. Thejournalctl command offers numerous powerful options for filtering and displaying logs.
Viewing Logs for a Specific Command
First, determine the full path of a command (e.g., sudo):journalctl without options displays all logs. During active troubleshooting, jump to the end of the log by using the -e flag:
-f flag and use Control+C to exit:
Filtering Logs by Priority
Logs are tagged with priorities such as debug, info, notice, warning, error, crit, alert, and emerg. To view only the error-level messages, use the-p option like this:
-g flag. For example, to show info-level log messages beginning with the letter “B”, use:
Filtering by Time
You can restrict your log output to a specific time window using the-S (since) and -U (until) options. For example, to view logs recorded between 1 a.m. and 2 a.m.:
Viewing Logs from a Specific Boot
Often, you may want to see only the logs from the current boot session. Use the-b option with a boot offset, where 0 refers to the current boot:
-b -1. Note that on systems like CentOS the journal may be stored in memory by default, so persistent storage must be configured if you require logs from previous boots:
Before configuring persistent journals, ensure you understand the disk space implications and security policies of your environment.
Viewing Login History
To review user login history, you can use thelast command. This command displays recent session information, with the newest entries at the top, including system reboots. For example:
lastlog command displays the most recent login for each user, including remote SSH login details: