$_ CLI In Depth
Learning progress 0%
lesson packages services cron

System Logs & dmesg

5 min read Module 11: Packages, Services & Scheduled Jobs Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:14

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.

BASH1 lines
dmesg | head -n 3

The 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.

BASH1 lines
ls -l /var/log

The 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.

BASH1 lines
tail -n 5 /var/log/apk.log

The 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.

Tip

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.

BASH3 lines
dmesg | head -n 1 > dmesg-first.txt
ls /var/log > logs.txt
cat dmesg-first.txt logs.txt

The 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.

Terminal lab · 2 steps · real Linux in your browser

Read the kernel log and the log directory

Save the first line of the kernel ring buffer, then list the log directory.

01
Save the first kernel message
The first line of dmesg usually names the kernel version. Save it in dmesg-first.txt. ``bash dmesg | head -n 1 > dmesg-first.txt `` The line starts with a time stamp in brackets and includes the words Linux version.
Show hint for step 1
Pipe dmesg to head -n 1 and redirect the result.
02
List the log directory
List /var/log and save the names in logs.txt. The apk log is there on this machine. ``bash ls /var/log > logs.txt `` Open logs.txt to see the names.
Show hint for step 2
Use ls on /var/log and redirect the output.
Check yourself Question 1 of 4
Concept Check Kernel message buffer recall

Which command prints the kernel ring buffer that holds the kernel messages?

Recap & Key Takeaways

The kernel writes its messages to a fixed-size ring buffer that dmesg prints, and the buffer is cleared at reboot.
Services write log files under /var/log or to a journal, and the most recent lines near the end describe the current problem.
Kernel time stamps are relative to boot, so convert all sources to one clock before you correlate events.
Reading logs is read-only, which makes tail, grep, and dmesg safe first steps on a shared machine.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue