Better dmesg in five minutes
How to handle dmesg on a modern computer


I last wrote about dmesg in 2016. It has mostly not changed since then, but I’ve changed my setup slightly. Here’s a short article about what I did so that you can do it too, and so that I can remember for the next time.


Previously:

Previous I had a bash alias which looked like this:

alias dmesg='dmesg --follow || dmesg'

since some machines didn’t yet support the --follow flag.


Currently:

Current machines disable reading dmesg if you’re not root. This is for security. There’s a sysctl flag you can tweak to bypass this for single user machines where you’re not worried about this risk.

cat /proc/sys/kernel/dmesg_restrict

If it’s 1, then you need to be root. 0 means non-root users can use dmesg.

Trying to make this article more eye catching and fun!


Better dmesg:

Let’s fix this permanently so I don’t need to keep finding this workaround. I’ve now got ~/bin/dmesg setup as:

#!/bin/bash

if [ $(cat /proc/sys/kernel/dmesg_restrict) = "1" ]; then
	echo 'dmesg is currently restricted, enter password to enable for user'
	sudo bash -c 'echo 0 > /proc/sys/kernel/dmesg_restrict'
	sudo bash -c 'echo "kernel.dmesg_restrict = 0" > /etc/sysctl.d/10-dmesg.conf'
fi

/usr/bin/dmesg --human --follow "$@"

This checks and adds the needed permissions if they’re not already set, and it also runs dmesg with my two favourite flags: --follow and --human.

The manual page will help you with anything else you need.


Conclusion:

As with last time, try out this snippet!

$ sudo bash -c 'echo The Technical Blog of James is awesome! > /dev/kmsg'

Happy Hacking,

James


You can follow James on Mastodon for more frequent updates and other random thoughts.
You can follow James on Twitter for more frequent updates and other random thoughts.
You can support James on GitHub if you'd like to help sustain this kind of content.
You can support James on Patreon if you'd like to help sustain this kind of content.

November 17, 2024
317 words


Categories
Tags
bash dmesg linux planetfedora sysctl

Links...


Comments

Nothing yet.


Post a comment



(sorry but the spammers were getting too crazy!)

Thank you

Your comment has been submitted and will be published if it gets approved.

Click here to see the patch you generated.

OK