Please enable Javascript to view the contents

Ubuntu 切换指定版本的内核

 ·  ☕ 2 分钟

1. 安装新的内核版本

推荐使用 apt 源安装,避免安装了不兼容的内核版本。

1.1 源安装

  • 查看可用版本
1
2
3
4
apt list linux-headers-5.15.*-*-generic linux-image-5.15.*-*-generic

linux-image-5.15.0-94-generic/focal-updates,focal-security 5.15.0-94.104~20.04.1 amd64
linux-image-5.15.0-97-generic/focal-updates,focal-security 5.15.0-97.107~20.04.1 amd64
  • 安装内核
1
apt install linux-image-5.15.0-97-generic linux-headers-5.15.0-94-generic

1.2 自行下载安装

需要先确认下当前的系统与目标内核版本是否兼容。

  • 下载内核

http://kernel.ubuntu.com/~kernel-ppa/mainline/

1
2
3
4
wget https://kernel.ubuntu.com/mainline/v5.19/amd64/linux-headers-5.19.0-051900-generic_5.19.0-051900.202207312230_amd64.deb
wget amd64/linux-headers-5.19.0-051900_5.19.0-051900.202207312230_all.deb
wget https://kernel.ubuntu.com/mainline/v5.19/amd64/linux-image-unsigned-5.19.0-051900-generic_5.19.0-051900.202207312230_amd64.deb
wget https://kernel.ubuntu.com/mainline/v5.19/amd64/linux-modules-5.19.0-051900-generic_5.19.0-051900.202207312230_amd64.deb
  • 安装内核
1
dpkg -i *.deb

2. 切换默认内核版本

2.1 查看当前版本

1
2
3
uname -r

5.15.0-1033-aws

2.2 查看可用的版本

1
2
3
4
5
6
dpkg --get-selections | grep linux-image

linux-image-5.15.0-1033-aws                     install
linux-image-5.15.0-1072-aws                     install
linux-image-5.15.0-97-generic                   install
linux-image-aws                                 install

2.3 查看 grub 引导标识

在 Ubuntu 中,引导有两级标识。

  • 查看一级引导标识
1
cat /boot/grub/grub.cfg | grep submenu

输出示例:

1
2
submenu 'Advanced options for Ubuntu' $menuentry_id_option 'gnulinux-advanced-4b727438-7c0b-4757-a56f-24bd780b3527' {
submenu 'Advanced options for Ubuntu 20.04.6 LTS (20.04) (on /dev/nvme0n1p1)' $menuentry_id_option 'osprober-gnulinux-advanced-4b727438-7c0b-4757-a56f-24bd780b3527' {

Advanced options for Ubuntu 为例,记录下 gnulinux-advanced-4b727438-7c0b-4757-a56f-24bd780b3527 作为引导的一级标识。

  • 查看二级引导标识
1
cat /boot/grub/grub.cfg | grep menuentry

输出示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
cat /boot/grub/grub.cfg | grep submenu
submenu 'Advanced options for Ubuntu' $menuentry_id_option 'gnulinux-advanced-4b727438-7c0b-4757-a56f-24bd780b3527' {
submenu 'Advanced options for Ubuntu 20.04.6 LTS (20.04) (on /dev/nvme0n1p1)' $menuentry_id_option 'osprober-gnulinux-advanced-4b727438-7c0b-4757-a56f-24bd780b3527' {
root@aws-mx-ai-kas-gpu-l40s-02:~#
root@aws-mx-ai-kas-gpu-l40s-02:~# cat /boot/grub/grub.cfg | grep menuentry
if [ x"${feature_menuentry_id}" = xy ]; then
  menuentry_id_option="--id"
  menuentry_id_option=""
export menuentry_id_option
menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-4b727438-7c0b-4757-a56f-24bd780b3527' {
submenu 'Advanced options for Ubuntu' $menuentry_id_option 'gnulinux-advanced-4b727438-7c0b-4757-a56f-24bd780b3527' {
        menuentry 'Ubuntu, with Linux 5.15.0-1072-aws' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-1072-aws-advanced-4b727438-7c0b-4757-a56f-24bd780b3527' {
        menuentry 'Ubuntu, with Linux 5.15.0-1072-aws (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-1072-aws-recovery-4b727438-7c0b-4757-a56f-24bd780b3527' {
        menuentry 'Ubuntu, with Linux 5.15.0-1033-aws' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-1033-aws-advanced-4b727438-7c0b-4757-a56f-24bd780b3527' {
        menuentry 'Ubuntu, with Linux 5.15.0-1033-aws (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-1033-aws-recovery-4b727438-7c0b-4757-a56f-24bd780b3527' {
        menuentry 'Ubuntu, with Linux 5.15.0-97-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-97-generic-advanced-4b727438-7c0b-4757-a56f-24bd780b3527' {

Ubuntu, with Linux 5.15.0-1033-aws 为例,记录下 gnulinux-5.15.0-1033-aws-advanced-4b727438-7c0b-4757-a56f-24bd780b3527 作为引导的二级标识。

2.4 编辑引导文件

按照 Ubuntu 的两级引导标识,指引引导程序加载指定的内核版本。

1
vim /etc/default/grub

GRUB_DEFAULT=0 改为:

1
GRUB_DEFAULT="gnulinux-advanced-4b727438-7c0b-4757-a56f-24bd780b3527>gnulinux-5.15.0-1033-aws-advanced-4b727438-7c0b-4757-a56f-24bd780b3527"

也可以按照索引顺序写成(从 0 开始):

1
GRUB_DEFAULT="0>2"

或者使用语义标识

1
GRUB_DEFAULT="Advanced options for Ubuntu>Ubuntu, with Linux 5.15.0-1033-aws"
  • 更新 grub
1
update-grub

重启之后,查看内核版本:

1
uname -r

微信公众号
作者
微信公众号