42

I'm managing a couple of servers for core services (NTP, DNS, etc) and it just occured to me that one of the servers seems to keep the 3 latest kernels, instead of 2 on the others :

nul@quark:~$ sudo apt-get autoremove --purge
Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
nul@quark:~$ dpkg -l |grep linux-image
ii  linux-image-3.13.0-51-generic       3.13.0-51.84                     amd64        Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-3.13.0-52-generic       3.13.0-52.85                     amd64        Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-3.16.0-37-generic       3.16.0-37.49~14.04.1             amd64        Linux kernel image for version 3.16.0 on 64 bit x86 SMP
ii  linux-image-extra-3.13.0-51-generic 3.13.0-51.84                     amd64        Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-extra-3.13.0-52-generic 3.13.0-52.85                     amd64        Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-extra-3.16.0-37-generic 3.16.0-37.49~14.04.1             amd64        Linux kernel extra modules for version 3.16.0 on 64 bit x86 SMP
ii  linux-image-generic                 3.13.0.52.59                     amd64        Generic Linux kernel image
ii  linux-image-generic-lts-utopic      3.16.0.37.29                     amd64        Generic Linux kernel image

...

nul@dwarf:~$ sudo apt-get autoremove --purge
Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
nul@dwarf:~$ dpkg -l |grep linux-image
ii  linux-image-3.13.0-52-generic       3.13.0-52.85                     amd64        Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-3.16.0-37-generic       3.16.0-37.49~14.04.1             amd64        Linux kernel image for version 3.16.0 on 64 bit x86 SMP
ii  linux-image-extra-3.13.0-52-generic 3.13.0-52.85                     amd64        Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-extra-3.16.0-37-generic 3.16.0-37.49~14.04.1             amd64        Linux kernel extra modules for version 3.16.0 on 64 bit x86 SMP
ii  linux-image-generic                 3.13.0.52.59                     amd64        Generic Linux kernel image
ii  linux-image-generic-lts-utopic      3.16.0.37.29                     amd64        Generic Linux kernel image

All servers are maintained identically, don't know what I could have done, must be a parameter somewhere but can't find it!

Please feed my curiosity! Thank you

1 Answer 1

47

UPDATE 2022-10-03:

It appears that Canonical/Debian has now implemented the settings directly into apt since version 2.4.5 to protect two kernels

apt changelog apt

