How can I write to dmesg from command line?

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.


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'

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.