tep 1 - Update Ubuntu Repository and Upgrade all Packages
The first thing we want to do before we upgrade the kernel is to update the Ubuntu repositories and to upgrade all packages to their latest versions.
Connect to your ubuntu server through SSH login. You have to be root user for the next steps. If root login is disabled on your server, then login as your normal user and run 'sudo -s to become root.
Update all repositories of the system.
sudo apt update
Then upgrade all packages installed with the command below.
sudo apt upgrade -y
Finally, reboot the server to ensure that potentially installed kernel updates get applied.
Now check again for upgradable packages with the command below.
sudo apt list --upgradeable
Make sure you have no pensing updates.
Step 2 - Checking the Active Kernel Version
In this step, we will check the active kernel of our system. We need to know the system architecture and the kernel version that is used on the system.
Check the active kernel used by the system with the uname command.
You will get the kernel version as below.
Linux 4.4.0-78-generic x86_64
This means, we are using the Generic Linux kernel version 4.4 with x86_64/64-bit system.
The Generic kernel is a general purpose kernel. If you want to use your box for tasks like editing, encoding video and audio, it's e.g. recommended to use one of the 'lowlatency' kernels.
Step 3 - Install New Kernel Version
Now we know that we are using kernel 4.4 at the moment. In this step, we will upgrade the used kernel to the latest mainline kernel build version 4.11.2. All mainline kernel build can be downloaded from this site - http://kernel.ubuntu.com/~kernel-ppa/mainline/.
Create a new directory for the mainline kernel version.
sudo mkdir -p ~/4.11.2
Download the generic kernel version packages (because we are using generic version) using the wget command.
Now install the packages with the dpkg command below.
dpkg -i *.deb
When the kernel package installation is complete, update grub and reboot the system.
After server rebooting, check again the kernel version.
You will get a new kernel on the system: 4.11.2.
Linux 4.11.2-041102-generic x86_64
Step 4 - Remove the old Kernel
At this stage, the kernel version of our server is updated to 4.11.2. In this step, we will remove the old kernel and just leave the new version that we are using now.
For this purpose, we will use a command-line utility named 'byobu'. It's a great utility to safely remove and purge an old kernel from the system and will never remove the currently running kernel.
Install byobu with the apt command below.
sudo apt install byobu
Whet it's complete, check installed kernel version on the system.
dpkg -l | grep linux-image
We will get the result as below.
On the server, iI have installed three versions of the Ubuntu kernel: 4.4.62 - 4.4.78 - 4.11.2. And for now, the system is using the latest kernel 4.11.2. We will remove the old kernel 4.4 and leave the latest one 4.11.2.
Run the command below to remove the old kernel.
If you see the result below:
No kernels are eligible for removal
Then you only have 2 kernel versions installed. By default, byobu will keep at least 2 kernel version on the system to ensure that you have a fallback kernel when the first one fails.
If you want to keep the only one (the latest) kernel, then you can use the command below to remove all except of one Kernel.
purge-old-kernels --keep 1 -q
Now update grub and you will get the only one kernel version installed on the system.
The Ubuntu Kernel now is upgraded to the latest version 4.11.2 from the mainline kernel build, the old kernel version has been removed from the system for freeing disk space.