Site icon CentLinux

How to remove Old Kernels in Linux 9

Share on Social Media

Learn how to remove old kernels in Linux 9 with our step-by-step guide. Free up disk space, streamline system performance, and enhance security by safely removing outdated kernel versions from your Linux system. #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)

Why Remove Old Kernels in Linux?

Removing old Linux kernels can help in maintaining a clean and efficient system. Here are some reasons why removing old kernels might be beneficial:

  1. Disk Space: Each kernel installation occupies disk space. Over time, multiple kernel installations can accumulate and consume a significant amount of disk space. Removing old kernels frees up valuable disk space for other purposes.
  2. System Performance: The bootloader menu, such as GRUB, can become cluttered with numerous kernel options. This can increase the time it takes for the system to boot as it scans through the available kernels. Removing old kernels streamlines the bootloader menu and improves boot times.
  3. Security: Older kernels may contain vulnerabilities that have been patched in newer versions. By removing old kernels and keeping the system up-to-date with the latest kernel releases, you ensure that security patches are applied promptly, reducing the risk of exploitation.
  4. Maintenance: Managing multiple kernel versions can complicate system maintenance and troubleshooting. Removing old kernels simplifies system management and reduces the likelihood of conflicts or compatibility issues between different kernel versions.
  5. Backup and Restore: When performing system backups or snapshots, old kernel files are often included, increasing backup sizes and restore times. Removing old kernels reduces the backup size and makes backup and restore processes more efficient.

Overall, removing old Linux kernels helps optimize disk space usage, improve system performance, enhance security, simplify maintenance, and streamline backup and restore processes. It is recommended to periodically review and remove old kernels to keep the system clean and efficient.

Recommended Online Training: Learn Bash Shell in Linux for Beginners

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.

GRUB Old Linux Kernels

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.

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
kernel-5.14.0-70.13.1.el9_0.x86_64
kernel-5.14.0-70.22.1.el9_0.x86_64
kernel-5.14.0-70.26.1.el9_0.x86_64

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
5.14.0-70.26.1.el9_0.x86_64

# grubby --default-kernel
/boot/vmlinuz-5.14.0-70.26.1.el9_0.x86_64

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*
/boot/vmlinuz-0-rescue-f118dcfb3b884e518d8d105671e2c0ea
/boot/vmlinuz-5.14.0-70.13.1.el9_0.x86_64
/boot/vmlinuz-5.14.0-70.22.1.el9_0.x86_64
/boot/vmlinuz-5.14.0-70.26.1.el9_0.x86_64

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

Delete Old Linux Kernels

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.

Check the current value of installonly_limit directive.

# grep limit /etc/dnf/dnf.conf
installonly_limit=3

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
================================================================================
Removing:
 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

Removed:
  kernel-5.14.0-70.13.1.el9_0.x86_64
  kernel-5.14.0-70.22.1.el9_0.x86_64
  kernel-core-5.14.0-70.13.1.el9_0.x86_64
  kernel-core-5.14.0-70.22.1.el9_0.x86_64
  kernel-modules-5.14.0-70.13.1.el9_0.x86_64
  kernel-modules-5.14.0-70.22.1.el9_0.x86_64

Complete!

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.

GRUB After Removing Old Linux Kernels

Video: How to Remove Old Linux Kernels

Final Thoughts

Congratulations on successfully learning how install Mainline Kernel on Rocky Linux 9! By upgrading to the latest kernel version, you’ve taken a significant step towards enhancing the performance, security, and compatibility of your system. Enjoy the benefits of the latest kernel updates and explore the new features and improvements they bring. Remember to periodically review and remove old kernels to maintain a clean and efficient system. Happy computing! 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.

Exit mobile version