Safely Remove Old Kernels in Linux 9

Share on Social Media

In this configuration guide, you will learn how to safely remove old kernels in Linux 9. #centlinux #linux #linuxkernel

What is a Linux Kernel?:

Linux kernel is a free and open-source, monolithic, modular, multitasking, Unix-like operating system kernel. It was originally authored in 1991 by Linus Torvalds for his i386-based PC, and it was soon adopted as the kernel for the GNU operating system, which was written to be a free (libre) replacement for Unix.

Since the late 1990s, it has been included as part of a large number of operating system distributions, many of which are commonly also called Linux.

Linux is provided under the GNU General Public License version 2 only, but it contains files under other compatible licenses.

Linux is deployed on a wide variety of computing systems, such as embedded devices, mobile devices (including its use in the Android operating system), personal computers, servers, mainframes, and supercomputers. It can be tailored for specific architectures and for several usage scenarios using a family of simple commands (that is, without the need of manually editing its source code before compilation); privileged users can also fine-tune kernel parameters at runtime.

Most of the Linux kernel code is written using the GNU extensions of GCC to the standard C programming language and with the use of architecture-specific instructions (ISA) in limited parts of the kernel. This produces a highly optimized executable (vmlinux) with respect to utilization of memory space and task execution times. (Source: Wikipedia)

Video to Remove Old Kernels in Linux:

YouTube player

Problem Definition:

It is a very common practice to update Linux operating systems, periodically and especially at the time of installing a new software. On each installation of new Linux Kernel, the Yum keeps your previous Kernels intact and may keep maximum last 3 Kernels at a time. It is the default limit and you can customize it according to your requirement.

A typical GRUB menu with mutiple Linux Kernels may look like the following.


Continue reading this article to get rid of old Linux kernels and keep your GRUB boot menu tidy.

Environment Specification:

We are using a minimal Rocky Linux 9 virtual machine with following specifications.

  • CPU – 3.4 Ghz (2 cores)
  • Memory – 2 GB
  • Storage – 20 GB
  • Operating System – Rocky Linux release 9.0 (Blue Onyx)
  • Hostname –
  • IP Address –

Check installed Linux Kernels:

By using a ssh client, Login to your Rocky Linux 9 server as root user.

Execute the following rpm command, to list the installed kernels in your Linux operating system.

# rpm -q kernel

Usually the latest Linux Kernel should be active on your operating system. Unless someone has explicitly set an older version as default Linux kernel.

You can confirm it by executing following commands.

# uname -r

# grubby --default-kernel

If the above command returns a kernel other than the latest one, then you have to set the default kernel for GRUB.

Set Default Linux Kernel:

List down the available Linux kernels in /boot directory.

# ls /boot/vm*

Execute the following command to set a default kernel for GRUB.

# grubby --set-default /boot/vmlinuz-5.14.0-70.26.1.el9_0.x86_64
The default is /boot/loader/entries/f118dcfb3b884e518d8d105671e2c0ea-5.14.0-70.26.1.el9_0.x86_64.conf with index 0 and kernel /boot/vmlinuz-5.14.0-70.26.1.el9_0.x86_64

Remove Old Kernels in Linux:

By default, Yum Package Manager installs Linux kernels separately, while keeping the old versions intact for fallback support.

This behavior can be controlled by two Yum Package Manager directives.

  • installonlypkgs – Space separated list of packages who will never update by package manager.
  • installonly_limit – Maximum number of versions that can be installed simultaneously for any single package listed in the installonlypkgs directive.

Check the current value of installonly_limit directive.

# grep limit /etc/dnf/dnf.conf

Execute following dnf command at Linux Bash prompt to remove the old versions of Linux kernels.

# dnf -y remove --oldinstallonly --setopt installonly_limit=2 kernel
Dependencies resolved.
 Package             Arch        Version                    Repository     Size
 kernel              x86_64      5.14.0-70.13.1.el9_0       @minimal        0
 kernel              x86_64      5.14.0-70.22.1.el9_0       @baseos         0
 kernel-core         x86_64      5.14.0-70.13.1.el9_0       @minimal       73 M
 kernel-core         x86_64      5.14.0-70.22.1.el9_0       @baseos        73 M
 kernel-modules      x86_64      5.14.0-70.13.1.el9_0       @minimal       20 M
 kernel-modules      x86_64      5.14.0-70.22.1.el9_0       @baseos        20 M

Transaction Summary
Remove  6 Packages

Freed space: 185 M
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Erasing          : kernel-5.14.0-70.22.1.el9_0.x86_64                     1/6
  Running scriptlet: kernel-5.14.0-70.22.1.el9_0.x86_64                     1/6
  Erasing          : kernel-5.14.0-70.13.1.el9_0.x86_64                     2/6
  Running scriptlet: kernel-5.14.0-70.13.1.el9_0.x86_64                     2/6
  Erasing          : kernel-modules-5.14.0-70.13.1.el9_0.x86_64             3/6
  Running scriptlet: kernel-modules-5.14.0-70.13.1.el9_0.x86_64             3/6
  Erasing          : kernel-modules-5.14.0-70.22.1.el9_0.x86_64             4/6
  Running scriptlet: kernel-modules-5.14.0-70.22.1.el9_0.x86_64             4/6
  Running scriptlet: kernel-core-5.14.0-70.22.1.el9_0.x86_64                5/6
  Erasing          : kernel-core-5.14.0-70.22.1.el9_0.x86_64                5/6
  Running scriptlet: kernel-core-5.14.0-70.22.1.el9_0.x86_64                5/6
  Running scriptlet: kernel-core-5.14.0-70.13.1.el9_0.x86_64                6/6
  Erasing          : kernel-core-5.14.0-70.13.1.el9_0.x86_64                6/6
  Running scriptlet: kernel-core-5.14.0-70.13.1.el9_0.x86_64                6/6
  Verifying        : kernel-5.14.0-70.13.1.el9_0.x86_64                     1/6
  Verifying        : kernel-5.14.0-70.22.1.el9_0.x86_64                     2/6
  Verifying        : kernel-core-5.14.0-70.13.1.el9_0.x86_64                3/6
  Verifying        : kernel-core-5.14.0-70.22.1.el9_0.x86_64                4/6
  Verifying        : kernel-modules-5.14.0-70.13.1.el9_0.x86_64             5/6
  Verifying        : kernel-modules-5.14.0-70.22.1.el9_0.x86_64             6/6



You may notice that, instead of altering the installonly_limit directive value in dnf.conf file, we are setting it at the runtime for current dnf command only.

Reboot your Linux operating system now.

# reboot

Check the GRUB boot menu, The old kernels has been removed from the list.



In this configuration guide, you have learned how to safely remove old kernels in Linux 9. If you face difficulty in understanding the above article than you should buy and read  Practical Guide to Linux Commands, Editors, and Shell Programming (PAID LINK) by Mark Sobell & Matthew Helmke.