Removing the default kernel aka the loaded kernel in CentOS 8 maybe challenging because the package is protected and cannot be removed by the yum or dnf.
Here is the case: an elrepo kernel-ml loaded and the dnf prints it cannot remove the package, because it is protected:
[root@srv ~]# dnf remove kernel-ml kernel-ml-core kernel-ml-modules Error: Problem: The operation would result in removing the following protected packages: kernel-ml-core (try to add '--skip-broken' to skip uninstallable packages or '--nobest' to use not only best candidate packages) [root@srv ~]# uname -a Linux srv.localhost 5.10.4-1.el8.elrepo.x86_64 #1 SMP Tue Dec 29 11:04:23 EST 2020 x86_64 x86_64 x86_64 GNU/Linux [root@srv ~]# grubby --default-kernel /boot/vmlinuz-5.10.4-1.el8.elrepo.x86_64
The system is booted up with the kernel we are trying to remove, which is impossible.
The solution is to set a new default kernel and load it. Then dnf will be able to remove the first kernel.
For CentOS 7, just use the yum instead of dnf command.
Using grubby is really easy and straightforward:
STEP 1) List all installed and available to boot kernels
[root@srv ~]# grubby --info=ALL |grep ^kernel kernel="/boot/vmlinuz-5.10.4-1.el8.elrepo.x86_64" kernel="/boot/vmlinuz-4.18.0-259.el8.x86_64" kernel="/boot/vmlinuz-4.18.0-257.el8.x86_64" kernel="/boot/vmlinuz-0-rescue-45e12f0814fd4947b99cbdcb88950361"
STEP 2) Select the kernel to load the next time
[root@srv ~]# grubby --set-default "/boot/vmlinuz-4.18.0-259.el8.x86_64" The default is /boot/loader/entries/45e12f0814fd4947b99cbdcb88950361-4.18.0-259.el8.x86_64.conf with index 1 and kernel /boot/vmlinuz-4.18.0-259.el8.x86_64
The parameter for “–set-default” is the whole string as it is reported from the previous grubby command.
The grubby reports this is the kernel with index 1 and kernel file – /boot/vmlinuz-4.18.0-259.el8.x86_64.
That’s all to load a different kernel from the currently loaded default kernel.
Checking the default before a reboot is easy:
[root@srv ~]# grubby --default-kernel /boot/vmlinuz-4.18.0-259.el8.x86_64
Despite a new kernel is selected and there is still the error for the currently loaded kernel when trying to remove it. A reboot of the server is required to load a different kernel.
STEP 3) Remove the previous kernel with yum/dnf. In this case the elrepo kernel.
[root@srv ~]# uname -a Linux srv.localhost 4.18.0-259.el8.x86_64 #1 SMP Mon Dec 21 21:05:36 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux [root@srv ~]# dnf remove kernel-ml kernel-ml-core kernel-ml-modules Dependencies resolved. =============================================================================== Package Arch Version Repository Size =============================================================================== Removing: kernel-ml x86_64 5.10.4-1.el8.elrepo @elrepo-kernel 0 kernel-ml-core x86_64 5.10.4-1.el8.elrepo @elrepo-kernel 67 M kernel-ml-modules x86_64 5.10.4-1.el8.elrepo @elrepo-kernel 25 M Transaction Summary =============================================================================== Remove 3 Packages Freed space: 91 M Is this ok [y/N]: y Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Erasing : kernel-ml-5.10.4-1.el8.elrepo.x86_64 1/3 Erasing : kernel-ml-modules-5.10.4-1.el8.elrepo.x86_64 2/3 Running scriptlet: kernel-ml-modules-5.10.4-1.el8.elrepo.x86_64 2/3 Running scriptlet: kernel-ml-core-5.10.4-1.el8.elrepo.x86_64 3/3 Erasing : kernel-ml-core-5.10.4-1.el8.elrepo.x86_64 3/3 Running scriptlet: kernel-ml-core-5.10.4-1.el8.elrepo.x86_64 3/3 Verifying : kernel-ml-5.10.4-1.el8.elrepo.x86_64 1/3 Verifying : kernel-ml-core-5.10.4-1.el8.elrepo.x86_64 2/3 Verifying : kernel-ml-modules-5.10.4-1.el8.elrepo.x86_64 3/3 Installed products updated. Removed: kernel-ml-5.10.4-1.el8.elrepo.x86_64 kernel-ml-core-5.10.4-1.el8.elrepo.x86_64 kernel-ml-modules-5.10.4-1.el8.elrepo.x86_64 Complete!
Disable or completely remove the elrepo
Disable the elrepo with
[root@srv ~]# grep -ir enable /etc/yum.repos.d/elrepo.repo enabled=1 enabled=0 enabled=0 enabled=0 [root@srv ~]# sed -i 's/enabled=1/enabled=0/g' /etc/yum.repos.d/elrepo.repo [root@srv ~]# grep -ir enable /etc/yum.repos.d/elrepo.repo enabled=0 enabled=0 enabled=0 enabled=0
Or completely remove the elrepo repo with:
[root@srv ~]# dnf remove elrepo-release Dependencies resolved. =============================================================================== Package Arch Version Repository Size =============================================================================== Removing: elrepo-release noarch 8.2-1.el8.elrepo @@commandline 5.0 k Transaction Summary =============================================================================== Remove 1 Package Freed space: 5.0 k Is this ok [y/N]: y Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Erasing : elrepo-release-8.2-1.el8.elrepo.noarch 1/1 warning: /etc/yum.repos.d/elrepo.repo saved as /etc/yum.repos.d/elrepo.repo.rpmsave Verifying : elrepo-release-8.2-1.el8.elrepo.noarch 1/1 Installed products updated. Removed: elrepo-release-8.2-1.el8.elrepo.noarch Complete!
And after removing the kernel, the index is updated accordingly:
[root@srv ~]# grubby --default-kernel /boot/vmlinuz-4.18.0-259.el8.x86_64 [root@srv ~]# grubby --info=ALL |grep ^kernel kernel="/boot/vmlinuz-4.18.0-259.el8.x86_64" kernel="/boot/vmlinuz-4.18.0-257.el8.x86_64" kernel="/boot/vmlinuz-0-rescue-45e12f0814fd4947b99cbdcb88950361" [root@srv ~]# grubby --default-index 0
Before removing the elrepo kernel, the current kernel was with index 1, now it is with index 0. When using dnf/yum to remove a (the elrepo) kernel no need to do anything else with grub 2 or other command. The dnf/yum updates and removes the necessary files to have healthy grub 2 options.
Fantastic!!