36

I regularly delete old kernels leaving only the latest two entries using Synaptic.

I'm using Precise. However in my Grub "previous Linux version" menu there are quite a few entries labelled 2.6.8. I cannot find these linux-images in Synaptic.

dpkg -l | grep linux-image

Gives:

rc  linux-image-3.0.0-17-generic           3.0.0-17.30                         
    Linux kernel image for version 3.0.0 on x86/x86_64
ii  linux-image-3.2.0-27-generic           3.2.0-27.43
Linux kernel image for version 3.2.0 on 32 bit x86 SMP
ii  linux-image-3.2.0-29-generic           3.2.0-29.46
Linux kernel image for version 3.2.0 on 32 bit x86 SMP
ii  linux-image-3.4.0-030400-generic       3.4.0-030400.201205210521
Linux kernel image for version 3.4.0 on 32 bit x86 SMP
ii  linux-image-generic                    3.2.0.29.31
Generic Linux kernel image

Sudo update-grub gives:

Generating grub.cfg ...
Found linux image: /boot/vmlinuz-3.4.0-030400-generic
Found initrd image: /boot/initrd.img-3.4.0-030400-generic
Found linux image: /boot/vmlinuz-3.2.0-29-generic
Found initrd image: /boot/initrd.img-3.2.0-29-generic
Found linux image: /boot/vmlinuz-3.2.0-27-generic
Found initrd image: /boot/initrd.img-3.2.0-27-generic
Found linux image: /boot/vmlinuz-2.6.38-11-generic
Found initrd image: /boot/initrd.img-2.6.38-11-generic
Found linux image: /boot/vmlinuz-2.6.38-10-generic
Found initrd image: /boot/initrd.img-2.6.38-10-generic
Found linux image: /boot/vmlinuz-2.6.38-8-generic
Found initrd image: /boot/initrd.img-2.6.38-8-generic
Found memtest86+ image: /boot/memtest86+.bin
Found Windows Vista (loader) on /dev/sda1

sudo apt-get remove linux-image-2.6.8-8-generic gives:

E: Unable to locate package linux-image-2.6.8-8-generic
E: Couldn't find any package by regex 'linux-image-2.6.8-8-generic'

My boot folder contains the following:

abi-2.6.38-10-generic         initrd.img-3.4.0-030400-generic
abi-2.6.38-11-generic         memtest86+.bin
abi-2.6.38-8-generic          memtest86+_multiboot.bin
abi-3.2.0-27-generic          System.map-2.6.38-10-generic
abi-3.2.0-29-generic          System.map-2.6.38-11-generic
abi-3.4.0-030400-generic      System.map-2.6.38-8-generic
config-2.6.38-10-generic      System.map-3.2.0-27-generic
config-2.6.38-11-generic      System.map-3.2.0-29-generic
config-2.6.38-8-generic       System.map-3.4.0-030400-generic
config-3.2.0-27-generic       vmcoreinfo-2.6.38-10-generic
config-3.2.0-29-generic       vmcoreinfo-2.6.38-11-generic
config-3.4.0-030400-generic   vmcoreinfo-2.6.38-8-generic
extlinux                      vmlinuz-2.6.38-10-generic
grub                          vmlinuz-2.6.38-11-generic
initrd.img-2.6.38-10-generic  vmlinuz-2.6.38-8-generic
initrd.img-2.6.38-11-generic  vmlinuz-3.2.0-27-generic
initrd.img-2.6.38-8-generic   vmlinuz-3.2.0-29-generic
initrd.img-3.2.0-27-generic   vmlinuz-3.4.0-030400-generic
initrd.img-3.2.0-29-generic

and

ls -l /etc/grub.d
yields:

total 56
-rwxr-xr-x 1 root root 6715 Apr 17 20:16 00_header
-rwxr-xr-x 1 root root 5522 Oct  1  2011 05_debian_theme
-rwxr-xr-x 1 root root 7407 May 17 09:22 10_linux
-rwxr-xr-x 1 root root 6335 Apr 17 20:16 20_linux_xen
-rwxr-xr-x 1 root root 1588 May  3  2011 20_memtest86+
-rwxr-xr-x 1 root root 7603 Apr 17 20:16 30_os-prober
-rwxr-xr-x 1 root root  214 Oct  1  2011 40_custom
-rwxr-xr-x 1 root root   95 Oct  1  2011 41_custom
-rw-r--r-- 1 root root  483 Oct  1  2011 README
gdisk -l /dev/sda

