Linux – How to dmesg content be logged into a file

dmesglinuxsyslog

I'm running a Linux OS that was built from scratch. I'd like to save the kernel message buffer (dmesg) to a file that will remain persistent between reboots.

I've tried running syslogd but it just opened a new log file, /var/log/messages, with neither the existing kernel message buffer, nor any new messages the kernel generated after syslogd was launched.

How can the kernel message buffer be saved to a persistent log file?

Best Answer

You need to look at either /etc/rsyslog.conf or /etc/syslog.conf. If you have a line early on such as:

*.*                -/var/log/syslog

Everything, including the stuff from dmesg, should go to that file. To target it better:

kernel.*           -/var/log/dmesg

If that fails for some reason, you could periodically (e.g. via cron):

dmesg > /var/log/dmesg

Depending on how big the dmesg buffer is (this is compiled into the kernel, or set via the log_buf_len parameter) and how long your system has been up, that will keep a record of the kernel log since it started.

If you want to write the dmesg output continuously to a file use the -w (--follow) flag.

dmesg --follow > mydmesg.log
Related Question