Uninstalling old kernels

Hi all, :wave:

I´ve got a question regarding removing old kernels.

Normally I go about it like this:

apt-get autoremove --purge

, which also removes old kernels.
The current kernel and the one before won´t be touched.

This has worked well in the past when I was using Lubuntu. But now with Linux Lite 6.2 it seems that the 3 latest kernels are kept when issuing the above command.
I don´t know if it´s a Lite “speciality” or whether ubuntu itself has changed its policies. :thinking:

Currently my system looks like this:

dpkg -l | grep -E 'linux-image|linux-headers'
ii  linux-headers-5.15.0-52                           5.15.0-52.58                               all          Header files related to Linux kernel version 5.15.0
ii  linux-headers-5.15.0-52-generic                   5.15.0-52.58                               amd64        Linux kernel headers for version 5.15.0 on 64 bit x86 SMP
ii  linux-headers-5.15.0-78                           5.15.0-78.85                               all          Header files related to Linux kernel version 5.15.0
ii  linux-headers-5.15.0-78-generic                   5.15.0-78.85                               amd64        Linux kernel headers for version 5.15.0 on 64 bit x86 SMP
ii  linux-headers-5.15.0-79                           5.15.0-79.86                               all          Header files related to Linux kernel version 5.15.0
ii  linux-headers-5.15.0-79-generic                   5.15.0-79.86                               amd64        Linux kernel headers for version 5.15.0 on 64 bit x86 SMP
ii  linux-headers-generic                             5.15.0.79.76                               amd64        Generic Linux kernel headers
ii  linux-image-5.15.0-52-generic                     5.15.0-52.58                               amd64        Signed kernel image generic
ii  linux-image-5.15.0-78-generic                     5.15.0-78.85                               amd64        Signed kernel image generic
ii  linux-image-5.15.0-79-generic                     5.15.0-79.86                               amd64        Signed kernel image generic
ii  linux-image-generic                               5.15.0.79.76                               amd64        Generic Linux kernel image

Actually I want to keep just 2 of them, the current one of course, and the one before that.

When looking at the oldest of the kernels (the one ending in “52”) I see three entries.

According to Kernel › Wiki › ubuntuusers.de removing any kernel should be like this:

sudo apt-get remove --purge linux-image-5.15.0-52-generic linux-headers-5.15.0-52-generic

But I´m not sure about the entry linux-headers-5.15.0-52. What is this one for :question:
The source doesn´t mention it. I guess it should be uninstalled as well :question:
What do you think of it?

Oh, BTW: how many kernels do you keep?

Many thanks and many greetings from Rosika :slightly_smiling_face:

Hi Rosika,
This may help

If you want to purge one specific kernel providing package you can do so via the following command in a terminal:

sudo apt-get purge linux-image-4.12.0-12-generic
sudo dpkg --purge linux-headers-4.12.0-12 linux-headers-4.12.0-12-generic

This will also purge the corresponding linux-image-extra package if it is installed e.g. linux-image-extra-4.12.0-12-generic. It will not purge linux-headers-4.12.0-12, if there is another linux-headers-4.12.0-12 flavor installed besides -generic. 

be careful, there may be a reason it keeps 3.
I usually have 3 kernels, more in Void
Dont forget to redo update-grub if you remove kernels
Regards
Neville

2 Likes

Hi Neville, :wave:

thanks a lot for your very quick answer.

I´m a bit confused though.

  • this is a mixture of apt and dpkg commands, right? I´ve never done it this way before… :thinking:

  • I´ve no linux-image-extra , but rather linux-headers-5.15.0-52. Would either of the commands you provided delete that one too?

I see. That´s a good thought. Perhaps I should keep all of them indeed.

So sudo apt-get autoremove --purge will take care of that.

Thanks for your help, Neville. :heart:

Many greetings from Rosika :slightly_smiling_face:

1 Like

It came from here
https://help.ubuntu.com/community/RemoveOldKernels

I cant comment, have never done it.
Debian seems to look after it automatically
Void has a special command vkpurge

2 Likes

I’ve removed old kernels manually on CentOS. For Ubuntu I let it manage those and it automatically removes old kernels, while saving three (be default), with sudo apt autoremove -y.

Saving three is one better than saving two I guess. If you need to revert to an old kernel you would think one would be enough. If there were a couple rapid releases (small patches), maybe a third one would be a good idea. The kernel doesn’t use much disk space anyway.

3 Likes

Thanks for your new comments.

@nevj :

I´ll look up the info provided by your link. Thanks for posting it, Neville.

@pdecker :

Thanks to you as well.

