This question already has an answer here:

I'm trying to:

sudo echo "$USER: my message" > /dev/kmsg

to write a message to the dmesg log. This causes a 'permission denied' error, because it is redirecting the output of 'sudo' (while no longer privileged) to /dev/kmsg. So the 'echo' command is running as root and the writing to /dev/kmsg is running as me. Quoting 'echo' command and ther the redirect ('>/dev/kmsg') doesn't work because the redirection is a shell service that sudo doesn't understand. (The userid in question is in sudoers with NOPASSWD.)

Is there some Linux command that takes the file name as an option and writes its arguments out? Like: 'tee /dev/kmsg -- My message to be logged' I know about the 'logger' command, but that doesn't write to whatever log file 'dmesg' sees. (I want to intersperse some of my messages in with the boot process to know when certain things happen.)

This works, but has the problem of needing to have some place to write a file to:

echo "$USER: My msg" >/tmp/foo
sudo cp /tmp/foo /dev/kmsg
rm /tmp/foo

Solution from ctrl-alt-delor's comment below:

echo "my message" | sudo tee /dev/kmsg >/dev/null

Although it does use piping and redirection, it occurs in the unprivileged shell. Privileged access to '/dev/kmsg' occurs in the 'tee' command in 'sudo'. And it doesn't need write to a disk file (which could have problems).

Also @stolenmoments solution:

echo something | sudo dd of=/dev/kmsg

marked as duplicate by Jeff Schaller, Isaac, Wieland, Michael Homer, Christopher Jan 1 at 21:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Your "this works" example actually is using redirection which you say at the outset you do not wish to use. Also, are you sure you don't want to append to the existing log file with >>? – DopeGhoti Dec 31 '18 at 21:43
  • 2
    echo "message" | sudo tee «file-name» >/dev/null should do it. – ctrl-alt-delor Dec 31 '18 at 21:57
  • @DopeGhoti Yes, that is not an ideal solution. Also, that works because the redirection is not to the R/W only by root /dev/kmsg file, it is to an ordinary file. – Ribo Jan 1 at 0:05
  • @JeffSchaller Not a duplicate, the other question deals with writing a new program. – Ribo Jan 1 at 0:27

Putting aside the "why", any command that reads stdin and writes to a named output file will do. My favorite is "dd", but I'm old enough to have used what "dd" refers to.

echo something | sudo dd of=/dev/kmsg

should work just fine.


Use the logger command which is the shell's command-line interface to the syslog system log module. You can use the -p switch to alter which priority the log message is raised with, which in turn is used to determine into which log file the message is written. From the manual:

   -p, --priority priority
          Enter the message into the log with the specified priority.  The priority may be specified numerically or as a facility.level pair.  For example, -p local3.info logs the message as infor‐
          mational in the local3 facility.  The default is user.notice.
  • I tried 'logger -p kern.info ...' and it didn't make it into the output of dmesg (I don't know where dmesg gets its stuff from, I think from the kernel's internal log). It did get written to syslog, messages, and user.log. – Ribo Jan 1 at 0:15

I know you asked for something without shell redirection, but you could make your sudo command work by changing it to the following:

sudo su -c 'echo "$USER: my message" > /dev/kmsg'

  • True, but there's still something unseemly about blowing out the kernel ring buffer. – DopeGhoti Dec 31 '18 at 21:50
  • @DopeGhoti: I agree. I don't know if it's considered "correct", but figured I would throw it out there if someone was wondering how to get redirection to work within sudo. – Peschke Dec 31 '18 at 21:53
  • Agreed, not 'correct'. This is just for debug/research -- I want to be able to intersperse some messages into the dmesg log at various points between other kern activity so I can see the order in which various events occur. Logging it elsewhere won't easily give me the chronology of the the events. – Ribo Jan 1 at 0:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.