Skip to content
Advertisement

How does the command ‘echo “6” > /proc/sys/kernel/printk’ work?

Please see an example in How can I show printk() message in console?,

$ cat /proc/sys/kernel/printk
4 4 1 7
$ echo "6" > /proc/sys/kernel/printk
$ cat /proc/sys/kernel/printk
6 4 1 7

If the /proc/sys/kernel/prink was a normal file, the file would have changed to just “6”, a single number. But I know the proc file system is a file system in ram and I guess it works different. In what mechanism did the file changed that way? And if I wanted to change the content to 4 6 1 7, can I do it with echo command and redirection?

Advertisement

Answer

Filesystem entries under /proc behave somewhat like function calls. “Writing” a string to the file is like calling a function with the string as an argument. “Reading” from the file is like calling a function with no argument and getting back the return value. The behavior of each “function” is defined by the kernel (or at least, by the proc file system exposed by the filesystem entries).

See https://www.kernel.org/doc/html/latest/core-api/printk-basics.html for the details on how printk in particular works. In short, writing a number to the file changes the current logging level, while reading shows the current (if changed), default, minimum, and boot-time default logging levels.

8 People found this is helpful
Advertisement