30

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?

Anthon
  • 78,313
  • 42
  • 165
  • 222
miluz
  • 423
  • 1
  • 4
  • 5

3 Answers3

22

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
atevm
  • 287
  • 2
  • 13
goldilocks
  • 86,451
  • 30
  • 200
  • 258
  • 5
    +1 It is probably worth mention that dmesg uses a ring buffer so that it doesn't grow without bound and is held within the kernel so that messages can be logged prior to things like the filesystem is even up. – msw Sep 03 '13 at 10:42
6

If you use systemd then you can get all the information from the systemd journal using journalctl -k. syslog and rsyslog are not necessary if you use systemd.

Catalin Hritcu
  • 201
  • 2
  • 4
0

PopSicle does this I use the old msdos redirect and it is redricted into a .csv file which is opened in a spread sheet by LibreOffice Calc in terminal try something like this

dmesg > /path to where you want the file written/File-Name.csv 
dmesg > /media/joe/Data/Z-Back/Script-Files/Dmesg-Output.csv 

echo "Dmesg-to-CSV.sh"" the script file"

#!/bin/bash
echo "This is a shell script"  
SOMEVAR='I am done running dmesg and redirecting to /media/joe/Data/B-Back/Script-Files/Dmesg-Output.csv'  
echo "$SOMEVAR"  
dmesg > /media/joe/Data/Z-Back/Script-Files/Dmesg-Output.csv 

echo "Dmesg-CSV.desktop"" the icon file"

[Desktop Entry]
Encoding=UTF-8
Name=Dmesg-to-CSV.sh
Comment=Launch DirSyncPro
Exec=gnome-terminal -e /media/joe/Data/Z-Back/Script-Files/Dmesg-to-CSV.sh
Icon=utilities-terminal
Type=Application
Name[en_US]=Dmesg-CSV.desktop

echo "both the .sh file and .desktop file are stored in the same directory as the .csv output file"
rubynorails
  • 2,223
  • 12
  • 24
Joe
  • 1
  • 1