dmesg コマンド:システム起動時のメッセージ

コマンドの概要

 dmesgコマンドは、Linuxカーネルが起動中に生成されるメッセージを表示するために使用されます。これには、ハードウェア検出、デバイスドライバの読み込み、システムの初期化などが含まれます。メッセージバッファーの保存領域が決まっており、バッファーサイズを超過するとメッセージデータは先頭から上書きされていきます。

コマンドの主なオプションと説明

オプション説明
-c読み込んでからすべてのメッセージを消去します。rootユーザーで実行する。
-kカーネルメッセージを表示します。
-l, –level指定されたレベルのメッセージのみを出力します。
-n, –console-level <level>コンソールに出力すべきメッセージのレベルを指定します。
-s, –buffer-sizeカーネルのリングバッファに問い合わせる際のバッファサイズを指定します。
-Tタイムスタンプを人間が読みやすい形式で表示する。
主なオプションと説明
# dmesg --help

Usage:
 dmesg [options]

オプション:
 -C, --clear                 clear the kernel ring buffer
 -c, --read-clear            read and clear all messages
 -D, --console-off           disable printing messages to console
 -d, --show-delta            show time delta between printed messages
 -e, --reltime               show local time and time delta in readable format
 -E, --console-on            enable printing messages to console
 -F, --file <file>           use the file instead of the kernel log buffer
 -f, --facility <list>       restrict output to defined facilities
 -H, --human                 human readable output
 -k, --kernel                display kernel messages
 -L, --color                 colorize messages
 -l, --level <list>          restrict output to defined levels
 -n, --console-level <level> set level of messages printed to console
 -P, --nopager               do not pipe output into a pager
 -r, --raw                   print the raw message buffer
 -S, --syslog                force to use syslog(2) rather than /dev/kmsg
 -s, --buffer-size <size>    buffer size to query the kernel ring buffer
 -T, --ctime                 show human readable timestamp (could be 
                               inaccurate if you have used SUSPEND/RESUME)
 -t, --notime                don't print messages timestamp
 -u, --userspace             display userspace messages
 -w, --follow                wait for new messages
 -x, --decode                decode facility and level to readable string

 -h, --help     display this help and exit
 -V, --version  output version information and exit

コマンドの使用例と解説

1.dmesgコマンドの基本的な使用

 カーネルが起動中に生成されたメッセージを表示します。古いメッセージから新しいメッセージまで順に表示されます。

$ dmesg
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Initializing cgroup subsys cpuacct
[    0.000000] Linux version 3.10.0-1160.108.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ) #1 SMP Thu Jan 25 16:17:31 UTC 2024
[    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-1160.108.1.el7.x86_64 root=/dev/mapper/centos-root ro crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet LANG=ja_JP.UTF-8
[    0.000000] e820: BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
[    0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved
(略)
2.特定のログレベルのメッセージを表示

エラーレベルのメッセージのみを表示します。他にもinfoやwarningなどのログレベルが指定できます。

$ dmesg -l err
[    3.158370] [drm:vmw_host_log [vmwgfx]] *ERROR* Failed to send host log message.
[    3.159992] [drm:vmw_host_log [vmwgfx]] *ERROR* Failed to send host log message.
3.タイムスタンプを表示

メッセージの前に人間が読みやすい形式でタイムスタンプが表示されます。

$ dmesg -T
[土  2月 24 00:49:06 2024] Initializing cgroup subsys cpuset
[土  2月 24 00:49:06 2024] Initializing cgroup subsys cpu
[土  2月 24 00:49:06 2024] Initializing cgroup subsys cpuacct
[土  2月 24 00:49:06 2024] Linux version 3.10.0-1160.108.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ) #1 SMP Thu Jan 25 16:17:31 UTC 2024
(略)
4.カーネルメッセージバッファのクリア

 メッセージをすべて読み込んでから、カーネルメッセージバッファをクリアします。rootユーザーに切り替えて実行する必要があります。

$ su -
パスワード:
最終ログイン: 2024/02/24 (土) 00:00:36 JST日時 pts/1
[root@localhost ~]# dmesg -c
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Initializing cgroup subsys cpuacct
[    0.000000] Linux version 3.10.0-1160.108.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ) #1 SMP Thu Jan 25 16:17:31 UTC 2024
(省略)

カーネルメッセージを表示します。

# dmesg

何も出力されません。カーネルメッセージがクリアされていることが確認できます。



 dmesgコマンドは、システムのトラブルシューティングやデバイスの検出、ドライバの問題の特定など、さまざまな用途で役立ちます。