0

Is there a way to setup ubuntu so that when a new kernel update is installed that it will remove the latest kernel +2? That is not my current kernel not the new kernel but the now useless older kernel. So that I only ever have 2 kernels new and stable.

Can this be set as a default in apt?

1
  • There used to be a way in /etc/default/grub to set a number of kernels to keep, but that was removed awhile ago.
    – ubfan1
    Oct 6, 2014 at 1:06

2 Answers 2

1

This command has been posted in several locations:

apt-get purge $(dpkg --list |egrep 'linux-image-[0-9]' |awk '{print $3,$2}' |sort -nr |tail -n +2 |grep -v $(uname -r) |awk '{ print $2}')

See http://chr4.org/blog/2013/08/04/apt-get-cleanup-commands/

1

It isn't possible to set APT to do this.

This is part design choice, part limitation.

Ubuntu designates each new kernel as a different package name (the package name contains the kernel version) so APT treats them as separate packages. When they want you to download and install a new package, they create the new kernel package, and then point the generic kernel package (eg linux-kernel-generic) to depend on that new package. This is what causes the new kernel package to be installed.

Now, it is (or at least, traditional wisdom says it is) generally a good idea to keep your previous kernel installed when installing a new one, in case there is any incompatibility or bug in the new one. This allows you to switch kernels from the boot loader. So Ubuntu do not mark the new kernel packages as "replacements" of the old ones, which means APT will simply leave the old ones installed.

There is no mechanism in APT or the Ubuntu repositories to specify "keep the last two kernel packages". There could be something like this hacked in, but it may not play very well with all situations, such as when people skip a kernel version.

Now, while I said this isn't possible to do in APT, there may exist third party software tools that can help you with this, if not on an automatic basis.

There is a tool called Ubuntu-tweak which is not available in Ubuntu, but can be installed from a PPA. This tool has a "computer janitor" feature that, among other things, helps you uninstall old kernels.

Or, you can simply use Synaptic. Synaptic makes it pretty easy to list installed kernels (search for packages containing linux-image) and from there you can uninstall them easily.

You must log in to answer this question.

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