Friday, November 7, 2008

Remove old kernels in Ubuntu

Whenever a new kernel is applied ubuntu keeps the old one. This clogs up the grub and hard disk space and ends up having a lot of kernels to boot in the grub.
To know the version that is currently running:

uname -r


and to remove all other kernels use
sudo apt-get remove --purge 2.6.24-16-*


change the 2.6.2x-xx- parts to remove those kernel. Make sure you dont remove the kernel you are using, or you will end up breaking the system. It is wise to keep a recent kernel and a second one just prior to the new one, just in case your system doesn't do well with the new kernel.

Update

Please be sure to type the command exactly as such, dont miss any of the hyphens (-) and dots (.)
Missing something would end up uninstalling all of your kernels and hence making the system not bootable.


Correct commands :
sudo apt-get remove --purge 2.6.27-7-*
sudo apt-get remove --purge 2.6.27-9-*

etc

Command Line Output:

arun@server4:~$ sudo apt-get remove --purge 2.6.27-9-*
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting linux-headers-2.6.27-9-generic for regex '2.6.27-9-*'
Note, selecting linux-image-2.6.27-9-generic for regex '2.6.27-9-*'
Note, selecting linux-image-2.6.27-9-server for regex '2.6.27-9-*'
Note, selecting linux-headers-2.6.27-9 for regex '2.6.27-9-*'
Note, selecting linux-headers-lbm-2.6.27-9-generic for regex '2.6.27-9-*'
Note, selecting linux-headers-2.6.27-9-server for regex '2.6.27-9-*'
Note, selecting linux-restricted-modules-2.6.27-9-server for regex '2.6.27-9-*'
Note, selecting linux-backports-modules-2.6.27-9-generic for regex '2.6.27-9-*'
Note, selecting linux-restricted-modules-2.6.27-9-generic for regex '2.6.27-9-*'
Note, selecting linux-headers-lbm-2.6.27-9-server for regex '2.6.27-9-*'
Note, selecting linux-backports-modules-2.6.27-9-server for regex '2.6.27-9-*'
Note, selecting linux-image-2.6.27-9-virtual for regex '2.6.27-9-*'
The following packages will be REMOVED:
linux-headers-2.6.27-9* linux-headers-2.6.27-9-generic*
linux-image-2.6.27-9-generic* linux-restricted-modules-2.6.27-9-generic*
0 upgraded, 0 newly installed, 4 to remove and 0 not upgraded.
After this operation, 149MB disk space will be freed.
Do you want to continue [Y/n]? y
(Reading database ... 127310 files and directories currently installed.)
Removing linux-headers-2.6.27-9-generic ...
Removing linux-headers-2.6.27-9 ...
Removing linux-restricted-modules-2.6.27-9-generic ...
update-initramfs: Generating /boot/initrd.img-2.6.27-9-generic
Purging configuration files for linux-restricted-modules-2.6.27-9-generic ...
Removing linux-image-2.6.27-9-generic ...
Examining /etc/kernel/prerm.d.
run-parts: executing /etc/kernel/prerm.d/dkms
Uninstalling: nvidia 177.82 (2.6.27-9-generic) (i686)

-------- Uninstall Beginning --------
Module: nvidia
Version: 177.82
Kernel: 2.6.27-9-generic (i686)
-------------------------------------

Status: Before uninstall, this module version was ACTIVE on this kernel.

nvidia.ko:
- Uninstallation
- Deleting from: /lib/modules/2.6.27-9-generic/updates/dkms/
- Original module
- No original module was found for this module on this kernel.
- Use the dkms install command to reinstall any previous module version.
depmod......

DKMS: uninstall Completed.
run-parts: executing /etc/kernel/prerm.d/last-good-boot
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.27-11-generic
Found kernel: /boot/memtest86+.bin
Replacing config file /var/run/grub/menu.lst with new version
Updating /boot/grub/menu.lst ... done

The link /vmlinuz.old is a damaged link
Removing symbolic link vmlinuz.old
you may need to re-run your boot loader[grub]
The link /initrd.img.old is a damaged link
Removing symbolic link initrd.img.old
you may need to re-run your boot loader[grub]
Purging configuration files for linux-image-2.6.27-9-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.27-11-generic
Found kernel: /boot/memtest86+.bin
Updating /boot/grub/menu.lst ... done

rmdir: failed to remove `/lib/modules/2.6.27-9-generic': Directory not empty
dpkg - warning: while removing linux-image-2.6.27-9-generic, directory `/lib/modules/2.6.27-9-generic' not empty so not removed.

6 comments:

Anonymous said...

I guess you should escape that * (if using tcsh, at least).
Thanks for the useful hint.

Anonymous said...

great,every kernel has 190MB, why then have 4 of them? Thx!!

Unknown said...

I get an error: Couldn't find package 2.6.31-15-generic

I am in the /lib/modules/ directory and they are all listed there.

kiran chowdry said...

thanx a lot man , it relly worked in my LINUX machine .....previously my desktop was fully with previous versions ......thanx a lot for ur info

Guppydas said...

Gees I freed up half a GB of space. Thanks a million for this.

Guppydas said...

Thank you so much. I was able to get much needed half GB of additional free space on my Linux.