Ubuntu – Remove Old Kernels

This is based partially on a post from http://www.liberiangeek.net/2011/11/remove-old-kernels-in-ubuntu-11-10-oneiric-ocelot/

As time goes on and you keep installing updates on an Ubuntu system, the number of old kernels will continue to grow without any automated cleanup. This issue can be caused by some or all of the following factors:
* Upgrade from 10.04 LTS to 12.04 LTS with default 250MB /boot partition
* Enable automatic updates on your system – this will install updated kernels until /boot is full causing errors like those described here.

You may be warned at login that the /boot partition is filling up when this happens. To properly clean up your system and remove the old kernels, use the following hints. You may also want to consider booting from a Linux maintenance disk and using a tool like gparted to resize /boot to a more reasonable size between 500MB and 1GB.

# View your running kernel version
uname -a

# Show installed kernel packages
dpkg --list | egrep 'linux-(image|headers)' | sort

# Remove specific kernel package versions (regex allowed)
apt-get purge KERNEL-PKG-NAMES-Ver.sion

# DO *NOT* remove your currently in-use kernel or any newer releases.
# NOTE: 'apt-get purge' automatically runs 'update-grub' for you

This type of maintenance is done automatically on Red Hat, CentOS, and Fedora systems during yum kernel updates. I’m not sure if there is an Ubuntu supported script or feature that will do this for you – maybe in a newer release? These notes were tested on Ubuntu 10.04 LTS and 12.04 LTS.

Related error messages:

NOTE: when the /boot device is full, you may need to use dpkg --purge KERNEL-PKG-NAME to remove each old kernel version before there will be enough space for apt-get commands to work.

# during motd (message of the day) login banner
  => /boot is using 93.0% of 227MB
# during "apt-get upgrade" - fails to install new kernel
**update-initramfs**: Generating /boot/initrd.img-3.2.0-48-generic
gzip: stdout: **No space left on device**
E: mkinitramfs failure cpio 141 gzip 1
update-initramfs: failed for /boot/initrd.img-3.2.0-48-generic with 1.
Errors were encountered while processing:
E: Sub-process /usr/bin/dpkg returned an error code (1)

UPDATE 3/18/2014 – I don’t see this mentioned many places but if you’re feeling adventurous and daring, try apt-get --purge autoremove which seems to purge all but the last three kernel releases (tested on Ubuntu 12.04 LTS). BEWARE that this also will autoremove other “unused” packages that you may actually want to keep – pay close attention to the list of packages which will be auto-removed before accepting the removal with “Y”. If you’re extremely adventurous, you could use cron to schedule “apt-get --yes --purge autoremove” – attempt at your own risk!


About notesbytom

Keeping technology notes on WordPress.com to free up my mind to solve new problems rather than figuring out the same ones repeatedly :-).
This entry was posted in Linux and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s