99

I wonder why Ubuntu won't remove the old kernels automatically.

Surely nothing is perfect and things can go wrong, so in case a update doesn't work, having a backup kernel maybe great. But it would also suffice to just keep the latest kernel and the one before the latest and delete all the older ones.

Is there a reason why Ubuntu won't do this automatically?.

10
  • See if below is good enough. I pulled some resources together and there is a lot of doubt on what should be considered to be deleted.
    – Rinzwind
    Feb 27, 2015 at 15:03
  • 1
    @Rinzwind delete? Arch simply overwrites them - and then some things remain unusable until you restart (like iptables).
    – muru
    Feb 27, 2015 at 15:29
  • 3
    "Why doesn't Ubuntu remove old kernels automatically?" it does. "I wonder why Ubuntu won't remove the old kernels manually." I have no idea what you mean with this.
    – Braiam
    Feb 27, 2015 at 18:12
  • 1
    It seems extremely silly to me that I get the message 'boot out of disk space' every second month, and then have to fix this rather technical issue. I really understand why ubuntu is not a system for non-technical users.
    – sunew
    Oct 16, 2016 at 17:32
  • 2
    @sunew yes, this issue completely killed my parents' machine running 12.04 for the full LTS period - filled up the disk's inode tables! There should be a sensible default here that technical users (who deliberately install different kernels) can overwrite easily. Oct 26, 2016 at 17:59

8 Answers 8

44

Is there a reason why Ubuntu wont do this automatically?

I can see only 1 reason: it does not work flawlessly; there is no clean way to currently decide what the definition of "old kernels" is. "old" does not mean "unused" nor does it mean "unwanted". And any mistake in this will kill a users' machine.

So up to now the manual method is preferred since this puts the power of removing into the users' hands.


Resources for this conclusion:

Ubuntu WIKI: Proposal for removing old kernels

Last-good-boot is implemented fully in Intrepid/8.10 final, however it has been disabled because it was not considered stable enough. The setting is a single line in the file /etc/default/kernel-helper-rc.


Launchpad: Should aptitude provide a way to remove old kernel versions ?

apt-get has an autoremove feature that uninstalls all packages that are not needed as dependencies and have not been installed manually. Since Ubuntu 14.04 all obsolete kernels and headers should automatically be flagged as no more needed, and thus can be purged with the apt-get autoremove command. (There are reports that this does not yet fully work). I am not aware of a feature in aptitude that is similar to apt-get's autoremove.


Ubuntu-devel: Distro-provided mechanism to clean up old kernels

While agreeing that it would be quite helpful and seems appropriate to have the cleanup automatic, there is a slight potential pitfall (or two). There are various flavours of kernels and people may or may not deliberately have those installed in parallel. Also various releases had sometimes a changing set of depending packages. For a while this should be only linux-backports-modules (there had been linux-ubuntu-modules and linux-restricted-modules). Though this is not so much of a problem. From a pattern matching point of view the generic-pae kernels are a bit of a pain as they tend to ruin the "use the last part of a split by "-" for the flavor". But anyway, I think the main issue is the various flavours, so a cleanup that is automatic should retain the last three of each, even though this may tend to leave more kernels around.

11
  • I agree that Ubuntu should not remove old kernels automatically. I once had a situation where newer kernels caused a serious problem with some software; until that problem was fixed, I had to retain the oldest kernel that worked. Naturally, I avoided apt-get autoremove. Mar 3, 2015 at 13:40
  • 3
    it should at least be an option to be able to enable autoremove just my .02
    – mBardos
    Nov 11, 2015 at 7:40
  • @mBardos there is (sort of): Ubuntu Tweak has a method for this.
    – Rinzwind
    Nov 11, 2015 at 7:43
  • 3
    Doesn't "old" mean "not one of the last 2 (or three) kernels to be booted"? Unless you've got two different lines of kernels installed? Hmm I suppose you might be using an older version of a kernel for a different install on another partition. But there should be a default state that gets cancelled out if you are some kind of funk wizard. Or it should at least by default allocate enough space on /boot that you don't need to worry about this for years. Otherwise grannies are never gonna run linux.
    – intuited
    Nov 26, 2015 at 17:35
  • @PaddyLandau: See @andrewdevans' answer. I just did autoremove and dpkg --get-selections | grep -v deinstall | grep linux shows three kernels installed.
    – idbrii
    Dec 2, 2015 at 15:31
22

There are two things going on here:

  1. New kernels are completely new packages, not updated packages with a newer version number, so installing a new kernel image doesn't replace any older ones. They coexist.

  2. Ubuntu uses a bunch of apt magic to protect the last two kernel versions from 'apt-get autoremove' (the latest one, and the one that was last booted). See /etc/apt/apt.conf.d/01autoremove-kernels for details.

What this all means is that once you've rebooted into the latest kernel, 'apt-get autoremove' should remove all but the current and last booted kernel packages.

Old kernels still accumulate in /boot because autoremove is not enabled in Ubuntu by default - you must either run it manually every so often, or you must enable it.

This may change in 16.04 - a bugfix to the unattended-upgrades package will enable autoremove of old kernels by default.

20

This script will do the job. It ll remove all kernels that are not in use.

sudo dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge

Other then that, not removing kernels must be a fairly new thing. I have one Ubuntu 14 system after the other running into this issue of a full boot partition due to old kernel garbage.

It can make the life of less experienced users extremely complicated. It is actually a noob trap. Canonical should fix this.

