Linux prevent unprivileged users from viewing dmesg command output

last updated in Categories , , , , ,

I want to restrict both normal users and root user inside LXD container “hypervisor” from viewing and using dmesg command output. How do I prevent non-root (unprivileged) Linux users from viewing the Linux kernel’s log buffer? Can Linux prevent unprivileged users from viewing dmesg command output?

The kernel ring buffer is nothing but a data structure that shows messages related to the operation of the Linux kernel. Like any other buffer, it always s always a constant size, removing the oldest messages when new messages come in.

How to view dmesg command output on Linux

One can use the dmesg command to see or control the kernel ring buffer. To display all messages from the kernel ring buffer just type the dmesg command:
$ dmesg
$ sudo dmesg
Sample outputs:

Fig.01: dmesg command displaying the Linux kernel ring buffer
Fig.01: dmesg command displaying the Linux kernel ring buffer

It is possible to see more human readable output with the following command:
$ sudo dmesg -H --color

How do I restrict unprivileged access to kernel syslog?

Run the following sysctl command as root user:
$ sudo sysctl -w kernel.dmesg_restrict=1

kernel.dmesg_restrict = 1

To view value, run:
$ sysctl kernel.dmesg_restrict
From the Linux kernel docs:

This toggle indicates whether unprivileged users are prevented from using dmesg(8) to view messages from the kernel’s log buffer. When dmesg_restrict is set to (0) there are no restrictions. When dmesg_restrict is set set to (1), users must have CAP_SYSLOG to use dmesg(8). The kernel config option CONFIG_SECURITY_DMESG_RESTRICT sets the default value of dmesg_restrict.

To make changes permanent add entry to /etc/sysctl.conf file using the sudo and tee command on Linux:

echo 'kernel.dmesg_restrict=1' | sudo tee -a /etc/sysctl.conf

Now unprivileged users will be greeted with the following error message when they run
$ dmesg
Sample outputs:

dmesg: read kernel buffer failed: Operation not permitted

Another session from LXC/KVM container:
Linux prevent unprivileged users from viewing dmesg output with sysctl

Why restrict access to to kernel syslog?

From the lwn mailing list:

The kernel syslog contains debugging information that is often useful during exploitation of other vulnerabilities, such as kernel heap addresses. Rather than futilely attempt to sanitize hundreds (or thousands) of printk statements and simultaneously cripple useful debugging functionality, it is far simpler to create an option that prevents unprivileged users from reading the syslog.

This patch, loosely based on grsecurity’s GRKERNSEC_DMESG, creates the dmesg_restrict sysctl. When set to “0”, the default, no restrictions are enforced. When set to “1”, only users with CAP_SYS_ADMIN can read the kernel syslog via dmesg(8) or other mechanisms.

How do I clear the ring buffer?

Type the following command:
$ sudo dmesg --clear
You can also clear the ring buffer after first printing its contents on screen:
$ sudo dmesg --read-clear

How can I disable the printing of messages to the console?

For security reason or to avoid too much message on the console, run:
$ sudo dmesg --console-off
To turn it on again pass the --console-on option:
$ sudo dmesg --console-on

How do I show kernel messages only?

$ sudo dmesg --kernel
To just display userspace messages, run:
$ sudo dmesg --userspace
I suggest you read dmesg man page or type the following command:
$ dmesg -h

 dmesg [options]
Display or control the kernel ring buffer.
 -C, --clear                 clear the kernel ring buffer
 -c, --read-clear            read and clear all messages
 -D, --console-off           disable printing messages to console
 -E, --console-on            enable printing messages to console
 -F, --file <file>           use the file instead of the kernel log buffer
 -f, --facility <list>       restrict output to defined facilities
 -H, --human                 human readable output
 -k, --kernel                display kernel messages
 -L, --color[=<when>]        colorize messages (auto, always or never)
                               colors are enabled by default
 -l, --level <list>          restrict output to defined levels
 -n, --console-level <level> set level of messages printed to console
 -P, --nopager               do not pipe output into a pager
 -r, --raw                   print the raw message buffer
 -S, --syslog                force to use syslog(2) rather than /dev/kmsg
 -s, --buffer-size <size>    buffer size to query the kernel ring buffer
 -u, --userspace             display userspace messages
 -w, --follow                wait for new messages
 -x, --decode                decode facility and level to readable string
 -d, --show-delta            show time delta between printed messages
 -e, --reltime               show local time and time delta in readable format
 -T, --ctime                 show human readable timestamp (may be inaccurate!)
 -t, --notime                don't print messages timestamp
     --time-format <format>  show time stamp using format:
Suspending/resume will make ctime and iso timestamps inaccurate.
 -h, --help     display this help and exit
 -V, --version  output version information and exit
Supported log facilities:
    kern - kernel messages
    user - random user-level messages
    mail - mail system
  daemon - system daemons
    auth - security/authorization messages
  syslog - messages generated internally by syslogd
     lpr - line printer subsystem
    news - network news subsystem
Supported log levels (priorities):
   emerg - system is unusable
   alert - action must be taken immediately
    crit - critical conditions
     err - error conditions
    warn - warning conditions
  notice - normal but significant condition
    info - informational
   debug - debug-level messages
For more details see dmesg(1).

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Notable Replies

  1. You must access root privilege to see kernel log
    Regular user can’t see log of kernel

Continue the discussion


Historical Comment Archive

5 comment

  1. “echo ‘kernel.dmesg_restrict=1’ | sudo tee -a /etc/sysctlc.onf”

    At the end it should read “”sysctl.conf.

    Still, have a question? Get help on our forum!