


Do you really know how to debug Linux kernel failures? You will be enlightened after reading this article!
Aug 03, 2023 pm 04:50 PM#The Linux kernel is the core of the operating system, which controls access to system resources (such as CPU, I/O devices, physical memory, and file systems). During the boot process and while the system is running, the kernel writes various messages to the kernel ring buffer. These messages include a variety of information about system operations.
The kernel ring buffer is a part of physical memory used to save the kernel's log messages. It has a fixed size, which means that once the buffer is full, older log records will be overwritten.
The dmesg command line utility is used to print and control the kernel ring buffer in Linux and other Unix-like operating systems. Useful for inspecting kernel boot messages and debugging hardware-related issues.
In this tutorial, we will cover the basics of the dmesg command.
Use the dmesg command
The syntax of the dmesg command is as follows:
dmesg [OPTIONS] 在不帶任何選項的情況下調(diào)用時,dmesg將所有消息從內(nèi)核環(huán)形緩沖區(qū)寫入標準輸出:
$ dmesg
Default , all users can run the dmesg command. However, on some systems, access to dmesg may be restricted to non-root users. In this case, you will receive the following error message when calling dmesg:
dmesg: readkernel buffer failed: Operation not permitted
Kernel parameterskernel.dmesg_restrictSpecifies whether non-privileged users can use dmesg to view messages from the kernel log buffer information. To remove the restriction, set it to zero:
$ sudo sysctl -w kernel.dmesg_restrict=0
Typically the output contains many lines of information, so only See the last part of the output. To view one page at a time, pipe the output to a paging utility such as less or more:
$ dmesg --color=always | less
where The --color=always parameter is used to preserve colored output.
If you want to filter buffer messages, you may use grep. For example, to view only USB-related messages, type:
$ dmesg | grep -i usb
dmesg 從/proc/kmsg虛擬文件中讀取內(nèi)核生成的消息。該文件提供了到內(nèi)核環(huán)形緩沖區(qū)的接口,并且只能由一個進程打開。如果系統(tǒng)上正在運行syslog進程,并且你嘗試使用cat或less命令讀取文件,則命令將掛起。
syslog守護程序?qū)?nèi)核消息轉(zhuǎn)儲到/var/log/dmesg,因此你也可以使用該日志文件:
$ cat /var/log/dmesg
格式化 dmesg 輸出。
dmesg命令提供了許多選項,可幫助你格式化和過濾輸出。
dmesg中最常用的選項之一是-H(--human),它將輸出更容易讀的結(jié)果。
$ dmesg -H
要打印人類可讀的時間戳,請使用-T(--ctime選項):
$ dmesg -T [Mon Oct 14 14:38:04 2019] IPv6: ADDRCONF(NETDEV_CHANGE): wlp1s0: link becomes ready
時間戳格式也可以使用--time-format
$ dmesg --time-format=delta
You can also combine two or more options:
$ dmesg -H -T
To watch the output of the dmesg command in real time, use the -w (--follow) option:
$ dmesg --follow
Filter dmesg output.
You can limit dmesg output to a given facility and level. dmesg supports the following types:
kern-kern message user-user-level message mail-mail system daemon - system daemon auth - security/authorization message - ##syslog - internal syslogd message
- lpr-line printer subsystem
- news-network news subsystem
- -f( The --facility
) option allows you to limit the output to a specific device. The option accepts one or more comma-separated capabilities.
$ dmesg -f kern,daemon
emerg-The system is unavailable alert-Action must be taken immediately crit-Emergency err-Error condition warn-Warning condition notice-Normal but important condition info - Informational debug - Debug level message
-l( --level <list>)
option allows you to limit the output to a defined level, this option accepts one or more comma separated levels. The following command displays only error and critical messages:
$ dmesg -l err,crit
clear ring buffer
-C (--clear ) option allows you to clear the ring buffer:
$ sudo dmesg -C
Only root or a user with sudo privileges can clear the buffer.
To print the buffer contents before clearing, use the -c (--read-clear) option:
$ sudo dmesg -c
If you want to Save the current dmesg log to a file before clearing it, you can redirect the output to a file:
$ dmesg > dmesg_messages
Conclusion
The dmesg command allows you to view and control the kernel ring buffer. It is useful when troubleshooting kernel or hardware issues.
Enter man dmesg in the terminal and you can get information about all available dmesg options.
The above is the detailed content of Do you really know how to debug Linux kernel failures? You will be enlightened after reading this article!. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Linux's cost of ownership is usually lower than Windows. 1) Linux does not require license fees, saving a lot of costs, while Windows requires purchasing a license. 2) Linux has low hardware requirements and can extend the service life of the device. 3) The Linux community provides free support to reduce maintenance costs. 4) Linux is highly secure and reduces productivity losses. 5) The Linux learning curve is steep, but Windows is easier to use. The choice should be based on specific needs and budget.