apt (2.4.5) unstable; urgency=medium

  * Only protect two kernels, not last installed one (LP: #1968154)
  * Fix segfault in CacheSetHelperAPTGet::tryVirtualPackage()

 -- Julian Andres Klode <[email protected]>  Fri, 08 Apr 2022 12:22:23 +0200

The direct link to the bug https://bugs.launchpad.net/ubuntu/+source/apt/+bug/1968154


Older info:

There is a file that is auto-generated that tells apt-get what kernels to autoremove and which ones to keep.
The file that tells apt-get which kernels they are is /etc/apt/apt.conf.d/01autoremove-kernels which is generated from /etc/kernel/postinst.d/apt-auto-removal.

Usually what happens is that when you are receiving kernel updates, when the kernel version changes, say from 3.13 to 3.16, /etc/apt/apt.conf.d/01autoremove-kernels is then updated to keep the 3.16* kernels and is then set to remove all of the 3.13 kernels unless specified by the generating script to not be removed.

From the apt-auto-removal script:

# Author: Steve Langasek 
#
# Mark as not-for-autoremoval those kernel packages that are:
#  - the currently booted version
#  - the kernel version we've been called for
#  - the latest kernel version (determined using rules copied from the grub
#    package for deciding which kernel to boot)
#  - the second-latest kernel version, if the booted kernel version is
#    already the latest and this script is called for that same version,
#    to ensure a fallback remains available in the event the newly-installed
#    kernel at this ABI fails to boot
# In the common case, this results in exactly two kernels saved, but it can
# result in three kernels being saved.  It's better to err on the side of
# saving too many kernels than saving too few.
#
# We generate this list and save it to /etc/apt/apt.conf.d instead of marking
# packages in the database because this runs from a postinst script, and apt
# will overwrite the db when it exits.

However, this sometimes will not mark them for auto-removal since some of the coding has changed over versions to prevent this from happening.

If you want mark the previous kernels for autoremove except for the required kernels based on the scripts, run the following command from a terminal window:

sudo apt-mark auto ^linux-image-

Then, when you run the apt-get autoremove command only the ones that are old and no longer needed can be removed. I have put examples below:

This first one shows all kernels on the system minus the current running kernel.

root@terrance-Linux:/home/share# dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'
linux-headers-3.16.0-34
linux-headers-3.16.0-34-generic
linux-headers-3.16.0-36
linux-headers-3.16.0-36-generic
linux-headers-3.16.0-37
linux-headers-3.16.0-37-generic
linux-headers-4.0.0-040000
linux-headers-4.0.0-040000-generic
linux-image-3.16.0-34-generic
linux-image-3.16.0-36-generic
linux-image-3.16.0-37-generic
linux-image-4.0.0-040000-generic
linux-image-extra-3.16.0-34-generic
linux-image-extra-3.16.0-36-generic
linux-image-extra-3.16.0-37-generic

This one shows the current running kernel.

terrance@terrance-Linux:~$ uname -r
4.0.1-040001-generic
terrance@terrance-Linux:~$ sudo apt-get autoremove
[sudo] password for terrance: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
terrance@terrance-Linux:~$ sudo apt-mark auto ^linux-image-
linux-image-extra-3.16.0-33-generic can not be marked as it is not installed.
linux-image-extra-3.13.0-27-generic can not be marked as it is not installed.
linux-image-3.13.0-44-lowlatency can not be marked as it is not installed.
linux-image-3.13.0-27-generic can not be marked as it is not installed.
linux-image-3.16.0-31-lowlatency can not be marked as it is not installed.
linux-image-3.16.0-36-generic set to automatically installed.
linux-image-lowlatency-lts-utopic can not be marked as it is not installed.
linux-image-extra-3.13.0-36-generic can not be marked as it is not installed.
linux-image-3.13.0-36-generic can not be marked as it is not installed.
linux-image-4.0.0-040000-generic set to automatically installed.
linux-image-extra-3.13.0-45-generic can not be marked as it is not installed.
linux-image-3.16.0-25-generic can not be marked as it is not installed.

NOTE: The above one was too long to list, so I truncated a bit.

terrance@terrance-Linux:~$ sudo apt-get autoremove
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  linux-image-3.16.0-34-generic linux-image-3.16.0-36-generic
  linux-image-4.0.0-040000-generic linux-image-extra-3.16.0-34-generic
  linux-image-extra-3.16.0-36-generic
0 upgraded, 0 newly installed, 5 to remove and 0 not upgraded.
After this operation, 613 MB disk space will be freed.
Do you want to continue? [Y/n] 

So, after running those commands, you can see that I can now auto-remove all the old but the current kernel (4.0.1-040001-generic) and the next newest one (3.16.0-37-generic).

Hopefully this helps.

10
  • Ubuntu does not do auto-remove by default does it?. We do have the option but it was (still) deemed impossible to decide what an "unwanted" kernel is (on other operating system "unwanted" is equal to "unused").
    – Rinzwind
    May 6, 2015 at 20:35
  • 1
    @Terrance I tried to pass sudo apt-mark auto ^linux-image- followed by sudo apt-get autoremove --purge but to no avail; however, a little dist-upgrade modified 2 kernels (3.13.0-52 and 3.16.0.37) and the following autoremove got rid of 3.13.0-51...
    – ionreflex
    May 7, 2015 at 14:37
  • 1
    @ionreflex Glad you found that. Not every answer is perfect, and it may not work for everyone. But I find it nice that people like yourself can do a little troubleshooting and figure out a way to remove them. I am thinking that the dist-upgrade you ran kicked off the /etc/kernel/postinst.d/apt-auto-removal which then modified the /etc/apt/apt.conf.d/01autoremove-kernels then telling it which ones to keep.
    – Terrance
    May 7, 2015 at 15:28
  • 1
    @Terrance Pretty sure I did a dist-upgrade before, but maybe the fact that the kernels got upgraded this morning did the trick... anyway, cheers, and thank you for your answer, I've learned a lot through it!
    – ionreflex
    May 7, 2015 at 15:38
  • 16
    Be nice if Ubuntu simply had a flag somewhere like "MAX_INSTALLED_KERNELS = 1" (meaning keep 1 additional kernel instead of ~3) in a config somewhere. This has been system-admin annoyance for MANY years. "Oh, /boot/ suddenly out of space!" 99% of time it's unused kernels laying about.
    – B. Shea
    Jul 23, 2020 at 15:25

You must log in to answer this question.

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