Quickzi: How To Remove Older Kernels from Ubuntu
If you’ve had the same Ubuntu installation for a while and have just been upgrading to newer releases, you may have noticed that a lot of older kernel versions are piling up in your grub menu and on your system.
How to remove older kernels from Ubuntu
This can be done by using the Synaptic Package Manager, however I will show you how it is done on the command line.
First, find out what kernel you are currently running:
# uname -a
Linux foogazi 2.6.24-19-generic #1 SMP Wed Jun 18 14:43:41 UTC 2008 i686 GNU/Linux
From the output you can see that you are currently using the 2.6.24-19-generic kernel.
Next, let’s take a look at all of the kernel versions you have installed:
# dpkg -l | grep linux-headers-*
linux-headers-2.6.24-16 2.6.24-16.30 Header files related to Linux kernel version
linux-headers-2.6.24-16-generic 2.6.24-16.30 Linux kernel headers for version 2.6.24 on x
linux-headers-2.6.24-19 2.6.24-19.34 Header files related to Linux kernel version
linux-headers-2.6.24-19-generic 2.6.24-19.34 Linux kernel headers for version 2.6.24 on x
linux-headers-generic 2.6.24.19.21 Generic Linux kernel headers
Now all you need to do is remove the old versions with apt-get. Since we’ve noted with uname -a that we are currently running 2.6.24-19-generic we want to make sure we do not remove it. All of the others can be removed.
# sudo apt-get remove linux-headers-2.6.24-16 linux-headers-2.6.24-16-generic
Now the older kernels are gone. Repeat the apt-get remove step to remove any others you may have. Remember to not remove your current kernel.
Important note: It is a good idea to keep at least one old kernel version around in case anything breaks in your current kernel and you are unable to boot into it. An example would be that you boot into your current kernel but recieve a kernel panic. With an old kernel still available you can reboot the computer and select the older kernel version from the Grub menu and still access your system to find out what is going on.
Popularity: 17% [?]
Tagged with: kernel • Linux • Linux Tip • quickzi • ubuntu
Filed under: Linux
Like this post? Subscribe to my RSS feed and get loads more!













Another interesting command is ‘apt-cache policy’ which gives more information than ‘dpkg -l’
dpkg -l “*image*”
or
dpkg -l \*image\*
less typing; though grep is great
There is a much easier and quicker way:
sudo apt-get autoremove
This will scan for absolete kernels and delete them (after user confirmation).
What w-sky is sorta wrong. I’ve got several kernel versions that do not get removed by that, and I do not remember if that ever did anything useful for grub menu kernel cleanup. Additionally, the hunt and peck method by the blog author is prone to “oops” due to the attention to detail required
I use this command:
# dpkg -l | grep ^ii | grep linux-image | grep -v image-generic | grep -v `uname -r | cut -d’-’ -f1,2` | cut -d’ ‘ -f3 | xargs apt-get remove -y && update-initramfs -u
It says: list the packages (|), find the ones that are installed (|) and named linux-image (|), but not the image-generic (|). Then take the one that we are currently using (`) out of the list (|). Now remove every image left in that list (|) and update my grub menu(&&).
You will not be left with a backup kernel, but I’ve never had a problem that could not be solved with the recovery mode of the current kernel.
What w-sky said is absolutely wrong.