The kernel is the bridge between software and hardware, and it is part of an operating system that interacts with the hardware.

In normal circumstances, the number of installed kernels does not affect the system’s performance, but it is still recommended to remove old unused kernels as it frees up space. Before we start removing old unused kernels, it is essential to note that you should always have at least two kernels on your system at any time.

Remove Old Kernels

It is recommended to update the whole system’s packages and active kernel before removing the old kernels. To do so, execute the command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Delete-Old-Unused-Kernels-CentOS-8-01.png" data-lazy- height="150" src="data:image/svg xml,” width=”974″>

After updating, to check the current kernel being used, use the following command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Delete-Old-Unused-Kernels-CentOS-8-02.png" data-lazy- height="93" src="data:image/svg xml,” width=”593″>

To list all the kernels installed in your system, use the command given below:

Output:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Delete-Old-Unused-Kernels-CentOS-8-03.png" data-lazy- height="154" src="data:image/svg xml,” width=”635″>

As you can see, I have three Kernels installed on my CentOS 8 system.

Now we will discuss how to remove old inactive kernels from our systems.

As mentioned above, you should never remove all of the old inactive kernels and should always have at least two kernels (1 active – 1 inactive) in your system.

Remove Old Kernels using DNF Command

We can use the DNF command to remove old unused kernels. Simply execute the “dnf remove” command with the :

$ sudo dnf remove –oldinstallonly –setopt installonly_limit=2 kernel

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Delete-Old-Unused-Kernels-CentOS-8-04.png" data-lazy- height="441" src="data:image/svg xml,” width=”974″>

As you can see in the screenshot attached above, the terminal is asking to remove three packages. Allow the removal by pressing “y” and hitting “Enter”:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Delete-Old-Unused-Kernels-CentOS-8-05.png" data-lazy- height="302" src="data:image/svg xml,” width=”974″>

After successful removal of the older kernel, if we execute the “rpm -q kernel” command again:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Delete-Old-Unused-Kernels-CentOS-8-06.png" data-lazy- height="122" src="data:image/svg xml,” width=”641″>

You can witness, we have only two kernels left.

If you have more than two kernels installed, the command given above will leave two kernels installed on your system and uninstall all other older and unused Kernel versions.

You can change the number by giving a different value to –setopt installonly_limit=2.

In the command above, the –setopt is used to modify the default value from /etc/dnf/dnf.conf. If you don’t command a value, then dnf will take the default value from dnf.conf.

This command will only work in case the latest kernel is active. If an older version is functional, the command will unsuccessfully try to remove it.

Another method works on older versions of CentOS, but with the release of CentOS 8, this method has stopped working.

So, if you’re still using an older version of CentOS/RHEL, you can still use the command given below to remove old kernels:

$ sudo package-cleanup –oldkernels –count=2

The command given above will leave two kernels installed and uninstall all other older and unused Kernel versions on your older version of the CentOS/RHEL system.

Conclusion

This article provides an extensive guide on how to remove old and unused kernels from CentOS 8. We also discussed some good practices to keep in mind while removing inactive kernels.

We learned two different methods we can use to remove old and unused kernels from our system. One of these methods has become obsolete on newer versions of CentOS operating systems but can still come in handy if you’re using an older version of CentOS.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/7011409B3A384F43A8417D1DAC68D179-150×150.jpg60e8c57f551f3.jpg" height="112" src="data:image/svg xml,” width=”112″>

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.