dmesg 命令 習慣於 顯示內核相關消息 類 Unix 系統。 dmesg 是“查看消息或查看驅動程序”。 dmesg 命令 該數據是通過讀取內核環形緩衝區獲得的。 dmesg 命令在對 Linux 系統進行故障排除時非常有用,有助於識別與硬件相關的錯誤和警告。它還將與守護進程相關的消息輸出到屏幕。
讓我們通過下面描述的實際示例來了解最著名的工具“dmesg”命令。 dmesg 的確切語法是:
# dmseg [options...]
以下是 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 -E, --console-on enable printing messages to console -F, --file use the file instead of the kernel log buffer -f, --facility restrict output to defined facilities -H, --human human readable output -k, --kernel display kernel messages -L, --color[=] colorize messages (auto, always or never) colors are enabled by default -l, --level restrict output to defined levels -n, --console-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 buffer size to query the kernel ring buffer -u, --userspace display userspace messages -w, --follow wait for new messages -x, --decode decode facility and level to readable string -d, --show-delta show time delta between printed messages -e, --reltime show local time and time delta in readable format -T, --ctime show human-readable timestamp (may be inaccurate!) -t, --notime don't show any timestamp with messages --time-format show timestamp using the given format: [delta|reltime|ctime|notime|iso]
現在讓我們進入提示部分。
1.列出內核中加載的所有驅動程序
打開終端,鍵入“dmesg”命令,然後按 Enter。
您可以通過 dmesg 命令使用文本處理工具,即“more”、“tail”、“less”或“grep”。
# dmesg |more
樣本輸出:
2.列出所有檢測到的設備
要查找內核檢測到的硬盤,可以搜索關鍵字“sda”和“grep”,如下所示:
$ dmesg | grep sda [ 4.989254] sd 0:0:0:0: [sda] 1250263728 512-byte logical blocks: (640 GB/596 GiB) [ 4.989263] sd 0:0:0:0: [sda] Write Protect is off [ 4.989265] sd 0:0:0:0: [sda] Mode Sense: 00 3a 00 00 [ 4.989280] sd 0:0:0:0: [sda] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA [ 5.053921] sda: sda1 sda2 [ 5.054436] sd 0:0:0:0: [sda] Attached SCSI disk [ 4266.344111] sd 0:0:0:0: [sda] Synchronizing SCSI cache [ 4266.344778] sd 0:0:0:0: [sda] Stopping disk [ 4268.621944] sd 0:0:0:0: [sda] Starting disk [ 6875.213393] sd 0:0:0:0: [sda] Synchronizing SCSI cache [ 6875.352287] sd 0:0:0:0: [sda] Stopping disk [ 6877.025739] sd 0:0:0:0: [sda] Starting disk
“sda”是第一個SATA硬盤,“sdb”是第二個SATA硬盤,以此類推。 對於 IDE 硬盤,使用“hda”或“hdb”進行搜索。
3.顯示內存、硬盤、U盤、串口信息
由於 dmesg 輸出的長度,很難找到特定的字符串。因此,它會過濾包含“usb”、“dma”、“tty”、“memory”等字符串的行。 ‘-i’ 選項告訴 grep 命令忽略大小寫字母(大寫或小寫)。
~]# dmesg | grep -i memory # dmesg | grep -i dma # dmesg | grep -i usb # dmesg | grep -i tty
還
# dmesg | grep -E "memory|dma|usb|tty"
4. 顯示彩色消息(dmesg 命令輸出)
如果要以彩色打印消息,請在 dmesg 命令中使用“-L”選項。
# dmesg -L
樣本輸出:
5.檢查以太網鏈路的狀態(UP / DOWN)
在以下示例中,dmesg 顯示 eth0 鏈接在引導期間處於活動狀態。
# dmesg | grep eth

6.讀取並清除dmesg緩沖日志
如果要在讀取後清除 dmesg 日誌,可以在 dmesg 命令中使用選項 -C。
# dmesg -C
7、dmesg實時監控
使用 dmesg 命令的“-follow”選項查看實時 dmesg 日誌。一個例子如下所示。
# dmesg --follow
一些發行版允許命令’tail -f / var / log / dmesg‘並用於實時 dmesg 監控。
# watch "dmesg | tail -20"
8.限制dmesg輸出到特定工具,如守護進程
如果要將 dmesg 的輸出限制為特定工具(例如守護程序),請在 dmesg 命令中使用選項“–facility = daemon”。
# dmesg --facility=daemon
9. 在 dmesg 日誌中啟用時間戳
您可能希望將時間戳與 dmesg 生成的消息相關聯。這可以使用 -T 命令行選項來完成,該選項會生成人類可讀的時間戳。
# dmesg -T
為了安全起見,如果您需要帶有解碼工具的時間圖和 dmesg 命令輸出的級別,請使用“-Tx”選項。
# dmesg -Tx
默認情況下,來自 dmesg 輸出的時間戳不是人類可讀的。為了使其易於閱讀,您可以使用 — ctime 參數。
# dmesg - -ctime
10.使用’-r’選項顯示原始消息緩衝區
使用 dmesg 命令的“-r”選項來顯示原始消息緩衝區。
# dmesg -r
11.強制dmesg命令使用syslog
在某些情況下,讓 dmesg 從 syslog 中獲取數據,而不是 /dev/kmsg。這可以使用選項“-S”輕鬆實現。一個例子如下所示。
# dmesg -S
dmesg 命令日誌保存在文件“/var/log/dmesg”中
12.只打印前20行,只打印最後20行
‘head’ 和 dmesg 顯示起始行。也就是說,’dmesg | head-20′ 從一開始只打印 20 行。
# dmesg | head -20
‘tail’ 和 dmesg 命令只打印最後 20 行。這在連接可移動設備時很有用。
# dmesg | tail -20
13.日誌級別打印
dmesg 還可以根據級別打印日誌。要以這種方式輸出輸出,請使用 -level 參數
# dmesg - -level=err
樣本輸出:
[ 0.000000] tsc: Fast TSC calibration failed [ 19.595760] piix4_smbus 0000:00:07.0: SMBus base address uninitialized - upgrade BIOS or use force_addr=0xaddr
此選項僅打印錯誤消息。
結論是
dmesg 命令很有用,因為 dmesg 實時記錄所做或發生的所有系統更改。與往常一樣,您可以使用 mandmesg 獲取更多信息。