How to update ubuntu kernel manually

Before going through the process of how to update ubuntu kernel manually, let’s have a brief introduction about what is kernel. Kernel is a core of an operating system. It usually resides in the protected area of system memory so that it can stay safe from being overwritten by any other program. Kernel is the first program of an operating system which loads after the boot loader and control many other tasks such as disk management, task management, memory management etc. It control various processes and decides which process needs to be executed by processor and which process needs to stay in main memory to execute.

Ubuntu Kernel

Linux Kernel is the core of an ubuntu operating system. It is one of the first software program which a booting device loads and run on a central processing unit (CPU). ubuntu kernel controls various hardware resources like storage, networking, graphics, memory and cpu of the device.

How to update ubuntu kernel manually

When you install ubuntu operating system on any physical or virtual machine then it might have any xyz default Linux kernel version depending upon different versions of ubuntu as well as the customized ubuntu images. There might be some scenario where you need to have some specified version of ubuntu kernel. But the default installed kernel is either an old kernel or a new one which you don’t need. Thus in that case you either need to upgrade or downgrade the ubuntu kernel. So in this article we will see how we can update the kernel to a specified version manually.

Check the Installed Kernel version

First and foremost you need to check the version of already installed kernel. You can use any of the following commands to see the kernel version.

uname -r 
Linux hpe25 5.4.0-1021-gcp

or

cat /proc/version
Linux version 5.4.0-1021-gcp

Get the list of available kernels

You can search the apt repositories to get the list of all available kernels.

sudo apt-get update
apt-cache search --names-only linux-image

Install the new kernel which you want ubuntu to use

For example we want to install 5.3.0-1035-azure ubuntu kernel then we need to install it along with other packages for the same version using apt-get install command. Below is an example snippet for the same. You need to install all these packages for your required version.

sudo apt-get install linux-image-5.3.0-1035-azure
sudo apt-get install linux-headers-5.3.0-1035-azure
sudo apt-get install linux-modules-5.3.0-1035-azure
sudo apt-get install linux-modules-extra-5.3.0-1035-azure

Verify that kernel got Installed

Once the kernel got installed successfully then you can verify its using apt list command along with grep command to filter the result only with the newly installed packages.

sudo apt list | grep -i 5.3.0-1035-azure

Update the Kernel boot sequence in GRUB

Now you have 2 kernels installed in your system, one is the default and other is which you have installed. In GRUB of linux boot process, there is a sequence of installed kernel which it loads. By default it always tries to boot a kernel which is available at first number in the sequence starting from 0 to 1,2,3 etc. In our case as we have two kernels installed thus the default installed kernel would be at top i.e at 0th position and system will always boot up using this kernel. Thus we need to update the kernel boot sequence in GRUB so that the system can boot from the newly installed kernel.

Let’s see how to check the kernel boot sequence in GRUB. You can use the following commands for the same.

export GRUB_CONFIG=`sudo find /boot -name "grub.cfg"`
sudo grep 'menuentry ' $GRUB_CONFIG | cut -f 2 -d "'" | nl -v 0
    0  Ubuntu
    1  Ubuntu, with Linux 5.4.0-1021-gcp
    2  Ubuntu, with Linux 5.4.0-1021-gcp (recovery mode)
    3  Ubuntu, with Linux 5.3.0-1035-azure
    4  Ubuntu, with Linux 5.3.0-1035-azure (recovery mode)
    5  System setup

The above commands will show the list of all installed kernel with a boot sequence number. You need to note down the sequence number or name of your newly installed kernel.

Once you noted down the boot sequence no or name of the kernel, now it is the time to update it in the grub file of the system. it needs to be updated in two files.

# /etc/default/grub
# /etc/default/grub.d/50-cloudimg-settings.cfg  #Ignore this file if it is not present. It will be present if you are using any cloud platform like Azure , AWS , GCP etc

These file can be opened in any file editors like vi, vim or nano and then you can update the kernel name or sequence number for a parameter called GRUB_DEFAULT. Below are the example snippets for the same.

Update Kernel Sequence number in GRUB

sudo vi /etc/default/grub
GRUB_DEFAULT=3
GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=0
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
GRUB_CMDLINE_LINUX=""
sudo vi /etc/default/grub.d/50-cloudimg-settings.cfg
# CLOUD_IMG: This file was created/modified by the Cloud Image build process
GRUB_DEFAULT=3
GRUB_HIDDEN_TIMEOUT=0.1
GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=0.1
GRUB_CMDLINE_LINUX_DEFAULT="console=ttyS0"
GRUB_RECORDFAIL_TIMEOUT=0
GRUB_TERMINAL=console

Update Kernel Name in GRUB

sudo vi /etc/default/grub
GRUB_DEFAULT="linux-image-5.3.0-1035-azure"
GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=0
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
GRUB_CMDLINE_LINUX=""
sudo vi /etc/default/grub.d/50-cloudimg-settings.cfg
# CLOUD_IMG: This file was created/modified by the Cloud Image build process
GRUB_DEFAULT="linux-image-5.3.0-1035-azure"
GRUB_HIDDEN_TIMEOUT=0.1
GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=0.1
GRUB_CMDLINE_LINUX_DEFAULT="console=ttyS0"
GRUB_RECORDFAIL_TIMEOUT=0
GRUB_TERMINAL=console

Note : You need to either update the Kernel sequence number or it’s name.

Once you have updated the files then run the below command to update the grub.

sudo update-grub

Reboot the system

Now you need to reboot the system, so that your changes can take affect and system can boot with new kernel.

sudo reboot

Verify the system kernel

Once the system has booted and comes up then it is the time to verify the system kernel. Again you can verify it with same command which we used in starting.

uname -r 
Linux hpe25 5.3.0-1035-azure
or

cat /proc/version
Linux version 5.3.0-1035-azure

Now this time it must show the newly installed kernel.

If system is still booting with old kernel

In case you are still seeing that system has booted with old kernel only instead of your newly installed kernel, then you can uninstall the old kernel using apt-get remove command. Below are the example snippet for the same.

sudo apt-get remove 5.4.0-1021-gcp
sudo apt-get remove linux-headers-5.4.0-1021-gcp
sudo apt-get remove linux-modules-5.4.0-1021-gcp
sudo apt-get remove linux-modules-extra-5.4.0-1021-gcp

After uninstalling the kernel , you again need to check and update the kernel boot sequence in GRUB as explained above and reboot the system. This time it must boot from the newly installed kernel.

Conclusion

In this article we have covered how we can update (upgrade/downgrade) the ubuntu kernel manually. If you have any questions, please comment below. We will get back to you as soon as possible.

Leave a comment