How to Simplify Working With Kernels

Reading Time: 5 minutes

What Is A Kernel?

The kernel is a piece of software that is at the core of an operating system. It usually has complete control over all of the server systems. It is normally the first program loaded after the bootloader has completed its tasks. The kernel then handles the rest of the start-up tasks as well as i/o requests from the system’s software. It then translates those requests into hardware instructions for the CPU (central processing unit).

The kernel also controls memory allocation, as well as hardware peripherals like keyboards, mice, monitors, printers, and any other hardware attached to the system.

Why Is It Useful?

The role of the kernel is to control the following primary server functions:

    null
  • Resource management
  • Memory management
  • Device management
  • System calls

The heart of the computer system is contained within these significant roles. 

How Do I Keep My Kernel Current?

To ensure our systems are up to date and have the latest software, both CentOS and Ubuntu both run updates nightly. These updates push the newest software versions (including kernels) and can contain updated hardware modules to improve the functionality of the server. Once these updates are installed and seen to be working correctly, we should be removing the older kernel versions to save space and reduce clutter on the server.

To get a quick glimpse at CentOS 7 system properties, we run:

root@host:~$ uname -a
Linux host.mylwinfo.com 3.10.0-862.14.4.el7.x86_64 #1 SMP Wed Sep 26 15:12:11 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

The flags for the uname command are as follows:

    -a, --all
       print all information, in the following order, except omit -p and -i if unknown:
   -s, --kernel-name
       print the kernel name
   -n, --nodename
       print the network node hostname
   -r, --kernel-release
       print the kernel release
   -v, --kernel-version
       print the kernel version
   -m, --machine
       print the machine hardware name
   -p, --processor
              print the processor type or "unknown"
       -i, --hardware-platform
              print the hardware platform or "unknown"
       -o, --operating-system
              print the operating system
       --help display this help and exit
       --version
              output version information and exit

How Do I See What Kernel I Am Currently Using?

The uname -r command (which works on both Ubuntu and CentOS) identifies the kernel version currently in use.

root@host [~]# uname -r
3.10.0-862.14.4.el7.x86_64

How Do I See How Many Kernels I Have Stored?

On the CentOS 7 server, we currently have two kernel versions. We can verify this by running the following command:


root@host [~]# rpm -q kernel 
kernel-3.10.0-862.14.4.el7.x86_64
kernel-3.10.0-1062.4.1.el7.x86_64

This command lists all stored kernel versions. 

On the Ubuntu server, we run the following command which will show the available kernels.

 root@host:~# dpkg --list | grep linux-image
ii linux-image-4.15.0-13-generic 4.15.0-13.14 amd64 Linux kernel image for version 4.15.0 on 64 bit x86 SMP
ii linux-image-4.15.0-50-generic 4.15.0-50.54 amd64 Signed kernel image generic
ii linux-image-virtual 4.15.0.50.52 amd64 Virtual Linux kernel image

How Do I See What Kernel Version I Am Currently Running?

This can be seen when we run the following command in Ubuntu 18.04

root@host:~# uname -sr
Linux 4.15.0-50-generic
root@host:~#

and when we run the same command in CentOS7.

root@host [~]# uname -sr
Linux 3.10.0-862.14.4.el7.x86_64
root@host [~]#

This command identifies the current kernel version that is being utilized on the server. This command works for both Ubuntu and CentOS.

Why Are The Kernel Versions Different?

All Linux distributions use a version of the Linux kernel, however, each operating system will have different requirements in terms of functionality and features. Because of this, each Linux version will add code into a modified kernel to allow it to work best for that specific distribution. This is why you will see some Linux distributions using kernel versions of 5.0x, and others will be at 4.15x and still others that use a 3.10x version. Does this mean that a 3.x kernel is older than a 5.x kernel? Not necessarily. It will depend on the Linux distribution itself and how the developers have implemented the numbering scheme of their kernel version.

How are the Ubuntu kernel version numbers broken down?

Let’s say we have a kernel version defined as “5.3.0-19-generic” in Ubuntu. That OS’s kernel versioning system can be broken into four parts:

-base kernel version- -ABI number- -upload number- -flavor-
5 .3 0-19 Generic

What’s an ABI you ask? The ABI or application binary interface is the communication gateway between components that facilitates interactions between those elements. The ABI simply establishes how one part of the code talks to another part of the code.

How are the CentOS kernel version numbers broken down?

In Redhat 7 based systems, the kernel is also versioned in multiple parts:

-major kernel version-  -major revision- -patch-build-    -architecture-
3.10 0-862 14.4.el7 x86_64

Since Red Hat Enterprise Linux 8 (Ootpa) is now modeled after Fedora 28, it contains the upstream Linux kernel version of 4.18. In every major RHEL release, the kernel version is fixed to the time of that initial release, and any additional security patches or driver updates will be backported into that kernel version. You can learn more info about Linux Kernel Version Numbering here.

How Do I Remove Old Unused Kernel Versions?

Method 1:

If you have multiple kernel versions stored on your system, it’s best to limit them to a predefined number. Having too many stored kernels can cause disk space issues, especially since they are usually stored in the /boot folder, which has limited space available. Our recommendation? Keep only what you need.

The first method we will use to remove old kernels is using the yum remove command. With this command, we can manually select individual kernel versions and remove them:

root@host [~]# yum remove kernel-3.10.0-1062.4.1.el7.x86_64
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package kernel.x86_64 0:3.10.0-1062.4.1.el7 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================================================================================================
 Package Arch Version Repository Size 
=======================================================================================================================================================================================================================
Removing:                                                                                                       
 kernel.x86_64 0:3.10.0-1062.4.1.el7
                                                                                                           
Transaction Summary                                                                                                  
=======================================================================================================================================================================================================================
Remove 2 Packages                                                                                                  
                                                                                                           
Installed size: 284 M                                                                                                 
Is this ok [y/N]:
y

How Do I Remove Unused Kernel Versions Automatically?

Method 2:

We can remove unused kernels automatically using the package-cleanup command. This command is part of the yum-utils package. We can use this command to remove the two oldest kernels:


root@host:~# package-cleanup --oldkernels --count=2 

The package-cleanup command cleans up locally installed, duplicate, or orphaned RPM packages. 

By default, CentOS will keep the last five kernels installed on your system. The following line defines the default setting that is located within the /etc/yum.conf file:

installonly_limit=5

If we want to limit the number of unused kernels that we store in our system to two, we have to modify the line in the /etc/yum.conf configuration file to:

installonly_limit=2

After this modification, the maximum number of kernels retained will be two.

If a system update is made and a new kernel version is added, this setting will allow the removal of the oldest unused kernel, while keeping the three most recent kernel versions.

Here is an example of a /etc/yum.conf configuration file after modification. As you can see, it is set to keep only the last two kernel versions:

[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=2

Here are a couple of other commands that you might find useful:

root@host:~# package-cleanup remove old kernels
root@host:~# package-cleanup --oldkernels --keepdevel

For more in-depth details about this and other kernel related topics visit: 
An Overview of Unix Kernels
Linux Kernel in a Nutshell

Join Now And Get 33% Off!

Check out our newest discounts and specials!!!
We’ve Lowered our pricing on all of our Dedicated and Virtual Private Servers! Get in on the action NOW!!!

Author Bio

About the Author: David Singer

I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....

Get 33% off the first 3 months on a new VPS! Find out why 30,000 customers have chosen our Best-in-Class Performance & 24x7x365 Support