yields:

Partition table scan:
  MBR: MBR only
  BSD: not present
  APM: not present
  GPT: not present


***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format.
***************************************************************

Disk /dev/sda: 312581808 sectors, 149.1 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): F832A498-05E1-4615-B5B1-757ACB4A757A
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 312581774
Partitions will be aligned on 2048-sector boundaries
Total free space is 4183661 sectors (2.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        61442047   29.3 GiB    0700  Microsoft basic data
   3       163842048       169986047   2.9 GiB     8200  Linux swap
   4       169986048       312578047   68.0 GiB    0700  Microsoft basic data
   5        61444096       159666175   46.8 GiB    8300  Linux filesystem

Please help with removing the old and inexistent kernels from Grub.

6
  • Could it be that the upgrade from 11.10 left these kernels installed in /boot but removed the packages somehow?
    – To Do
    Aug 16, 2012 at 9:58
  • Is it safe to manually delete the 2.6.8 files in /boot and then update Grub?
    – To Do
    Aug 16, 2012 at 10:00
  • 1
    Please run sudo os-prober Aug 16, 2012 at 13:06
  • sudo os-prober gives '/dev/sda1:Windows Vista (loader):Windows:chain'
    – To Do
    Aug 26, 2012 at 15:18
  • 'sudo apt-get remove linux-image-2.6.8-8-generic' suppose to be 'sudo apt-get remove linux-image-2.6.38-8-generic'
    – penreturns
    Sep 1, 2012 at 16:55

7 Answers 7

63
  • 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") )
2
  • 1
    Great summary and an even better one-liner. Thanks!
    – Alex
    Jan 24, 2016 at 9:52
  • maybe use remove or autoremove instead of purge? and great with a one-liner.
    – Lamda
    Oct 30, 2017 at 21:57
12

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.

1
  • 2
    I had some kernels I had built from source. So these did not show up in dpkg --list | grep linux-image This answer is how you get rid of them.
    – RyanNerd
    Mar 13, 2017 at 10:11
7

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-grub

After that, you will see only the latest kernel.

1
  • 1
    I think the original question was about grub, not burg. so that last command won't do anything. Oct 4, 2012 at 16:12
3

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

4
  • As I mentioned in the post, there are no entries in Synaptic for 2.6.8 kernels.
    – To Do
    Aug 26, 2012 at 15:19
  • Do you have other partitions, possibly with older versions of Linux installed? GRUB might be finding those and adding entries to the menu.
    – Sergey
    Aug 27, 2012 at 0:55
  • what if it does not have GUI environment? im working on a 'ubuntu server' and wanted to remove those old entries Jan 31, 2013 at 22:43
  • @kapitanluffy: for a command-line solution see green7's answer.
    – Sergey
    Feb 1, 2013 at 4:48
3

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)

1
  • +1 , but if OP doesn't want to install additional software my experience might be helpful. I deleted the actual files by sudo su, after that GRUB 2, because of OS-Prober, automatically adjusted.
    – pfeiffep
    Mar 19, 2013 at 19:40
3

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).

10
  • I tried that but it detects 2.6.8 which is not shown in Synaptic. And apt-get remove gives "unable to locate package". See more details above.
    – To Do
    Aug 16, 2012 at 9:48
  • You have to boot each old Ubuntu version, in order and first clean up with sudo apr-get remove or synaptic, leaving just one and then run update-grub there. Do that in order. End with update-grub in your current ubuntu partition. It's picking up those kernels from grub menus in old Ubuntu partitions found by os-prober. Aug 16, 2012 at 13:00
  • Could you attach to your question a link to the contents of /etc/default/grub, ls -l /etc/grub.d, and /boot/grub/grub.cfg ? Aug 27, 2012 at 2:12
  • For the first see last edit. There is no /boot/grub/grub.cfg file
    – To Do
    Aug 27, 2012 at 5:02
  • Precise uses grub2 and grub2 stores the menus in /boot/grub/grub.cfg. The fact that there isn't one may mean that grub is actually finding its executables and menu in another partition. Could you add the contents of sudo gdisk -l, you may have to install the gdisk package first. Anything in /etc/default/grub, by the way? Might you have a bios-loader partition on a GPT disk? Aug 27, 2012 at 5:17
1

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
1
  • The problem with this is that these 2.6.8 kernels are NOT present in Synaptic so apt-get remove doesn't work
    – To Do
    Aug 24, 2012 at 18:24

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