71

I'd like to write a statement to dmesg. How can I do this?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
notlesh
  • 895
  • 1
  • 8
  • 11
  • 12
    Two reasons I've wanted to do this before: (1) to see what "now is" in dmesg-timestamp format, and (2) to know when I last looked at dmesg. – notlesh Apr 03 '12 at 20:39
  • 1
    I'm pretty sure `now` is seconds from boot, and I think it takes a kernel call to write to it so a userland program can't. – Kevin Apr 03 '12 at 21:22
  • 3
    @Kevin, so you know without looking how many seconds have elapsed since you booted? – notlesh Apr 04 '12 at 01:28
  • @Kevin it's the number of *microseconds* since boot, just formatted as seconds to make it easier to read for us humans. – Matthieu Aug 02 '18 at 12:10

6 Answers6

70

Write to /dev/kmsg (not /proc/kmsg as suggested by @Nils). See linux/kernel/printk/printk.c devkmsg_writev for the kernel-side implementation and systemd/src/journal/journald-kmsg.c server_forward_kmsg for an example of usage.

ephemient
  • 15,640
  • 5
  • 49
  • 39
  • It's not allowed on some system for non-root users (e.g.: Android kernel) :-(. – pevik Jan 07 '16 at 11:20
  • 25
    E.g. `date | sudo tee /dev/kmsg` – sanmai Apr 11 '16 at 03:46
  • Worth noting, that while this will make the message appear in `dmesg`, however over a `netconsole` debugging utility *(where you'd expect to see the same content)* you will not see the message until you increase `dmesg` debugging level. For example `sudo dmesg -n 8` works. – Hi-Angel Mar 26 '23 at 21:37
  • As a n00b, I was pleasantly surprised that you have only to put a colon in your message if you want `dmesg` to highlight the prefix: `echo "BLAH: This is your message!" | sudo tee /dev/kmsg`. – AbuNassar Jun 29 '23 at 17:55
20

For BSDs:

logger -p kern.notice MESSAGE

(courtesy Ian, freebsd-questions mailing list)

or other priorities.

For Linux:

su root -c 'echo MESSAGE > /dev/kmsg'
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
xitij
  • 381
  • 3
  • 6
1

Assuming nobody else comes up with an official way to do this ...

You can write a kernel module that calls the printk function. There's an example here that might just do the job for you.

ams
  • 5,707
  • 1
  • 19
  • 27
  • 10
    a. No need, others have written it already (e.g. [kecho](http://cgit.freedesktop.org/~halfline/kecho/)). b. **Really** no need for an extra module, see my answer. – ephemient Apr 03 '12 at 21:44
1

--> You may write a C program as below:

test_mod.c

#include <linux/module.h>
#include <linux/kernel.h>

int init_module(void)
{
    printk("Hello All\n This is a test init\n");
    return 0;
}

void cleanup_module(void)
{
    printk("Good Bye");
}

--> make object file:

echo "obj-m := test_mod.o" > Makefile

--> compile by running :

make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules`

--> load your module as below:

insmod ./test_mod.ko

see the output:

dmesg | tail

--> unload module:

rmmod test_mod.ko
Paulo Tomé
  • 3,754
  • 6
  • 26
  • 38
0
echo "Add your message here" | sudo tee /dev/kmsg

Verify by running dmesg -T

Sashank
  • 121
  • 4
  • 3
    This might have been better as an edit to [the accepted answer](https://unix.stackexchange.com/a/35647/116858), which already suggests writing to `/dev/kmsg`. – Kusalananda Jun 13 '22 at 11:27
-1

If I understood man dmesg correctly, you should be able to write to /proc/kmsg.

Rot-man
  • 143
  • 1
  • 6
Nils
  • 18,202
  • 11
  • 46
  • 82