CodeBlanks Logo Icon CodeBlanks

dmesg and logs

Learn kernel logs with dmesg.

dmesg and kernel logs

The dmesg command reads the kernel message buffer and prints it. Hardware discovery, driver loading and error messages from boot onward accumulate here.

The kernel writes logs with printk; each message is tagged with a priority level. KERN_EMERG is highest, KERN_DEBUG is lowest. User space keeps similar records through syslog and /var/log files. Logs are critical for both debugging and auditing; dmesg -T shows human-readable timestamps.

Priority levels and filtering

Kernel logs have eight levels: KERN_EMERG (0) is highest, KERN_DEBUG (7) is lowest. The buffer is bounded; new messages push out old ones.

You can filter with dmesg --level=err to see only errors. In printk a prefix sets the level: printk(KERN_ERR "error\n"). Drivers choose levels carefully; noisy logs overflow the buffer. console_loglevel controls which levels reach the console.

Practice Print log levels

Define void print_level(const char *name, int level) with body printf("[%d] %s\n", level, name);. In main call it with ("KERN_EMERG", 0), ("KERN_ALERT", 1), ("KERN_ERR", 3), ("KERN_INFO", 6).

Starter code

main.c
#include <stdio.h>

// Define print_level function
// Call it for KERN_EMERG, KERN_ALERT, KERN_ERR, KERN_INFO

int main(void) {
    return 0;
}

Sample solution

main.c
#include <stdio.h>

void print_level(const char *name, int level) {
    printf("[%d] %s\n", level, name);
}

int main(void) {
    print_level("KERN_EMERG", 0);
    print_level("KERN_ALERT", 1);
    print_level("KERN_ERR", 3);
    print_level("KERN_INFO", 6);
    return 0;
}

Expected output

[0] KERN_EMERG
[1] KERN_ALERT
[3] KERN_ERR
[6] KERN_INFO

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which command shows kernel logs?

  • dmesg Correct answer
  • lsmod
  • ps
  • top

Explanation: dmesg reads the kernel message buffer.

2 Match log levels

Term Meaning
KERN_EMERG System crash
KERN_ALERT Act immediately
KERN_ERR Error condition
KERN_INFO Informational

3 Complete the printk level prefix

Question
printk(KERN_____ "driver loaded
");
Answer
printk(KERN_INFO "driver loaded
");

Explanation: KERN_INFO tags informational messages.

All lessons in Expert