The kernel ring buffer
The kernel logs its messages with the printk function. The messages go into a ring buffer in memory, which has a fixed size. When the buffer is full, the oldest messages are overwritten. The buffer holds the boot messages, the device probes, the driver warnings, and the errors reported by the kernel. Because it lives in memory, it is cleared at reboot.
The dmesg command prints the buffer. Each line starts with a time stamp in seconds since boot, shown in square brackets. The time stamps are the most useful part when you correlate a kernel message with an event in a service log.
dmesg | head -n 3The first lines are the kernel version and the early boot messages. The exact text depends on the kernel, so the lab only checks that a version line exists. On some systems dmesg is restricted to root, and then it prints a permission error instead of the buffer. The restriction is controlled by a sysctl setting, and reading it is a separate question from reading the log.
Log files under /var/log
Services write to log files in /var/log, one file per service or one file for each facility. Some programs write their own files, and others hand their messages to a logging daemon such as syslogd or syslog-ng, which sorts them by facility and priority. The directory is therefore a good first place to look when you know which service failed.
Many log files grow without limit unless a rotation tool trims them. The logrotate program, which the course does not cover, is the usual tool for that job. When you read a log, start at the end, because the most recent entries describe the current problem.
ls -l /var/logThe listing shows the files and their sizes. A file that is very large or that has an old time stamp may be a problem, and the size alone is a useful first signal.
Reading a log safely
The tail command prints the last lines of a file, and the -f option follows the file as new lines arrive. The grep command finds lines that match a pattern. Combining the two lets you watch for errors while a service runs. Reading logs is a read-only task, so it is one of the safest things a user can do on a shared machine.
tail -n 5 /var/log/apk.logThe apk log records package operations. Even when a log is short, the last lines tell you what was done most recently. A log that is empty is also informative, because it means nothing has been written since the file was created.
Timestamps and time zones
Log lines often carry a time stamp, and the time stamp only makes sense with a time zone. Kernel time stamps are relative to boot, so they do not match wall-clock time. Log files usually use local time, and some systems write UTC. When you correlate events from two sources, convert them to the same clock first, and check whether a time zone change happened between the events.
When two logs disagree about the time of an event, check the boot time in the kernel log first. A clock that was set late at boot often explains a gap in the timestamps.
Logs also need care in what you write down. A log line can contain a user name, an address, a path, or a token that the program printed by mistake. When you copy a log into a bug report or a chat, check it first and remove anything that identifies a person or grants access. The same caution applies to the files you collect for later analysis, which should be stored with the same permissions as the originals.
Finally, a log is only useful if the service writes to it at the level you need. Many programs have a verbosity setting, and the default often hides the detail that explains a failure. Raise the level for one investigation, reproduce the problem, and then lower it again, so that the logs do not grow without limit or fill the disk during the investigation.
Where to look first
A short checklist helps when something is broken. Start with the kernel buffer for hardware and boot errors. Then read the log for the service that failed, from the end. Then check the disk, because a full disk breaks many services at once, and the next units of this module explain how to measure it. Finally, check the time stamps to put the events in order.
Permissions matter as much as location. A log file that a service writes is often readable only by its owner, and a user may see an empty result from a command that is simply not allowed to read the file. An empty output is therefore not proof that nothing happened. Check the permissions with ls -l before you conclude that a log is quiet, and remember that a permission error from a log reader is itself a useful clue.
Hands-on: collect the evidence
Save the first line of the kernel log and the list of log files in two files. The first file shows that the kernel log is readable, and the second shows what the system keeps.
dmesg | head -n 1 > dmesg-first.txt
ls /var/log > logs.txt
cat dmesg-first.txt logs.txtThe first file should contain a time stamp and the words Linux version. The second should list apk.log. You can use the same two commands on any Linux machine to collect a first set of facts about a problem.