RemoveOldKernels – Community Help Wiki – Ubuntu Documentation

LVM installations and encrypted installations use a separate /boot partition. The default partition is capable of containing only four or five cores, and can fill to capacity quickly. To prevent your /boot partition from filling up, you need to set up automatic removal of old kernels or manually delete old kernels regularly.

Changing the kernel that provides packages on your system requires commands with root access, so read RootSudo

.

Removing old kernels is easy to do on a system with enough free space on its root partition or separate boot partition. You can delete them manually or configure unattended updates to do so automatically. If you receive or have received an error from a package management tool, manual removal may not work until the problem is fixed. See the Issues section.

Manual maintenance

using apt

You can remove old kernels with a simple

autoremove command in a terminal: sudo

apt-get

autoremove -purge

Note: In Ubuntu 14.04, due to error # 1439769 this only works, if you have installed security updates automatically, and not manually, for example, Software Updater, see in this section how to configure it.

The packages to remove are determined in part based on whether the package is marked as installed manually or automatically. You can check

if a package that provides the kernel is marked as automatically installed by using this command in a terminal: apt-mark showauto ‘linux-image-.*’

or to see if it is marked as manually installed:

apt-mark showmanual ‘linux-image-.*’

In the event that some packages that provide kernel are marked as manual (possibly because they were installed via ‘apt-get’) and you want them to be removed automatically, You can change the package status using apt-mark auto and the package name. For example, to

mark kernel 4.12.0-12-generic as self-removable: sudo apt-mark auto ‘^linux-.*-4\.12\.0-12(-generic)?$’

Note: apt-get autoremove will not remove all automatically installed legacy kernel provisioning packages as backup versions are maintained; the list of saved kernels is automatically maintained and updated in the /etc/apt/apt.conf.d/01autoremove-kernels file as a regular expression list Matching.

Other methods

If you want to purge a specific package provided by the kernel, 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, for example, linux-image-extra-4.12.0-12-generic. It will not purge linux-headers-4.12.0-12, if there is another type of linux-headers-4.12.0-12 installed besides -generic.

If you just need to purge kernels selectively, you can benefit from this response with unofficial code, if the system is not already broken

.

There is an unofficial script for purging kernels; it’s called Linux-Purge. By it you can purge the cores selectively, or choose to keep the given number of older cores and remove others depending on the given options. You can even make some repairs (with the -fix option), if the system is already broken; that’s a kind of programmed version of what’s said in the chapter Safe removal of old kernels.

Automatic maintenance

The unattended update package, included with the default installation of all flavors of Ubuntu, includes a feature to automatically remove unused packages. Enabling this feature is a two-step process in Ubuntu 14.04.

Enable Unattended Updates

(Ubuntu 14.04) In Ubuntu

16.04 and later, unattended updates are enabled by default, however, in Ubuntu 14.04 you will need to enable it through the GUI or a command line. Note There is separate documentation on configuring automatic security updates.

GUI shape

Enable unattended updates using the Software & Updates application tab

‘s “Updates”: Check

  • the box for *-security (and/or any other repository you want) Check for
  • updates automatically: Set to

  • any frequency (except ‘Never’)
  • When there are security updates: Set to automatically download and install

Shell Way

sudo dpkg-reconfigure -plow unattended-upgrades

By default, available security updates are installed every day. If you want to change what type of packages you will update (if any) or how often, see here for more details.

Configure unattended updates to

automatically remove unnecessary kernels

Note: The following methods will only remove kernels marked as automatically installed as described above. In Ubuntu 16.04 kernels installed by Software Updater are marked as automatically installed. In Ubuntu 14.04 only cores installed by unattended updates are marked as installed automatically. See bug #1439769 for details.

Note: This will not remove all old installed packages automatically, providing as you save the fallback versions; the list of saved kernels is automatically maintained and updated in the /etc/apt/apt.conf.d/01autoremove-kernels file as a list of matching regular expressions.

The second step is to edit the old configuration file to enable automatic deletion or, Preferably, create a new overridden configuration file in which you only need to write the options that you change from the default value or from the values defined in the submitted one, because updates in the sent configuration file may conflict with local changes that block unattended updates from being updated.

The

default configuration file is

/etc/apt/apt.conf.d/50unattended-upgrades

You can name the new one as /etc/apt/apt.conf.d/52unattended-upgrades-local

See here for reference

.

Option for all Ubuntu versions

The following configuration configures unattended updating to remove unused dependencies after an unattended upgrade

  • .

Ensure that the configuration file contains an

Unattended-Upgrade::Remove-Unused-Dependencies “true” line;

