This guide teaches how to locate and analyze system log files on a Linux system for effective server management.
In this guide, you will learn how to locate and analyze system log files on a Linux system. Logging is a critical part of managing any Linux server since logs offer detailed insights into system activities—covering events such as user actions, system errors, and service operations. These logs are written as text messages, making them easy to search, read, and troubleshoot.
The Linux kernel and many applications generate status messages, errors, and warnings, which are stored in log files. Because multiple programs continuously generate these messages, logging daemons are used to collect and organize information into centralized log files. The most common logging daemon on Linux is rsyslog (Rocket Fast System for Log Processing). By default, rsyslog stores log files in the /var/log directory. These plain text files can be easily explored using text-search tools like grep.Below is an example of listing the files in the /var/log directory:
Most log files under /var/log are restricted to the root user. If you need to view these logs as a non-root user, consider switching to root using commands like su --login or sudo --login.
If you are unsure where specific logs are stored—such as SSH logs that give details about login attempts—you can search all log files for entries related to the SSH daemon (sshd):
Copy
Ask AI
$ grep -r 'ssh' /var/log/
The output might look like this:
Copy
Ask AI
/var/log/auth.log:Mar 3 03:32:37 kodekloud sshd[1653]: Connection closed by authenticating user aaron 10.11.12.1 port 57660/var/log/auth.log:Mar 3 03:32:39 kodekloud sshd[1655]: Accepted password for aaron from 10.11.12.1 port 52560 ssh2/var/log/auth.log:Mar 3 03:32:39 kodekloud sshd[1655]: pam_unix(sshd:session): session opened for user aaron(uid=1000) bygrep: /var/log/private: Permission denied/var/log/installer/installer-journal.txt:Jun 30 12:18:56 ubuntu-server sshd[1409]: Server listening on 0.0.0.0 port 22....
This indicates that SSH logs are primarily found in /var/log/auth.log. You can open this file using an editor like Vim or a pager such as less to search for additional SSH-specific details.Example output from /var/log/auth.log:
Copy
Ask AI
$ less /var/log/auth.logMar 3 03:21:24 kodekloud sshd[1501]: Accepted password for aaron from 10.11.12.1 port 56862 ssh2Mar 3 03:32:34 kodekloud sshd[1653]: Failed password for aaron from 10.11.12.1 port 57660 ssh2Mar 3 03:32:53 kodekloud sudo: aaron : TTY=pts/0 ; PWD=/home/aaron; USER=root ; COMMAND=/usr/bin/apt updateMar 3 03:37:30 kodekloud passwd[2129]: pam_unix(passwd:chauthtok): password changed for aaron
Another key log file is /var/log/syslog, which includes general system messages:
Older log files often have suffixes such as .1 or are compressed with .gz, while the uncompressed file (for example, auth.log) contains the latest entries.
When you need to debug an application or monitor system changes as they happen, you can use the tail command with the -f option to follow a log file in real time. For example:
Copy
Ask AI
$ tail -F /var/log/auth.logMar 3 03:32:53 kodekloud sudo: aaron : TTY=pts/0 ; PWD=/home/aaron ; USER=root ; COMMAND=/usr/bin/apt updateMar 3 03:32:53 kodekloud sudo: pam_unix(sudo:session): session opened for user root (uid=0) by aaron(uid=1000)Mar 3 03:32:58 kodekloud sudo: pam_unix(sudo:session): session closed for user rootMar 3 03:37:30 kodekloud passwd[2129]: pam_unix(passwd:chauthtok): password changed for aaron...
Press Ctrl+C to exit the follow mode.To filter the live output for specific entries, such as those related to sudo, you can pipe the output through grep:
Modern Linux systems that use systemd employ the journal daemon for structured log management. The journalctl command provides powerful options to filter and analyze logs.
To view logs for a specific command, such as sudo, first determine its full path:
Copy
Ask AI
$ which sudo/usr/bin/sudo
Then filter the journal logs associated with it:
Copy
Ask AI
$ journalctl /usr/bin/sudo
This command opens the log output in your default pager (typically less), allowing you to navigate and search through the logs. Press q to exit the pager.
Use the -p option with journalctl to filter log output based on priority levels. Available priorities include: emerg, alert, crit, err, warning, notice, info, and debug. For example, to display only error messages:
Copy
Ask AI
$ journalctl -p errFeb 08 21:09:19 kodekloud systemd[1]: multipathd.socket: Socket service already active, refusing.Feb 08 21:09:19 kodekloud systemd[1]: Failed to listen on multipathd control socket.-- Boot 35a9a34be95e43cb85c097ecdd0afa4d --Mar 03 00:33:14 kodekloud systemd[1]: Failed to start Refresh fwupd metadata and update motd.
If you forget the priority names, type journalctl -p (with a trailing space) and press Tab twice to list all available options.
You can combine filters with regular expressions. For example, to display only log entries with info priority where messages start with the letter “b”, use:
For previous boot sessions, use a negative offset (e.g., -b -2 for two boots ago). Note that some systems might only store logs in memory, so persistent logging may not be available unless enabled. To enable persistent logging, create the directory:
Copy
Ask AI
$ sudo mkdir /var/log/journal/
If you run a journalctl command and see no output, try using sudo or check that your user has the appropriate permissions.
Reviewing user login history is straightforward with the last command, which displays recent logins in reverse chronological order:
Copy
Ask AI
$ lastaaron pts/0 10.11.12.1 Sun Mar 3 23:15 - 23:15 still logged inreboot system boot 5.15.0-97-generic Sun Mar 3 23:12 - 23:12 still runningaaron tty1 Sun Mar 3 04:14 - 04:22 (00:08)...
The lastlog command provides a summary of the last login times for each user and can include the originating IP address for remote logins (such as SSH):
In this article, we explored how to locate and analyze system log files in Linux. We examined where logs are stored (mostly under /var/log), how to view and filter logs using tools like grep, tail, and journalctl, and how to review user login history with commands such as last and lastlog. By mastering these tools, you can efficiently diagnose issues, monitor system activities, and ensure that your Linux server is running smoothly.Happy logging!