• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Dmesg Command in Linux

Written by Admin, Updated On September 4, 2020
dmesg, kernel, terminal
Dmesg Command in Linux

The dmesg command-line utility is used to view and control the kernel ring buffer in Linux and other Unix-like operating systems. It is useful for examining kernel boot messages and debugging hardware related issues. In this guide, we will show you the basics of the dmesg command.

In the Linux operating system the Linux kernel is the core that manages the system resources. The kernel writes various messages to the kernel ring buffer during the boot process, and when the system is running. These messages include various information about the operation of the system.

Using the dmesg Command#

The basic syntax for the dmesg command is as following:

dmesg [OPTIONS]

If you run the dmesg without any options it will writes all messages from the kernel ring buffer to the standard output:

dmesg

In some of systems the root level users only can run the dmesg command and in some of system it allows users to run dmesg command. If the user have not permission then following error message will be displayed:

dmesg: read kernel buffer failed: Operation not permitted

You also can manage the restriction that the non-root user can use the dmesg command or not by kernel parameter kernel.dmesg_restrict. You should set it zero to remove the restrictions:

sudo sysctl -w kernel.dmesg_restrict=0

There will many lines in output so the last few lines are viewable on terminal. You can pipe the output with less utility:

dmesg --color=always | less

The --color=always is used to preserve the colored output.

You also can use the grep command to filter the output. For example, to view only the USB related messages, you would type:

dmesg | grep -i usb

dmesg reads the messages generated by the kernel from the /proc/kmsg virtual file. This file provides an interface to the kernel ring buffer and can be opened only by one process. If syslog process is running on your system and you try to read the file with cat , or less, the command will hang.

The syslog daemon dumps kernel messages to /var/log/dmesg, so you can also use that log file:

cat /var/log/dmesg

Formating dmesg Output#

As we seen previously the output of dmesg have the lots of lines. The dmesg command provides number of options that help to format and filter the output.

The -H (--human) is the commonly used option, which show the human readable output.

dmesg -H

To print human-readable timestamps use the -T (--ctime) option:

dmesg -T
[Fri Sep 4 10:20:04 2020] IPv6: ADDRCONF(NETDEV_CHANGE): wlp1s0: link becomes ready

The timestamps format can also be set using the –time-format option, which can be ctime, reltime, delta, notime, or iso. For example to use the delta format you would type:

You also can set the timestamps using the --time-format option. To use delta format, type:

dmesg --time-format=delta

You also can use the two options simultaneously:

dmesg -H -T

To watch the output of the dmesg command in real-time use the -w (--follow) option:

dmesg --follow

Filtering dmesg Output#

The dmesg supports the following log facilities:

  • kern – kernel messages
  • user – user-level messages
  • mail – mail system
  • daemon – system daemons
  • auth – security/authorization messages
  • syslog – internal syslogd messages
  • lpr – line printer subsystem
  • news – network news subsystem

Using the -f (--facility ) option you can limit the output to specific facilities. The option accepts one or more comma-separated facilities.

For example, to display only the kernel and system daemons messages you would use:

dmesg -f kern,daemon

Each log message is associated with a log level that shows the importance of the message. dmesg supports the following log levels:

  • emerg – system is unusable
  • alert – action must be taken immediately
  • crit – critical conditions
  • err – error conditions
  • warn – warning conditions
  • notice – normal but significant condition
  • info – informational
  • debug – debug-level messages

The -l (--level ) option restricts the output to defined levels. The option accepts one or more comma-separated levels.

The following command displays only the error and critical messages:

dmesg -l err,crit

Clearing the Ring Buffer#

Option -C (--clear) allows you to clear the ring buffer:

sudo dmesg -C

Make sure to clear the ring buffer you should logged in as root or user with sudo privileges.

To print the buffer contents before clearing use the -c (--read-clear) option:

sudo dmesg -c

If you want to save the current dmesg logs in a file before clearing it, redirect the output to a file:

dmesg > dmesg_messages

Conclusion#

The dmesg command allows you to view and control the kernel ring buffer. Type man dmesg in your terminal for information about all available dmesg options.

If you have any questions or feedback, feel free to leave a comment.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Copy, Cut and Paste in Vim / Vi
Next Article   How to Install Chromium Web Browser on Ubuntu 20.04

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook