grub2 - Removing old kernel entries in Grub

24
2014-04
  • To Do
  • Answers
  • green7
    • Open up a terminal (Ctrl+Alt+T).

    • Type uname -r. This will show you the kernel you're using currently, so you don't want to remove this.

    • Run the following command: dpkg --list | grep linux-image. This will show the all the kernels that are installed.

    • Note down the names of all the kernels which you want to remove.

    • To remove the kernels, run: sudo apt-get purge linux-image-x.x.x.x-xyz (replace the kernel name with an appropriate one).

    • Update the GRUB: sudo update-grub2

    And, you're done.

    Bonus: here's a little one-liner to do all that automatically:

    sudo apt-get purge $( dpkg --list | grep -P -o "linux-image-\d\S+" | grep -v $(uname -r | grep -P -o ".+\d") )
    
  • Sergey

    I usually just start Synaptic (which has to be installed separately these days), search for "linux" in the Installed category and mark all old versions of the kernel and linux-headers (i.e. all except the current one) to be removed. When uninstalled, they're removed from GRUB menu.

    There must be a nifty single-line command to do that, but it feels safer just to do that manually

  • SeMeKh

    The slickest solution would be using ubuntu-tweak. After installation, fire it up, go to Janitor, choose Old Kernel, select the old kernels you intend to remove and Clean. That's it! (Note that only old kernels show up in the janitor)

  • david6

    For Ubuntu 12.04 LTS (Precise) 64bit (on non-PAE 32bit)

    Check which versions are present:

    sudo update-grub
    

    Remove the oldest first, and don't remove current/latest version:

    sudo apt-get remove linux-image-3.2.0-23-generic
    sudo apt-get remove linux-image-3.2.0-24-generic
    sudo apt-get remove linux-image-3.2.0-25-generic
    

    Re-check what version are now present. (Optional)

    Note: This step is required, if you remove all but latest (or leave just one) version.

    sudo update-grub
    

    For Ubuntu 12.04 LTS (Precise) 32bit (PAE)

    Check which versions are present:

    sudo update-grub
    

    Remove the oldest first, and don't remove current/latest version:

    sudo apt-get remove linux-image-3.2.0-23-generic-pae
    sudo apt-get remove linux-image-3.2.0-24-generic-pae
    sudo apt-get remove linux-image-3.2.0-25-generic-pae
    

    Re-check what version are now present. (Optional)

    Note: This step is required, if you remove all but latest (or leave just one) version.

    sudo update-grub
    
  • lambda23

    Try this command. It's a refined version of the same apt-get remove command

    sudo apt-get autoremove linux-image-3.2.0-23-generic-pae
    

    By replacing remove with autoremove, the removal process automatically finds and removes other dependencies left as well, so you will get a clean uninstallation, rather than having to go here and there and delete the leftovers.

    Repeat the process for other old kernels as well.

    After that, run

    sudo update-burg
    

    After that, you will see only the latest kernel.

  • jokerdino

    I suggest you enter this in a terminal session (Alt-Ctrl-T):

    sudo update-grub
    

    This command causes grub to use os-prober and to update the menu.

    If you have partitions with old Ubuntu releases on them that had used grub (it appears the OP had a natty release partition somewhere, maybe more) it's necessary to clean them up too before running update-grub on the current system. os-prober finds Windows and other Linux partitions. For other Linux partitions it copies the items from their grub menus, if any, rather than looking at their kernel files.

    If you have a really messed up configuration the best solution may be to boot your Ubuntu system and reinstall grub with:

    sudo apt-get --reinstall install grub2
    

    While reinstalling grub it will recreate the /etc/default/grub and the /boot/grub/menu.cfg file, based on the installed kernels in that partition and on the kernels installed in the grub configurations of other (presumably older) Ubuntu partitions (doesn't seem to be any for the OP).

  • To Do

    I found what this problem was.

    The issue was that, when I upgraded Ubuntu, by installing through a liveUSB instead of doing a normal upgrade, it left behind the old kernel files in the /boot folder.

    Now that I have upgraded in the same way from 12.10 to 13.04, I encountered the same situation.

    The solution is to manually delete all the files related to the old kernels in the /boot folder and run sudo update-grub. The extra entries vanished.


  • Related Question

    10.04 - How to modify grub entry for supporting KGDB kernel image?
  • Nishant

    I am trying to update target m/c grub.cfg file for KGDB setup but while booting the m/c it got hung completely and not asking/waiting for remote gdb connection. Following is the entry which I added:-

    menuentry 'Ubuntu, with Linux 2.6.32-24-kgdb' --class ubuntu --class gnu-linux --class gnu --class os {
            recordfail
            insmod ext2
            set root='(hd0,1)'
            search --no-floppy --fs-uuid --set 12878c3b-c553-4b4b-986a-6e32daea3ad1
            linux   /vmlinuz-2.6.32-kgdb root=/dev/mapper/ubuntu-root ro kgdbwait kgdboe=@192.168.140.23/,@192.168.140.158/  quiet
            initrd  /initrd.img-2.6.32-24-server
    }
    

    I have also compiled and copied /boot/vmlinuz-2.6.15.5-kgdb & /boot/System.map-2.6.15.5-kgdb to target m/c from devlopement m/c.

    STD entry before adding KGDB in grub.cfg was:-

    menuentry 'Ubuntu, with Linux 2.6.32-24-server' --class ubuntu --class gnu-linux --class gnu --class os {
            recordfail
            insmod ext2
            set root='(hd0,1)'
            search --no-floppy --fs-uuid --set 12878c3b-c553-4b4b-986a-6e32daea3ad1
            linux   /vmlinuz-2.6.32-24-server root=/dev/mapper/ubuntu-root ro   quiet
            initrd  /initrd.img-2.6.32-24-server
    }
    

    Please suggest how to get rid of this problem.


  • Related Answers
  • andrewsomething

    If you are using grub2 (which was the default in 10.04), menu entries should not be manually added to grub.cfg. Instead they can be added in /etc/grub.d/40_custom or you can drop a new file in /etc/grub.d/. More GRUB2 info can be found here:

    https://help.ubuntu.com/community/Grub2#Custom Menu Entries

    As for the KGDB kernel itself, I'm certainly not an expert here. My understanding is that you need to also set kgdb8250=<port number>,<port speed>. You might try looking here for more KGDB specific help:

    http://kgdb.linsyssoft.com/docu.htm