Putting custom message to kernel printk buffer

We have servers with high uptime values. I can see several errors and warnings when I look at the kernel message buffer with dmesg command. Newer kernels supports printing monolithic time information for every lines in kernel message buffer which starts from 0 when system is power up.

But when the server is up for a long time, it is hard to understand that whether a printk time with a value of 30 million is closed to today or not. So, is there any way to put a custom message into kernel buffer with current monolithic time?

There is a special character device named /dev/kmsg in Linux which provides userspace access to kernel’s printk buffer.

You can write to /dev/kmsg (you need write permission) like that:

echo "Test message" > /dev/kmsg

After the command you see the “Test message” with monolithic time on left in dmesg output.

It is also valid to read from /dev/kmsg device file, you can read it from your program.

Please note that, printk buffer implemented as a ring buffer and it is generally limited with 128Kb. You can also change this limit on kernel compilation.