So this is similar to the command I´ve been using so far: sudo apt-get autoremove --purge.
And you say it saves three kernels this way. So it seems to be a ubuntu poilicy then.

Good point. I´ve never thought of that aspect.

Thanks to both of you for providing your views on the matter. :heart:

Cheers from Rosika :slightly_smiling_face:

1 Like

If I have a problem that seems to lead to a problem in the current kernel, I back up the data, flush the distro, and try something else.

4 Likes

Hi @Rosika,

I remember reading there was a way to control the number of kernels to keep. Had to look it up, but here it is.

“Edit the file ‘/etc/dnf/dnf.conf’, changing the numeric value in installonly_limit=3 to a number of your choosing.”

I just looked and see I have 4 kernels installed. I have 10 GB of free space on my “/” partition, so I do not worry about the number of kernels installed. Yes, maybe once a month, I will check to see how many kernels are installed. I only remember once using Timeshift to back out a kernel install.

Best wishes,
Howard

3 Likes

The great joy of Linux (and BSD) is that one can , sometimes with a great deal of effort, find a setting like that in an ASCII file that can be viewed and edited.

Dont let anyone ever take that away. It is part of your Unix heritage, part of what ‘free’ means.

Well done Howard.

4 Likes

Hi Howard, :wave:

thanks so much for looking it up for me. :heart:

However my situation seems to be different.
I looked for the dnf.conf file and it seems there´s no such thing on Linux Lite 6.2, not even in another path. :thinking:

That made me curious. What about other systems?

So I looked it up on my Debian VM and my Bodhi Linux VM:

Debian doesn´t have it either, neither does Bodhi.
I wonder what distro uses ‘/etc/dnf/dnf.conf’. Do you have any further information about that?

Thanks a lot, Howard.

Many greetings from Rosika :slightly_smiling_face:

1 Like

It would seem dnf is only used to manage packages in rpm systems, not in deb systems

3 Likes

Ah, that´s it then.
Thanks for the clarification, Neville. :+1:

Well, I think it´s best for me to keep three kernels, as my system would suggest anyway.
So, no need to take any action on my part.

Kernel management is an interesting topic at any rate.

Thanks a lot and many greetings from Rosika :slightly_smiling_face:

1 Like

It is interesting in Gentoo. You can compile any kernel you wish. They all seem to work. The kernels are compiled separately from all the other packages, so they are not part of the package system at all.

Kernels include driver modules and firmware (those binary blobs that everyone argues about). So kernels are the most hardware dependent part of your OS. They are also the best managed… problems are quite rare. We hardly ever hear of a new kernel release having issues.

2 Likes

Thanks for the additional info, Neville.

I know you have abundant experience with Gentoo, so you´re pretty much ahead of me.
Complinig kernels oneself is something I never would´ve thought of.

I found an article here: https://www.makeuseof.com/tag/compile-linux-kernel/ .
I might look into it.

Would compiling a kernel work in Gentoo only or can it be any other Linux distro as well :question:

Cheers from Rosika :slightly_smiling_face:

You can compile any package ( including a kernel) in any Linux distro. You just need to install what are called the build tools… things like gcc, make, linux headers…

I would suggest you try something smaller than a kernel first…a gentoo kernel takes about 2 hours on my core i5 .

There are instructions.

2 Likes

Thanks Neville, :heart:

I actually compiled a package in the past; it is a very long time ago though.
I the recent past I didn´t need to do any compilation. So I´m vaguely familiar with the process although I´d look up the how-to´s before doing it again.

Well, the way you put it, it makes sense, of course. The kernel is just another package, albeit a pretty big one.

Still: it would never have occurred to me to compile a kernel myself. :blush:

Thanks again for the info, Neville.

Cheers from Rosika :slightly_smiling_face:

1 Like

The steps are

  • download source
  • configure (be sure all the needed bits are present )
  • make (the actual compile)
  • make install (put the binary where you want it)
1 Like

Thanks a lot providing the steps, Neville. :heart:

One question still comes to mind: What about any potential unmet dependencies?

If the package to be installed needs some dependencies that are not already present on the system, would the configure-part fail then?

That is what configure is supposed to sort out.
If configure fails, you cannot proceed… you have to go find the missing pieces.
Configure is a huge job for a kernel… you have to find all the right drivers and firmware for your hardware. There are programs which do it… the Gentoo one is called genkernel

2 Likes

Thanks Neville.

Just wanted to make sure. :wink:

I guess the description of the packeage to be installed would list needed dependencies so that one would be able to get the info beforehand.

That seems to be a great advantage. Gentoo looks better by the minute. :wink:

Thanks and cheers from Rosika :slightly_smiling_face: