This page looks best with JavaScript enabled

Switch to a Specific Kernel Version on Ubuntu

 ·  ☕ 3 min read

1. Installing a New Kernel Version

Installing from the apt repositories is recommended, so you avoid installing an incompatible kernel version.

1.1 Installing from the Repository

  • List the available versions
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
  • Install the kernel
1
apt install linux-image-5.15.0-97-generic linux-headers-5.15.0-94-generic

1.2 Downloading and Installing Manually

You need to first confirm whether the current system is compatible with the target kernel version.

  • Download the kernel

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
  • Install the kernel
1
dpkg -i *.deb

2. Switching the Default Kernel Version

2.1 Check the Current Version

1
2
3
uname -r

5.15.0-1033-aws

2.2 List the Available Versions

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 Find the grub Boot Identifiers

In Ubuntu, boot entries have two levels of identifier.

  • Find the first-level boot identifier
1
cat /boot/grub/grub.cfg | grep submenu

Example output:

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' {

Taking Advanced options for Ubuntu as an example, note down gnulinux-advanced-4b727438-7c0b-4757-a56f-24bd780b3527 as the first-level boot identifier.

  • Find the second-level boot identifier
1
cat /boot/grub/grub.cfg | grep menuentry

Example output:

 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' {

Taking Ubuntu, with Linux 5.15.0-1033-aws as an example, note down gnulinux-5.15.0-1033-aws-advanced-4b727438-7c0b-4757-a56f-24bd780b3527 as the second-level boot identifier.

2.4 Edit the Boot Configuration File

Following Ubuntu’s two-level boot identifiers, direct the bootloader to load the specified kernel version.

1
vim /etc/default/grub

Change GRUB_DEFAULT=0 to:

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

You can also write it by index order (starting from 0):

1
GRUB_DEFAULT="0>2"

Or use the semantic identifiers

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

After rebooting, check the kernel version:

1
uname -r

WeChat Official Account
WRITTEN BY
WeChat Official Account