1 - 查看内核版本
查看当前 debian 内核的版本:
$ uname -r
6.1.0-31-amd64
$ uname -v
#1 SMP PREEMPT_DYNAMIC Debian 6.1.128-1 (2025-02-07)
$ uname -a
Linux debian12 6.1.0-31-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.128-1 (2025-02-07) x86_64 GNU/Linux
这说明安装时的内核版本为 6.1.0-31-amd64,当前已经更新到 6.1.0-31-amd64 版本(debian 12.9,更新时间 2025-02-07)。
其他方式:
$ cat /proc/version
Linux version 6.1.0-31-amd64 (debian-kernel@lists.debian.org) (gcc-12 (Debian 12.2.0-14) 12.2.0, GNU ld (GNU Binutils for Debian) 2.40) #1 SMP PREEMPT_DYNAMIC Debian 6.1.128-1 (2025-02-07)
dmesg 命令:
$ sudo dmesg | grep Linux
[ 0.000000] Linux version 6.1.0-31-amd64 (debian-kernel@lists.debian.org) (gcc-12 (Debian 12.2.0-14) 12.2.0, GNU ld (GNU Binutils for Debian) 2.40) #1 SMP PREEMPT_DYNAMIC Debian 6.1.128-1 (2025-02-07)
......
dpkg 命令:
$ dpkg --list | grep linux-image
ii linux-image-6.1.0-31-amd64 6.1.128-1 amd64 Linux 6.1 for 64-bit PCs (signed)
ii linux-image-amd64 6.1.128-1 amd64 Linux for 64-bit PCs (meta-package)
$ dpkg --list | grep -Ei 'linux-image|linux-headers|linux-modules'
ii linux-image-6.1.0-31-amd64 6.1.128-1 amd64 Linux 6.1 for 64-bit PCs (signed)
ii linux-image-amd64 6.1.128-1 amd64 Linux for 64-bit PCs (meta-package)
2 - 更新内核
自动更新内核
可以单独更新 linux 内核:
$ sudo apt update
$ sudo apt install linux-image-amd64
也可以使用 apt upgrade
更新所有内容,期间如果有内核更新则自动安装:
$ sudo apt update
$ sudo apt upgrade
这个方式会自动更新 linux 内核,但是是安装 debian 12 的设定,比如目前即使是 debian 12.9 版本,也只会使用 linux 6.1.x 内核,不会使用更新的内核版本。
手工更新内核
如果要更新为比默认的 linux 6.1.x 更新的内核版本,则需要手工更新内核。
apt list -a linux-image-amd64
输出为:
Listing... Done
linux-image-amd64/stable-backports 6.12.12-1~bpo12+1 amd64
linux-image-amd64/stable-security 6.1.133-1 amd64
linux-image-amd64/stable 6.1.129-1 amd64
可以看到有多个版本,通常自动更新时只会选择最新的 stable 或者 stable-security 版本,如果需要选择其他版本,则需要手工更新。
比如这里的 stable-backports 6.12.12-1~bpo12+1 版本,手工安装命令为:
sudo apt install -t stable-backports linux-image-amd64=6.12.12-1~bpo12+1
这里的 -t stable-backports
参数表示从 stable-backports 仓库安装软件包,linux-image-amd64=6.12.12-1~bpo12+1
指定了要安装的内核版本。
安装完成后,重启系统:
sudo reboot
重启后,查看内核版本:
uname -r
输出为:
6.12.12+bpo-amd64
可以看到已经更新为 6.12.12 版本。
3 - 删除内核
多次升级之后,系统内就会累计有多个内核版本,可以考虑删除旧的不用的内核。
参考:
https://askubuntu.com/questions/1253347/how-to-easily-remove-old-kernels-in-ubuntu-20-04-lts
vi remove_old_kernels.sh
新建一个文件内容如下:
#!/bin/bash
# Run this script without any param for a dry run
# Run the script with root and with exec param for removing old kernels after checking
# the list printed in the dry run
uname -a
IN_USE=$(uname -a | awk '{ print $3 }')
if [[ $IN_USE == *-generic ]]
then
IN_USE=${IN_USE::-8}
fi
echo "Your in use kernel is $IN_USE"
OLD_KERNELS=$(
dpkg --list |
grep -v "$IN_USE" |
grep -v "linux-headers-generic" |
grep -v "linux-image-generic" |
grep -Ei 'linux-image|linux-headers|linux-modules' |
awk '{ print $2 }'
)
echo "Old Kernels to be removed:"
echo "$OLD_KERNELS"
if [ "$1" == "exec" ]; then
for PACKAGE in $OLD_KERNELS; do
yes | apt purge "$PACKAGE"
done
fi
执行
bash ./remove_old_kernels.sh
看查看到要删除的内核版本和相关的包,确认没有问题之后再通过
sudo bash ./remove_old_kernels.sh exec
进行实际删除。
之后重启,再检查现有的内核是否符合预期。