LinuxoftenoutperformsWindowsinI/Operformanceduetoitscustomizablekernelandfilesystems,whileWindowsoffersmoreuniformperformanceacrosshardware.1)LinuxexcelswithcustomizableI/OschedulerslikeCFQandDeadline,enhancingperformanceinhigh-throughputapplications

The key to installing dual systems in Linux and Windows is partitioning and boot settings. 1. Preparation includes backing up data and compressing existing partitions to make space; 2. Use Ventoy or Rufus to make Linux boot USB disk, recommend Ubuntu; 3. Select "Coexist with other systems" or manually partition during installation (/at least 20GB, /home remaining space, swap optional); 4. Check the installation of third-party drivers to avoid hardware problems; 5. If you do not enter the Grub boot menu after installation, you can use boot-repair to repair the boot or adjust the BIOS startup sequence. As long as the steps are clear and the operation is done properly, the whole process is not complicated.

The key to enabling EPEL repository is to select the correct installation method according to the system version. First, confirm the system type and version, and use the command cat/etc/os-release to obtain information; second, enable EPEL through dnfinstallepel-release on CentOS/RockyLinux, and the 8 and 9 version commands are the same; third, you need to manually download the corresponding version of the .repo file and install it on RHEL; fourth, you can re-import the GPG key when encountering problems. Note that the old version may not be supported, and you can also consider enabling epel-next to obtain the test package. After completing the above steps, use dnfrepolist to verify that the EPEL repository is successfully added.

Linux usually performs better in web server performance, mainly due to its advantages in kernel optimization, resource management and open source ecosystem. 1) After years of optimization of the Linux kernel, mechanisms such as epoll and kqueue make it more efficient in handling high concurrent requests. 2) Linux provides fine-grained resource management tools such as cgroups. 3) The open source community continuously optimizes Linux performance, and many high-performance web servers such as Nginx are developed on Linux. By contrast, Windows performs well when handling ASP.NET applications and provides better development tools and commercial support.

Newbie users should first clarify their usage requirements when choosing a Linux distribution. 1. Choose Ubuntu or LinuxMint for daily use; programming and development are suitable for Manjaro or Fedora; use Lubuntu and other lightweight systems for old devices; recommend CentOSStream or Debian to learn the underlying principles. 2. Stability is preferred for UbuntuLTS or Debian; you can choose Arch or Manjaro to pursue new features. 3. In terms of community support, Ubuntu and LinuxMint are rich in resources, and Arch documents are technically oriented. 4. In terms of installation difficulty, Ubuntu and LinuxMint are relatively simple, and Arch is suitable for those with basic needs. It is recommended to try it first and then decide.

The steps to add a new hard disk to the Linux system are as follows: 1. Confirm that the hard disk is recognized and use lsblk or fdisk-l to check; 2. Use fdisk or parted partitions, such as fdisk/dev/sdb and create and save; 3. Format the partition to a file system, such as mkfs.ext4/dev/sdb1; 4. Use the mount command for temporary mounts, such as mount/dev/sdb1/mnt/data; 5. Modify /etc/fstab to achieve automatic mount on the computer, and test the mount first to ensure correctness. Be sure to confirm data security before operation to avoid hardware connection problems.

Logs in Linux systems are usually stored in the /var/log directory, which contains a variety of key log files, such as syslog or messages (record system logs), auth.log (record authentication events), kern.log (record kernel messages), dpkg.log or yum.log (record package operations), boot.log (record startup information); log content can be viewed through cat, tail-f or journalctl commands; application logs are often located in subdirectories under /var/log, such as Apache's apache2 or httpd directory, MySQL log files, etc.; at the same time, it is necessary to note that log permissions usually require s
