Read Kernel Messages Safely with /proc/kmsg and dmesg
You will finish with a safe way to inspect the kernel message buffer, a permission check for /proc/kmsg, and a clear rule for when direct reads are inappropriate. The examples match the local proc_kmsg(5) page from Linux man-pages 6.7, with dmesg from util-linux 2.41.3.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about ten minutes. You need a shell and, for the actual message read, an account with the required elevated privileges. This guide only reads status and messages. It does not clear the buffer, change console logging, restart a logger or alter kernel settings.
1. Check the path without reading it
/proc/kmsg is a procfs file representing kernel messages. First confirm that the path exists and inspect its mode. This does not consume messages:
$ stat -c 'path=%n mode=%A owner=%U group=%G' /proc/kmsg
path=/proc/kmsg mode=-r-------- owner=root group=root
The mode shown above is an example of the usual arrangement, not a promise about every kernel or distribution. The useful facts are the path and whether your account can read it. If the command says that the file does not exist, check that procfs is mounted and that you are looking at the host you intended.
Checkpoint: run the following read-only test as your normal account:
$ test -r /proc/kmsg && printf '%s\n' 'readable' || printf '%s\n' 'not readable'
not readable
A non-readable result is common for an unprivileged account. It is not evidence that the kernel has no messages.
2. Prefer dmesg for an ordinary inspection
The manpage identifies dmesg(1) as the program used to retrieve information from /proc/kmsg. Use it for a one-off inspection instead of opening the proc file yourself:
$ sudo dmesg --kernel --nopager
[ 0.000000] Linux version ...
[ 0.123456] ... host-specific kernel messages ...
The lines depend on this machine, its boot, and its kernel. Do not compare the timestamps or message count with another host and assume that either system is broken. The command options used here are available in the installed util-linux build: --kernel selects kernel messages and --nopager keeps the output in the terminal.
Reading messages requires elevated privileges on systems that protect the kernel log. sudo is therefore shown explicitly. If your administrator has granted the necessary capability or relaxed the kernel's message restrictions, the same command may work without it. Do not add broad permissions to make a diagnostic command convenient.
3. Understand the single-reader boundary
Only one process should read /proc/kmsg at a time. This is the key operational trap: a monitoring process that opens the file directly can compete with another reader and make troubleshooting confusing. A system logger may already be collecting kernel messages through the syslog(2) system call facility.
Before experimenting with a direct reader, find out whether the host has a logging service responsible for kernel messages. The exact service name is distribution-specific, so inspect the active units rather than guessing:
$ systemctl --type=service --state=running --no-pager | grep -Ei 'syslog|rsyslog|syslog-ng|journald'
systemd-journald.service loaded active running Journal Service
No output is also a valid result. It only means that those names were not visible in the command's filtered output. It does not prove that no process is reading kernel messages.
Safety boundary
Do not run cat /proc/kmsg, tail -f /proc/kmsg or a similar long-lived reader on a production host merely to see what happens. It can compete with the existing logging arrangement, and it gives you no better first diagnostic than dmesg.
4. Use a direct permission check when a program needs the file
Some narrowly controlled diagnostics need to confirm that a service account can open the path. Test the permission without consuming messages:
$ sudo -u SERVICE_USER test -r /proc/kmsg
$ printf 'permission check: %s\n' "$?"
permission check: 0
Replace SERVICE_USER with the real account name. The sudo -u command itself normally needs administrator permission, and the account must exist. A status of 0 means the access check succeeded. A non-zero status means that the account cannot read the path, or that the path is unavailable. It does not justify changing ownership or mode bits.
If the service genuinely must read /proc/kmsg, stop here and document which existing reader it replaces. Do not add a second reader to work around a missing log record. Change the service configuration only through the host's normal change process, and make sure the previous reader is stopped or reconfigured first. This guide has no undo command for that change because it deliberately makes none.
5. Diagnose the common failures
When dmesg fails, capture the command's status immediately:
$ sudo dmesg --kernel --nopager
$ status=$?
$ printf 'dmesg exit status: %s\n' "$status"
dmesg exit status: 0
Do not treat the sample status as guaranteed. A non-zero status can reflect permissions, a restricted kernel log interface, or another local failure. Read the diagnostic text printed by your installed command, then check the path with stat and the active logging arrangement. If the path exists but direct access is denied, keep using the authorised dmesg or logging service interface rather than weakening the host's security settings.
If output is unexpectedly empty, check the command you ran and the host context first. Kernel messages are not a general application log, and a quiet buffer can simply mean that no messages matching the selected view are currently available. Avoid clearing the buffer as a test: clearing is a state-changing operation and is outside this workflow.
Done means
- You confirmed whether
/proc/kmsgexists without opening it as a reader. - You used
dmesgfor the normal kernel-message inspection. - You know that reading the proc file requires superuser privileges on the documented interface.
- You checked for an existing logger before considering any direct reader.
- You did not add a competing reader, clear the buffer or change permissions.