Linux systems generate various log files to record system activity, errors, and security events. These log files are essential for system monitoring, troubleshooting, and security auditing.
Key Log Files
Log files are typically stored under the /var/log directory.
1. /var/log/messages (or syslog)
- Content: Records general system-wide information, kernel messages, messages from system daemons, hardware-related events, etc.
- Location: The filename varies by distribution.
- Red Hat-based (CentOS, Fedora):
/var/log/messages - Debian-based (Ubuntu):
/var/log/syslog
- Red Hat-based (CentOS, Fedora):
2. /var/log/secure (or auth.log)
- Content: Records authentication events and security-related information. Includes login attempts, sudo command executions, SSH connections, etc.
- Location:
- Red Hat-based:
/var/log/secure - Debian-based:
/var/log/auth.log
- Red Hat-based:
3. /var/log/wtmp
- Content: Records system login and logout history. Stored in binary format and cannot be read directly with a text editor.
- Viewing command:
lastThis command displays user login/logout history, system reboot history, etc.last
4. /var/run/utmp (or /var/log/utmp)
- Content: Records information about currently logged-in users. Also in binary format.
- Viewing commands:
who,w,userswho # List of currently logged-in users w # Currently logged-in users and the processes they are running users # Only the names of currently logged-in users
5. /var/log/lastlog
- Content: Records the last login time for each user. Also in binary format.
- Viewing command:
lastlogThis command displays the last login information for all users on the system.lastlog
Viewing and Managing Log Files
Log files grow over time and require regular management.
- Real-time monitoring: Using the
tailcommand with the-foption, such astail -f /var/log/syslog, displays content appended to the end of the log file in real time. - Searching: The
grepcommand can be used to search for lines containing specific keywords.grep "error" /var/log/messages - Paging: The
lessormorecommands can be used to browse long log files page by page.less /var/log/secure - Log rotation: The
logrotateservice periodically compresses, deletes, or rotates log files to manage disk space consumption. Configuration is done in/etc/logrotate.confand files within the/etc/logrotate.d/directory.
Understanding these log files and commands enables you to maintain Linux system health and respond quickly when issues arise.