How to upgrade kernel version in Ubuntu 20.04

Check the current installed kernel version

1
2
$ uname -r
5.4.0-107-generic

Check the repository connected to the server

1
2
3
4
5
6
7
8
9
10
11
12
$ grep ^deb /etc/apt/sources.list
deb http://us.archive.ubuntu.com/ubuntu focal main restricted
deb http://us.archive.ubuntu.com/ubuntu focal-updates main restricted
deb http://us.archive.ubuntu.com/ubuntu focal universe
deb http://us.archive.ubuntu.com/ubuntu focal-updates universe
deb http://us.archive.ubuntu.com/ubuntu focal multiverse
deb http://us.archive.ubuntu.com/ubuntu focal-updates multiverse
deb http://us.archive.ubuntu.com/ubuntu focal-backports main restricted universe multiverse
deb http://us.archive.ubuntu.com/ubuntu focal-security main restricted
deb http://us.archive.ubuntu.com/ubuntu focal-security universe
deb http://us.archive.ubuntu.com/ubuntu focal-security multiverse
deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable

Check the kernel version provided by the repository

1
2
3
4
5
6
7
8
9
10
$ apt-get update

$ apt-cache search linux-image-5.*-generic
linux-image-5.4.0-26-generic - Signed kernel image generic
<omitted..>
linux-image-5.11.0-22-generic - Signed kernel image generic
linux-image-5.13.0-21-generic - Signed kernel image generic
linux-image-5.15.0-100-generic - Signed kernel image generic
linux-image-5.15.0-97-generic - Signed kernel image generic
<omitted..>

Install the target kernel version

1
$ apt install linux-{image,headers}-5.15.0-91-generic

Check all the bootable kernel versions

1
2
3
4
5
6
7
$ awk -F"--class" '/menuentry/ && /with Linux/ {print $1}' /boot/grub/grub.cfg | awk '{print i++ " : " $5,$6,$7,$8}' | sed -e "s/'/ /g"
0 : 5.15.0-91-generic
1 : 5.15.0-91-generic (recovery mode)
2 : 5.4.0-107-generic
3 : 5.4.0-107-generic (recovery mode)
4 : 5.4.0-73-generic
5 : 5.4.0-73-generic (recovery mode)

Configure the most recent kernel to be booted

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ sed -i 's/GRUB_DEFAULT=.*/GRUB_DEFAULT=saved/g' /etc/default/grub
$ grep "GRUB_DEFAULT" /etc/default/grub
GRUB_DEFAULT=saved
$ grub-set-default "Advanced options for Ubuntu>Ubuntu, with Linux 5.15.0-91-generic"
$ grub-editenv list
saved_entry=Advanced options for Ubuntu>Ubuntu, with Linux 5.15.0-91-generic

$ update-grub
Sourcing file `/etc/default/grub'
Sourcing file `/etc/default/grub.d/init-select.cfg'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.15.0-91-generic
Found initrd image: /boot/initrd.img-5.15.0-91-generic
Found linux image: /boot/vmlinuz-5.4.0-107-generic
Found initrd image: /boot/initrd.img-5.4.0-107-generic
Found linux image: /boot/vmlinuz-5.4.0-73-generic
Found initrd image: /boot/initrd.img-5.4.0-73-generic
done

Reboot the server to activate the latest kernel

1
$ reboot

Verify the booted kernel version

1
2
$ uname -r
5.15.0-91-generic