The output of the dmesg
command in FreeBSD
isn’t very user-friendly. Additionally, it has a significant limitation: we can’t determine the exact time each event was recorded. In this tutorial, we’ll work around this by enabling timestamps and converting them to human-readable dates.
In the Linux
version of dmesg
, at least we have the -T
parameter to view dates directly, but in FreeBSD
, this parameter simply doesn’t exist:
dmesg -T
dmesg: illegal option -- T
usage: dmesg [-ac] [-M core [-N system]]
First, we need to enable timestamp logging at the kernel level:
echo 'kern.msgbuf_show_timestamp=1' >> /etc/sysctl.conf
sysctl kern.msgbuf_show_timestamp=1
kern.msgbuf_show_timestamp: 0 -> 1
And reboot:
shutdown -r now
The timestamps will appear as seconds since system boot:
[30] CPU: Intel(R) Core(TM)2 Duo CPU P9500 @ 2.53GHz (2533.32-MHz K8-class CPU)
[30] Origin="GenuineIntel" Id=0x10676 Family=0x6 Model=0x17 Stepping=6
[30] Features=0xbfebfbff<FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CLFLUSH,DTS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE>
[30] Features2=0x8e3fd<SSE3,DTES64,MON,DS_CPL,VMX,SMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,SSE4.1>
[30] AMD Features=0x20100800<SYSCALL,NX,LM>
[30] AMD Features2=0x1<LAHF>
[30] VT-x: HLT,PAUSE
[30] TSC: P-state invariant, performance statistics
[30] wlan0: Ethernet address: 00:22:5f:62:3e:68
[30] nfe0: link state changed to UP
[30] lo1: changing name to 'bastille0'
[30] lo0: link state changed to UP
[30] nfe0: link state changed to DOWN
[31] pflog0: promiscuous mode enabled
[32] Security policy loaded: MAC/ntpd (mac_ntpd)
[33] nfe0: link state changed to UP
To convert the timestamps to human-readable dates, we’ll use the following script, which depends on ccze
. Let’s install it first:
pkg install ccze
vi .scripts/dmesg-human.sh
#!/bin/sh
# Get system boot time (Unix timestamp)
boot_time=$(sysctl -n kern.boottime | awk '{print $4}' | tr -d ',')
# Process dmesg output
dmesg | awk -v bt="$boot_time" '
# Preserve boot headers and copyright lines unchanged
/^---<<BOOT>>---/ || /^Copyright/ || /^FreeBSD/ { print; next }
# Convert timestamped lines (e.g., "[8] message")
/^\[[0-9]+\]/ {
gsub(/\[|\]/, "", $1); # Remove brackets from timestamp
ts = $1; # Extract timestamp
$1 = ""; # Remove timestamp from line
# Convert to human-readable time
cmd = "date -j -f %s " (bt + ts) " \"+%Y-%m-%d %H:%M:%S\" 2>/dev/null"
cmd | getline human_time
close(cmd)
printf "[%s]%s\n", human_time, $0
next
}
# Print all other lines as-is
{ print }
' | ccze -A
Set the necessary permissions:
chmod 700 .scripts/dmesg-human.sh
When we run the script, we’ll see much more user-friendly output:
.scripts/dmesg-human.sh
DMESG | DMESG-Script |
---|---|
![]() |
![]() |