(and not commented on; comments begin with ‘//’.)

Thereafter, unattended updates will automatically remove packages that provide older kernels as part of the unattended update. (It doesn’t purge them, though.) It also removes other unnecessary packages, which is or is not what you want.

Options for Ubuntu 16.04 and

newer

unattended updates Version 0.90 supports a new configuration variable called Unattended-Upgrade::Remove-New-Unused-Dependencies that allows you to automatically remove only packages that become excessive during an unattended update run. It is enabled, that is, “true” by default. The way it is designed, it is important that you allow unattended updates to control the installation of security updates. Otherwise, unattended uprades will not delete old kernels and you may have to do a manual removal of cores.

Version 1.1 which is also currently available for Ubuntu 16.04 adds another option called Unattended-Upgrade::Remove-Unused-Kernel-Packages. Remove unused kernel packages before installing a new one (or even if nothing is installed). It is also enabled by default.

If package management is interrupted, or if previous regular maintenance does not work, one of several problems may have occurred. You may have no storage space, have a package version discrepancy, or have another problem.

For

users of LVM systems, cipher systems, or limited storage systems, the most frequent problem is that the /boot partition is simply full. The package manager cannot install a pending update due to lack of space. Also, apt-get cannot delete a package due to a broken dependency.

This problem can be solved from the shell: by manually remvoing one or two old kernels, we can provide the package manager with enough space to install the update in a queue

.

First, delete any temporary files left over from previous kernel updates.

$ sudo rm -rv ${TMPDIR:-/var/tmp}/mkinitramfs-*

Determine the version number of the kernel that is currently running, that you do NOT want to delete.

$ uname -r 4.2.0-21-generic

List all kernels, including booted (4.2.0-21-generic in this example), in the package database and their states.

$ dpkg -l | queue -n +6 | grep -E ‘linux-image-[0-9]+’ rc linux-image-4.2.0-14-generic ## The oldest kernel in the database ## Status ‘rc’ means that the oldest installed kernel has already been removed. Removable. ## Status ‘ii’ means Installed. ii linux-image-4.2.0-16-generic ## Another old kernel installed. Removable. ii linux-image-4.2.0-18-generic ## Another old kernel installed. Elimination. ii linux-image-4.2.0-19-generic ## The good old kernel. Save. ii linux-image-4.2.0-21-generic ## Same version as ‘uname -r’, package for the current kernel. DO NOT REMOVE. iU linux-image-4.2.0-22-generic ## DO NOT REMOVE. Status ‘iU’ means it is not installed, ## but queued to install in apt. ## This is the package we want apt to install.

To free up space in /boot we will delete an initrd.img file for a suitable old kernel manually, this is necessary due to a kenel packaging error.

$ sudo update-initramfs -d -k 4.2.0-15-generic

Now we will use dpkg to TRY to purge the kernel package for the same old

kernel: $ sudo dpkg -purge linux-image-4.2.0-15-generic

NOTE: The above command will probably fail, as there is probably a dependent linux-image-extra package installed along with a ‘generic’ kernel package. Generally, the output of the above command will tell you which package you need to remove first. In this example case, you would run

$sudo dpkg -purge linux-image-4.2.0-15-generic linux-image-extra-4.2.0-15-generic

Finally, we will fix the package installation process that failed earlier

. $ sudo apt-get -f install ## Try to fix the broken dependency.

If the last command fails due to insufficient disk space in /boot, you should purge another kernel (for example, linux-image-4.2.0-16-generic) in the same way. Otherwise, you can proceed to the Manual Maintenance chapter to remove more kernels. Ensure that the Linux header packages corresponding to the previously purged kernels will also be purged.

Unsatisfied dependency

errors

If you have ignored apt and dpkg errors for a while, some packages may have been updated while others have not. This is an expected follow-up effect. You will see mysterious and persistent version errors in normal updates.

The simple way to fix most version mismatch errors is to update the package database, clear the package cache, and download and reinstall the latest version of the offending package

.

This is easier than it sounds. For example, if the package ‘hello’ is one of those unsatisfied dependencies:

$ sudo apt-get update ## Update the package database $ sudo apt-get clean hello ## Delete the package hola from the local cache $ sudo apt-get install -reinstall hello ## Download and reinstall the latest version of

helloOops, removed all kernels

!

If you got a little carried away and deleted all the kernels, you fall into the “Did you do WHAT?!?! ” class. You will be pleased to know that you are not the first, nor will you be the last, person to do this. Get your LiveCD and head to GRUB reports that there is no operating system.