7
  • 3
    This script is potentially dangerous: when I do a system update which installs a new kernel, then run this script before rebooting, the new kernel will be removed instead of the old one. I don't want to say it's a bad script, just that one has to be careful when to call it. Jul 30, 2015 at 7:05
  • Thanks! "0 upgraded, 0 newly installed, 48 to remove and 5 not upgraded. After this operation, 3,322 MB disk space will be freed." Oct 22, 2015 at 11:43
  • 7
    It is wise to run this script without | xargs ... part to check what packages are about to be removed, before actually removing them: sudo dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'
    – PeterM
    Nov 26, 2015 at 13:43
  • 2
    @PeterM, and sudo is not needed in front of dpkg.
    – jarno
    Dec 23, 2015 at 16:36
  • There is a separate question about the script here.
    – jarno
    Dec 23, 2015 at 16:40
3

The simple-minded way to remove old kernels is to use ubuntu-tweak. You select the Janitor tag and tick the Old Kernel box. That gives you a list of kernels it thinks you don't need any more. You can select them all and click Clean and, after a while, the job's done. Being of a nervous disposition I checked which kernel I was running using uname -a but it didn't figure on the list. This worked on my wife's laptop (which has a rather small boot partition and was showing warnings) and mine (which was working fine). Does everyone agree that this is safe?

2
  • Oh removing kernels automatically can be done safely; it is just that the person in charge of making this happen does not feel confident enough to claim he can decide for everyone a kernel of a specific system should be deleted. Old does not mean unwanted ;)
    – Rinzwind
    Feb 28, 2015 at 13:15
  • As @Rinzwind points out, there are really two issues here. How and when/if to do it. I used to use ubuntu-tweak and it worked, but didn't like KDE (which I use) very much. Now, whenever I get a kernel update, I go into the muon gui package manager, search for kernel, find the one to delete, add it's last number like 36 to kernel - kernel 36, sort on installed, and select the 2 or 3 packages to be removed. It's easy. It's also manual, so if I have compiled any driver modules that still need an older kernel, that kernel won't get deleted until I'm ready for that to happen.
    – Joe
    Mar 6, 2015 at 11:58
2

Ubuntu only auto-removes items that are no longer needed or are a security risk...So I assume that the reason for this is security purposes. Lets say for some odd reason a new kernel becomes insecure...Then you would be rerouted back to the old kernel temporarily while the new one is being fixed. Also new kernels can come with changes so therefore some kernels are opt-in for example updating 14.04.1 to 14.04.2 you can opt-in for a new kernel, but this April when the new LTS version 15.04 comes out you will automatically have you kernels updates. So I reckon that its for security purposes.

but if you want to remove it try running this command line

sudo apt-get autoremove

This should remove any unneeded software such as your old kernel if not try

sudo apt-get remove (kernels-name)
6
  • 1
    "Lets say for some odd reason a new kernel becomes insecure...Then you would be rerouted back to the old kernel temporarily while the new one is being fixed." Do you have a source for this claim? Feb 27, 2015 at 22:51
  • 3
    @JohnKugelman it wouldn't happen. A new package would be released with the fix and people would upgrade to it. In case of extreme unfixable problem, an older kernel would be packaged with higher version number (epoch change perhaps) to effect the upgrade.
    – hobbs
    Mar 1, 2015 at 5:29
  • "...when the new LTS version 15.04 comes out..." - 15.04 will not be LTS, unless there has been a major support shift by Canonical which I haven't heard of.
    – iGadget
    Apr 14, 2015 at 14:24
  • actually 15.04 is going to be the new LTS but the release as is the same for all releases will not become the new standard LTS until a specific date after the new system has been released...i believe Canonical does this because they want to be absolutely sure the system is completely secure...as i am typing this 15.04 has already been released so give it a few months and it'll be an LTS...i don't plan on upgrading till then...not much changed anyway
    – Tactux
    Apr 28, 2015 at 21:01
  • @TAC_Tux That's not how Ubuntu does LTS releases. You're possibly thinking of the way the Debian project does things. Here's the current Ubuntu release schedule: wiki.ubuntu.com/LTS Jul 30, 2015 at 13:32
2

I recommand use Ubuntu Tweak to remove old kernels. look here:

How do I remove old kernel versions to clean up the boot menu?

2

I think the latest release of Ubuntu 15.10 could remove old kernels automatically, if you edit /etc/apt/apt.conf.d/50unattended-upgrades file: Change line

//Unattended-Upgrade::Remove-Unused-Dependencies "false";

to

Unattended-Upgrade::Remove-Unused-Dependencies "true";

I haven't tested this, though.

2
  • See a Bug report concerning the issue.
    – jarno
    Dec 23, 2015 at 20:18
  • And see also my related answer.
    – jarno
    Dec 24, 2015 at 11:13
2

I had a problem with being unable to install software. Apt-get was unable to install linux-headers, reporting the disk was full.$ df -i reported that IUse% was at 100%: The disk had free space but had run out of inodes. $ sudo dpkg --configure -a failed, & so did $ sudo apt-get -f install and $ sudo apt-get autoremove

The problem solved when I manually removed several of the older kernel folders from /usr/src - just $ rm or shift-delete the folders. This freed up enough inode space to allow $ apt-get -f install to complete.

After that I had only 10 linux kernels. I ran $ sudo apt-get autoremove which deleted 1.4 GB of files. Which begs the question: Really?? Really?? But honestly, is the this the right way to run a user friendly system?

2
  • Yes, it is, because you might unexpectedly need that older kernel, as explained above. Jun 17, 2016 at 9:14
  • Did you try to purge some kernel by dpkg --purge?
    – jarno
    Aug 16, 2016 at 15:40

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