Linux Log files and System messages


In Linux, log files and system messages are essential for monitoring, troubleshooting, and auditing system activity. These logs provide detailed information about system events, user activities, and system processes. Understanding where these logs are located, how to view them, and what information they contain is crucial for effective system administration.

Key Log Files in Linux

Log files are usually stored in the /var/log/ directory. Different logs contain different types of information, depending on the service or subsystem. Below is an overview of some common log files in Linux.


1. /var/log/syslog (General System Activity)

  • Description: The syslog file contains general system logs and messages from various system services. It is one of the most important logs for troubleshooting system-wide issues.
  • Used for: System startup messages, kernel messages, and messages from system services like networking, cron jobs, or hardware-related messages.

Example (viewing the contents):

cat /var/log/syslog

Sample Output:

Oct 6 09:22:16 servername systemd[1]: Starting Daily apt download activities... Oct 6 09:22:17 servername systemd[1]: Started Daily apt download activities. Oct 6 09:22:18 servername systemd[1]: Starting Daily apt upgrade and clean activities... Oct 6 09:22:18 servername systemd[1]: Started Daily apt upgrade and clean activities.
  • Explanation: The log shows timestamps, service names (systemd), and messages about various system services starting.

2. /var/log/messages (General System Messages)

  • Description: The messages file is similar to syslog and contains system-wide messages from different system services. It is often used by administrators to debug general system issues.
  • Used for: System startup, kernel-related messages, networking issues, etc.

Example (viewing the contents):

cat /var/log/messages

Sample Output:

Oct 6 09:22:16 servername kernel: [ 188.177276] eth0: link up, 1000 Mbps full duplex Oct 6 09:22:16 servername systemd[1]: Starting Network Service... Oct 6 09:22:17 servername systemd[1]: Started Network Service.
  • Explanation: The log shows messages related to the network interface (eth0) going up and the starting of the network service.

3. /var/log/auth.log (Authentication Logs)

  • Description: The auth.log file contains logs related to authentication and authorization, such as user login attempts, sudo usage, SSH logins, and other security-related events.
  • Used for: Tracking login attempts, sudo commands, and other security-sensitive events.

Example (viewing the contents):

cat /var/log/auth.log

Sample Output:

Oct 6 09:22:16 servername sshd[23567]: Accepted password for user from 192.168.1.1 port 22 ssh2 Oct 6 09:22:17 servername sudo[1234]: user : TTY=tty1 ; PWD=/home/user ; USER=root ; COMMAND=/bin/ls Oct 6 09:22:20 servername sshd[23567]: Received disconnect from 192.168.1.1 port 22:11: Bye Bye Oct 6 09:22:20 servername sshd[23567]: Disconnected from 192.168.1.1 port 22
  • Explanation: This log records an SSH login for user and a sudo command to list files. It also logs the disconnection of the SSH session.

4. /var/log/kern.log (Kernel Logs)

  • Description: The kern.log file contains messages generated by the kernel, including hardware-related messages, device drivers, and kernel warnings.
  • Used for: Troubleshooting hardware issues, kernel errors, and device driver issues.

Example (viewing the contents):

cat /var/log/kern.log

Sample Output:

Oct 6 09:22:16 servername kernel: [ 187.233455] ata1.00: failed to IDENTIFY (I/O error, err_mask=0x0) Oct 6 09:22:17 servername kernel: [ 188.144601] ata1.01: failed to IDENTIFY (I/O error, err_mask=0x0)
  • Explanation: These are kernel messages related to an I/O error with hard drives ata1.00 and ata1.01.

5. /var/log/daemon.log (Daemon Logs)

  • Description: This log contains messages related to system daemons (background services) running on the system. For example, it may include logs from services like cron, ntpd, or custom background processes.
  • Used for: Debugging or monitoring services and daemons running in the background.

Example (viewing the contents):

cat /var/log/daemon.log

Sample Output:

Oct 6 09:22:16 servername cron[12345]: (user) CMD (/usr/bin/python3 /home/user/script.py) Oct 6 09:22:18 servername ntpd[7890]: Soliciting pool server 0.centos.pool.ntp.org
  • Explanation: This log records a cron job being executed by the user user and an NTP daemon trying to synchronize the time with an NTP server.

6. /var/log/dmesg (Boot and Kernel Messages)

  • Description: The dmesg log stores messages generated by the kernel during the boot process. These messages are useful for diagnosing hardware and boot-related issues.
  • Used for: Debugging boot problems and examining kernel output, particularly hardware and driver issues.

Example (viewing the contents):

cat /var/log/dmesg

Sample Output:

[ 0.000000] Initializing cgroup subsys cpuset [ 0.000000] Initializing cgroup subsys cpu [ 0.000000] Initializing cgroup subsys cpuacct [ 0.000000] Linux version 5.4.0-42-generic (buildd@lamiak) (gcc version 8.4.0 (Ubuntu 8.4.0-3ubuntu2)) #46-Ubuntu SMP Fri May 8 20:37:58 UTC 2020
  • Explanation: This log contains the kernel's boot process messages, such as initializing cgroup subsystems and the Linux version being used.

7. /var/log/apt/ (APT Package Manager Logs)

  • Description: Logs related to package management activities performed by apt (such as installing, removing, and upgrading packages) are stored in /var/log/apt/.
  • Used for: Tracking package installations, upgrades, and removals.

Example (viewing the contents):

cat /var/log/apt/history.log

Sample Output:

Start-Date: 2024-10-06 09:00:42 Commandline: apt-get install nginx Install: nginx:amd64 (1.14.2-2ubuntu1) End-Date: 2024-10-06 09:01:10
  • Explanation: This log shows an apt-get installation of the nginx package, including the package version and installation time.

8. /var/log/boot.log (Boot Process Logs)

  • Description: The boot.log file contains logs related to the system's boot process. It is generated by the init system and shows messages about the services and components that started during boot.
  • Used for: Debugging issues related to system startup.

Example (viewing the contents):

cat /var/log/boot.log

Sample Output:

Starting LSB: Raise network interfaces... Starting OpenBSD Secure Shell server: sshd.
  • Explanation: This log shows the services that are started during the system boot, including network interfaces and SSH server.

Viewing Log Files in Real-Time

You can use tools like tail and less to view log files in real-time.

  • View last few lines of a log file:
tail -n 10 /var/log/syslog
  • Follow the log in real-time:
tail -f /var/log/syslog
  • View logs interactively:
less /var/log/syslog

Conclusion

Log files in Linux provide invaluable information for administrators to monitor system health, troubleshoot issues, and maintain system security. Understanding key log files like /var/log/syslog, /var/log/auth.log, /var/log/messages, and /var/log/kern.log can help identify problems ranging from hardware failures to security breaches.