2

Overtime with Ubuntu updates the GRUB menu lists a lot of previous Linux kernels and eats a lot of hard-disk space. How can the unused kernels be removed easily?

1

6 Answers 6

5

The below mentioned solution is Ubuntu specific.

  1. Check the kernel currently in use:

    uname -r
    
  2. List all the kernel images and headers installed:

    dpkg --list | grep linux-image
    dpkg --list | grep linux-headers
    
  3. Remove the image and headers of the kernel which you want to remove:

    sudo apt-get purge linux-image-3.19.0-15
    sudo apt-get purge linux-headers-3.19.0-15
    

    The above commands will remove the kernel image and its associated kernel modules and header files.

  4. After removing the unused kernel, update the GRUB configuration.

    sudo update-grub2
    
  5. Now reboot and verify if the GRUB menu is been properly cleaned up or not.

3

Open the Synaptic package manager from the System->Administration menu.

Click the “Search” button on the tool bar and search for linux-image-2.

The results should show every available and installed kernel. A green box on the left indicates that the package is installed. The only linux-image you want installed is the latest one. Find the package corresponding to the kernel to you running currently (this is the kernel you found in the terminal window). Make sure you keep that one. Now you can uninstall the old kernels from the list by clicking their boxes and selecting "Mark for Removal".

Got this from here

3

Open a terminal and run the below command. On reboot, only the latest kernel will be shown in the GRUB menu.

dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge

Root password is required to run the above command.

Thanks to ubuntugenius.

2
  • 1
    afaik this only removes the entry from the grub menu, and doesn't remove the old kernels itself
    – eugenevd
    Dec 20, 2016 at 8:22
  • This crashed my node
    – alper
    May 3, 2023 at 8:49
1

You can use Ubuntu Tweak for that or remove entries in /boot. For example, if I have vmlinuz-2.6.32-23-generic and vmlinuz-2.6.32-22-generic by removing the last one I'll get rid of one entry. Also, there can be a vmlinux instead of vmlinuz. There are also other files with version information too. You can remove them too. Always keep the latest one.

0

There's a script called 'purge-old-kernels' which available in the byobu package:

apt-get install byobu
purge-old-kernels

You can specify how many kernels to keep excluding the current in-use kernel:

purge-old-kernels --keep 2

Put this in cron if you need.

See: https://raw.githubusercontent.com/dustinkirkland/byobu/master/usr/bin/purge-old-kernels

1
  • This did not do anything
    – alper
    May 3, 2023 at 8:49
-2
  1. First check the current kernel version running on your host machine.

    uname -r
    
  2. List all the kernels installed in the host.

    rpm -qa kernel //Lists all the kernels including the one you want to remove
    

    Optional: If you want to take the backup of the kernel:

    cp /etc/grub.conf /tmp/grub1.conf
    
  3. Uninstall the kernel which you want to remove.

    rpm -e kernel-2.6.32-xxx.xxx –nodeps
    
  4. Check if it is uninstalled or not.

    rpm -qa kernel
    
1
  • This is clearly an Ubuntu-specific question. rpm does not exist on Ubuntu as it uses apt.
    – Karu
    Feb 18, 2016 at 2:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .