Enter your email address:

Delivered by FeedBurner

Clean up your grub menu and the kernels you do not use


StumbleUpon Toolbar

For Distros with lots of Kernel upgrades Like Ubuntu Gutsy now that it is still in Beta, it is annoying to start every day your PC and see that your grub list keep growing and growing.

Clean this unused kernel images is not only good for haven a cleaner grub menu, but also to gain disk space, as all those kernel images uses a lot of space in your disk.

I will show you this under Ubuntu, but should work for other distros just taking care of the names of their kernel images, and also its package managers.

If you remove the kernel you are using, you will broke your Linux, if you remove all your kernel images, you will surely broke your Linux, so use this with care!!

1. Determine which Kernel you are using
Just run:

uname -r

and write down the result, in my case this was my output:

$ uname -r
2.6.22-14-generic
2. Look for all installed kernel images
Go to /boot/ and list its contents.

cd /boot
ls vmlinuz*

Take note of all versions you have installed
This was my output:

$ ls vmlinuz*
vmlinuz-2.6.20-16-generic  vmlinuz-2.6.22-11-generic  vmlinuz-2.6.22-12-generic  vmlinuz-2.6.22-13-generic  vmlinuz-2.6.22-14-generic
3. Remove the kernels you want
As I told you before, take care with this step.

Potential risk of breaking your Linux if you erase the wrong kernel

Run:

sudo apt-get remove linux-image-[version]-generic linux-image-[version]-generic

For all the versions you may want to erase, keep at least two or three kernel images, the one you are using and one or two more.

This was my output:

$ sudo apt-get remove linux-image-2.6.22-11-generic linux-image-2.6.22-12-generic linux-image-2.6.20-16-generic
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  linux-image-2.6.20-16-generic linux-image-2.6.22-11-generic linux-image-2.6.22-12-generic linux-restricted-modules-2.6.20-16-generic
  linux-restricted-modules-2.6.22-11-generic linux-restricted-modules-2.6.22-12-generic linux-ubuntu-modules-2.6.22-11-generic linux-ubuntu-modules-2.6.22-12-generic
0 upgraded, 0 newly installed, 8 to remove and 0 not upgraded.
Need to get 0B of archives.
After unpacking 346MB disk space will be freed.
Do you want to continue [Y/n]? y
(Reading database ... 110373 files and directories currently installed.)
Removing linux-restricted-modules-2.6.20-16-generic ...
Removing linux-image-2.6.20-16-generic ...
Running postrm hook script /sbin/update-grub.
Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... found: /boot/grub/default
Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
Searching for splash image ... none found, skipping ...
Found kernel: /boot/vmlinuz-2.6.22-14-generic
Found kernel: /boot/vmlinuz-2.6.22-13-generic
Found kernel: /boot/vmlinuz-2.6.22-12-generic
Found kernel: /boot/vmlinuz-2.6.22-11-generic
Found kernel: /boot/memtest86+.bin
Updating /boot/grub/menu.lst ... done

Removing linux-restricted-modules-2.6.22-11-generic ...
Removing linux-ubuntu-modules-2.6.22-11-generic ...
update-initramfs: Generating /boot/initrd.img-2.6.22-11-generic
find: /lib/firmware/2.6.22-11-generic: No such file or directory
find: /lib/firmware/2.6.22-11-generic: No such file or directory
find: /lib/firmware/2.6.22-11-generic: No such file or directory
find: /lib/firmware/2.6.22-11-generic: No such file or directory
find: /lib/firmware/2.6.22-11-generic: No such file or directory
find: /lib/firmware/2.6.22-11-generic: No such file or directory
Removing linux-image-2.6.22-11-generic ...
Running postrm hook script /sbin/update-grub.
Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... found: /boot/grub/default
Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
Searching for splash image ... none found, skipping ...
Found kernel: /boot/vmlinuz-2.6.22-14-generic
Found kernel: /boot/vmlinuz-2.6.22-13-generic
Found kernel: /boot/vmlinuz-2.6.22-12-generic
Found kernel: /boot/memtest86+.bin
Updating /boot/grub/menu.lst ... done

Removing linux-ubuntu-modules-2.6.22-12-generic ...
update-initramfs: Generating /boot/initrd.img-2.6.22-12-generic
Removing linux-restricted-modules-2.6.22-12-generic ...
Removing linux-image-2.6.22-12-generic ...
Running postrm hook script /sbin/update-grub.
Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... found: /boot/grub/default
Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
Searching for splash image ... none found, skipping ...
Found kernel: /boot/vmlinuz-2.6.22-14-generic
Found kernel: /boot/vmlinuz-2.6.22-13-generic
Found kernel: /boot/memtest86+.bin
Updating /boot/grub/menu.lst ... done

As you may see apt-get takes care of the menu.lst file also, anyway you may run:

sudo update-grub

In case your menu.lst file was not automatically cleaned.

I am sure there should be a better way to do it, I am thinking in some line that can automatically parse all the content of /boot/ and remove the result of uname -r command and then perform the clean automatically, maybe with awk, and regular expressions, but I am not as good on that stuff.

Trackback URL for this post:

http://www.go2linux.org/trackback/271
StumbleUpon Toolbar

 If you like this article, subscribe to our full rss

If this article was somehow useful for you, you can leave something in the tip's jar

Please post your question in our forum and use comments only to leave your comments about the article, thanks.

Thanks, this works well. I

Thanks, this works well. I had eight unnecessary kernels cluttering up the place - madness.

It seems possible to do this more easily in Ubuntu with Synaptic. (I used your method and made 4 typing errors. Tedious.) Since these are just packages like any other you can just scroll down the listing for "linux-image--generic" and find the kernels you want to lose and uncheck them.

Finally, it seems like it would be a good idea for the Linux or Ubuntu folks to allow you to set how many old kernels you leave on your machine. Like, say I want to keep around the last two previous kernels at all times, so during a kernel upgrade any older ones beyond those 2 plus the new one would be automatically deleted. This would prevent your ever having to do this cleanup, but still leave you with a backup kernel or two for rescue or whatever.

Yes you are right about that

Yes you are right about that application, it should exist.

About Synaptic, yes it is a better method, I always put here the command line way, because some of the users maintain servers where they have only ssh access, but I think that maybe most of my readers are Desktop users, so I will start writing more about Graphical solutions.

Guillermo Garron

A better solution as you

A better solution as you suggested above would be a script like this:

ls /boot/ | grep vmlinuz | sed 's@vmlinuz-@linux-image-@g' | grep -v `uname -r` > /tmp/kernelList
for I in `cat /tmp/kernelList`
do
aptitude remove $I
done
rm -f /tmp/kernelList

Save that to a file, give it execute permissions:

chmod 755 cleanKernels

and then run it as root:

sudo ./cleanKernels

Aptitude will prompt you for each kernel, just give it a "Y" and it will work fine.

Thanks, I will include it in

Thanks, I will include it in the original post and give you credit, if you have a webpage let me know please.

I need to learn more about sed, awk, and stuff like to improve some tasks, this is the good thing about computers you never stop learning!

I am into it since 1990 and know almost nothing yet!

Guillermo Garron

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Captcha
This question is for testing whether you are a human visitor and to prevent automated spam submissions.

This site is proudly hosted at Bluefur Hosting