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.

To Do
  • 15,502

7 Answers7

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") )
green
  • 14,306
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.

To Do
  • 15,502
  • 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 '17 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.

DougC
  • 233
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

Sergey
  • 43,665
  • As I mentioned in the post, there are no entries in Synaptic for 2.6.8 kernels. – To Do Aug 26 '12 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 '12 at 00:55
  • what if it does not have GUI environment? im working on a 'ubuntu server' and wanted to remove those old entries – kapitanluffy Jan 31 '13 at 22:43
  • @kapitanluffy: for a command-line solution see green7's answer. – Sergey Feb 01 '13 at 04:48
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).

jokerdino
  • 41,320
John S Gruber
  • 13,336
  • 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 '12 at 09: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. – John S Gruber Aug 16 '12 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 ? – John S Gruber Aug 27 '12 at 02:12
  • For the first see last edit. There is no /boot/grub/grub.cfg file – To Do Aug 27 '12 at 05: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? – John S Gruber Aug 27 '12 at 05:17
  • gdisk gives Problem opening -l for reading! Error is 2. The specified file does not exist! What should I look for in /etc/default/grub? I do not know what a bios-loader partition on a GPT disk is - I have a Dell laptop. – To Do Aug 27 '12 at 08:14
  • My mistake about gdisk -l, it should be gdisk -l /dev/sda. Just wondering if there was anything strange about /etc/default/grub, but if there is nothing in /boot/grub/grub.cfg I think it's more likely that file is on some other partition. It would explain why there's no 2.6.8 in grub-update, and no other Linix partition, but there is a 2.6.8 in grub. (A GPT partition format is often used for the bigger disks that are often found today). – John S Gruber Aug 27 '12 at 13:06
  • Is there a /boot/grub/menu.lst perhaps? That's where the older version of grub kept its configuration. See my last comment, too, please. – John S Gruber Aug 27 '12 at 22:37
  • Regarding gdisk -l /dev/sda see modofication above. There's no menu.lst in /boot/grub. – To Do Sep 01 '12 at 16:39
  • I've added to this answer. If you would rather figure out what is going on than just reinstall grub2 I suggest you follow http://askubuntu.com/a/24487/63886 and use a pastebin to store the RESULTS.txt file bootinfoscript generates. If you do that please post the pastebin link in your question. – John S Gruber Sep 05 '12 at 15:11
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)

semekh
  • 569
  • +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 '13 at 19:40
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
david6
  • 14,499
  • 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 '12 at 18:24