24

I might be encountering odd symptoms resulting from competing kernels in CentOS 7. So how do I safely delete the old kernels? And how do I know which kernel is the newest one?

Below is the terminal output I get at the moment when researching this on the server in question. Note that I tried package-cleanup but it leaves the same 2 kernels:

The instructions in this tutorial say that the output of the following two commands should match, but you can see that they do not match, even after a reboot:

[root@localhost ~]# rpm -qa kernel |sort -V |tail -n 1
kernel-3.10.0-229.el7.x86_64
[root@localhost ~]# uname -r
3.10.0-229.14.1.el7.x86_64

The remaining commands confirm that there are two kernels, and illustrate attempts to delete the old one.

[root@localhost ~]# cd /usr/src/kernels
[root@localhost kernels]# ls -al
total 16
drwxr-xr-x.  4 root root 4096 Oct  2 12:55 .
drwxr-xr-x.  4 root root 4096 Oct  2 13:15 ..
drwxr-xr-x. 22 root root 4096 Oct  2 12:55 3.10.0-229.14.1.el7.x86_64
drwxr-xr-x. 22 root root 4096 Oct  2 12:35 3.10.0-229.el7.x86_64
[root@localhost kernels]# rpm -q kernel
kernel-3.10.0-229.el7.x86_64
kernel-3.10.0-229.14.1.el7.x86_64
[root@localhost kernels]# package-cleanup --oldkernels=1
Loaded plugins: fastestmirror
Usage: 
    package-cleanup: helps find problems in the rpmdb of system and correct them

    usage: package-cleanup --problems or --leaves or --orphans or --oldkernels
Command line error: --oldkernels option does not take a value
[root@localhost kernels]# package-cleanup --oldkernels
Loaded plugins: fastestmirror
No old kernels to remove
[root@localhost kernels]# rpm -q kernel
kernel-3.10.0-229.el7.x86_64
kernel-3.10.0-229.14.1.el7.x86_64
[root@localhost kernels]# 

I also opened up /etc/yum.conf and set installonly_limit=1, but this resulted in an error from a subsequent yum update command saying that 1 is outside the range of acceptable values for installonly_limit.

I assume that 3.10.0-229.14.1.el7.x86_64 is the newest, but how can I know this? Also, the boot options seem to offer multiple kernels to choose from. And the opportunities for confusion get worse when the system auto-boots from the first kernel on the list of options.

Can someone please explain how this works, and in specific, how to safely delete old kernels so that kernel version can be eliminated as a possible cause of odd symptoms? I want to make sure that the most recent kernel is the only kernel that can ever run, no matter how the system is restarted.

3
  • Walking out the door so I can't write up a full answer right now. Will try to check back later. yum-utils used to do this (not sure if it still does). if-not-true-then-false.com/2012/…
    – 0xSheepdog
    Oct 2, 2015 at 22:39
  • @0xSheepdog Yes, I am using those commands from a similar tutorial, but the CentOS 7 server will not allow installonly_limit=1. I am getting funny symptoms that might be resulting from two kernel versions, so I would like to have only one version, if possible, or at least to have a definitive explanation of why it is not possible to have less than 2 versions, if in fact it is not possible.
    – CodeMed
    Oct 2, 2015 at 22:42
  • 2
    I would suggest changing your "keep" criteria to "booted kernel" as opposed to simply newest -- for various reasons, you may not be booting to the newest kernel.
    – Jeff Schaller
    Oct 2, 2015 at 23:41

5 Answers 5

32

package-cleanup --oldkernels --count=1 delete all kernels except latest.

5
  • 1
    Thanks Fractal Turtle for brief answer. I also want to you to safely keep at least two latest kernel. package-cleanup --oldkernels --count=2 Jun 1, 2017 at 3:58
  • 2
    I run package-cleanup --oldkernels --count=1. After reboot, I still get two kernels: CentOS Linux (4.4.6-1.el7.elrepo.x86_64) 7 (Core) and CentOS Linux (3.10.0-514.2.2.el7.x86_64) 7 (Core), do you know how to just keep one?
    – Ron
    Sep 18, 2017 at 0:44
  • Can someone answer @Ron 's question? Apr 12, 2018 at 13:26
  • 1
    fyi, package-cleanup is part of: yum install yum-utils
    – TiloBunt
    Nov 5, 2018 at 19:18
  • 2
    This solution no longer works with Centos 8 onwards. Gives the error message package-cleanup has to be executed with one of the options: --dupes, --leaves, --orphans, --problems or --cleandupes Refer my answer below for a working solution for Centos 8.
    – Mrinal
    Mar 19, 2021 at 12:30
14

RHEL and CentOS >= 8 (and Fedora with DNF).

RHEL 8+ use a new package manager (DNF / yum 4, see yum2dnf), which recommend:

dnf remove $(dnf repoquery --installonly --latest-limit=-2)

--latest-limit=-2 means to leave two last versions - current and the previous one;

--latest-limit=-1 will remove all except current one.

There's also yum remove --oldinstallonly to remove old installonly packages, not just kernels (?).

(this command requires dnf >= 4.2.17-4 bug #1774670)

Older RHEL and CentOS ( <=7 )

The command package-cleanup was used to remove older kernel with yum <4. Use --count=1 to keep just the latest. The program will never remove the kernel currently used ("Not removing kernel 3.10.0-327.el7 because it is the running kernel"), so you may end-up with two kernels in /boot... until you reboot and re-run the command.

So the command is

package-cleanup --oldkernels --count=1 

The manpage document three options:

--oldkernels
          Remove old kernel and kernel-devel packages.
--count <COUNT>
          Number of duplicate/kernel packages to keep on the system (default 2)
--keepdevel
          Do not remove kernel-devel packages when removing kernels
1
  • 1
    This solution no longer works with Centos 8 onwards. Gives the error message package-cleanup has to be executed with one of the options: --dupes, --leaves, --orphans, --problems or --cleandupes Refer my answer below for a working solution for Centos 8.
    – Mrinal
    Mar 19, 2021 at 12:33
5

For Centos 8 onwards (Yum version 4) you can use the command

yum remove --oldinstallonly

This removes all old versions of the kernel. Use when you want to have only the latest release version of the kernel.

1

Use yum upgrade instead of yum update

Then it will only keep three kernels. If you use update then it will fill up your /boot

-3

according to this:

run rpm -q kernel to show installed kernel

then run yum remove kernel <shown kernel from output above> to remove the kernel

reboot ur computer

edit: this actualy work for @Ron's problem, but make sure elrepo kernel is running correctly

2
  • 2
    How do you avoid removing your current kernel?
    – Jeff Schaller
    Jun 10, 2018 at 14:59
  • IDK specific command for that @JeffSchaller I guess, to make sure u are not removing current kernel, u sud check it first by using uname -r then delete the unused kernel but shown from that command, Jun 11, 2018 at 12:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .