How can I write to dmesg from command line?
Solution 1
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.
Solution 2
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'
Solution 3
--> 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
Solution 4
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.
Related videos on Youtube
notlesh
Full-stack software engineer with 10+ years of programming experience, offering a diverse range of engineering talent. Extensive experience designing C++ products on embedded Linux systems. Comfortable with entire product lifecycle from design to support.
Updated on September 18, 2022Comments
-
notlesh almost 2 years
I'd like to write a statement to dmesg. How can I do this?
-
Admin about 12 yearsTwo 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.
-
Admin about 12 yearsI'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. -
Admin about 12 years@Kevin, so you know without looking how many seconds have elapsed since you booted?
-
Admin almost 6 years@Kevin it's the number of microseconds since boot, just formatted as seconds to make it easier to read for us humans.
-
-
notlesh about 12 years# echo "test" >> /proc/kmsg [yields =>] -su: echo: write error: Input/output error
-
ephemient about 12 yearsa. No need, others have written it already (e.g. kecho). b. Really no need for an extra module, see my answer.
-
pevik over 8 yearsIt's not allowed on some system for non-root users (e.g.: Android kernel) :-(.
-
sanmai about 8 yearsE.g.
date | sudo tee /dev/kmsg
-
Admin about 2 yearsThis might have been better as an edit to the accepted answer, which already suggests writing to
/dev/kmsg
.