Debian系统如何查看最新安装内核?grub-mkconfig识别逻辑是什么?
Hey there! Let's tackle your questions one by one—this is a common scenario when managing kernels on Debian, so I've got you covered.
General & Debian-Specific Commands
First, for Debian-based systems (including Debian itself), there are a few straightforward ways to list your installed kernels and find the newest one:
List installed kernel packages sorted by installation time:
dpkg-query -W -f='${Installed-Time}\t${Package}\n' linux-image-* | sort -nThe last line here will show the most recently installed kernel package, since we're sorting by the timestamp of when it was installed.
Quickly get the latest kernel file (by version):
The actual kernel binaries live in/boot, so you can list and sort them by version number directly:ls /boot/vmlinuz-* | sort -V | tail -n 1The
-Vflag tellssortto use natural version number sorting, so the newest version will be the last entry.Using APT to list installed kernels:
apt list --installed | grep linux-imageYou can pipe this to
sort -Vtoo if you want to order them by version:apt list --installed | grep linux-image | sort -V
How grub-mkconfig Identifies the Latest Kernel
You're right that grub-mkconfig (and its Debian wrapper update-grub) does a great job of picking up the newest kernel. Here's how it works:
It relies on the scripts in /etc/grub.d/, specifically the 10_linux script. This script:
- Scans the
/bootdirectory for files matchingvmlinuz-*(kernel images) andinitrd.img-*(initial ramdisks). - Extracts the version string from each kernel filename.
- Compares these version numbers using a version-aware sorting algorithm (similar to
sort -V). - Orders the kernel entries in the generated
grub.cfgfrom newest to oldest, and sets the newest one as the default boot option (unless you've modified the default behavior in/etc/default/grub).
Essentially, it's just automating the same manual check you'd do by looking at /boot files, but in a scripted, reliable way.
Commands to Parse Kernel Version Information
If you need to extract or parse kernel version details specifically:
Get the currently running kernel version:
uname -rThis gives you the version string of the kernel your system is actively using right now.
Parse the version from a specific kernel file:
For a kernel binary like/boot/vmlinuz-6.1.0-12-amd64, you can use:strings /boot/vmlinuz-6.1.0-12-amd64 | grep -E 'Linux version' | head -n 1 | awk '{print $3}'Or a simpler approach with
file:file /boot/vmlinuz-6.1.0-12-amd64 | grep -oP 'version \K[^ ]+'Check details of an installed kernel package:
To get metadata (like installation time, version, dependencies) for a specific kernel package:dpkg --status linux-image-$(uname -r)Replace
$(uname -r)with the version string you're interested in if you want to check a different kernel.
内容的提问来源于stack exchange,提问作者Ben

