22

I have. a simple question. After I updated my kernel, I couldn't boot (stuck on purple screen) unless I chose the old kernel. So how do I delete the new kernel with synaptic. And how do I do this safely? I'm in Quantal Quetzel. My kernel is 3.5.0-40. The working kernel is 3.5.0-39. Also, how do I stop kernel updates from appearing in the update manager? Thanks.

Dave
  • 223
  • 1
  • 2
  • 5

5 Answers5

31

If the latest kernel update break things (Original question), the best option for most is to fall back to the previous working kernel (provided that you keep at least 1 fallback option).

Otherwise, use @ZAB 's solution to install a specific version of the kernel version provided via the apt package manager.

In generic, to remove the unwanted out-dated kernels, open a terminal session and run the command:

IMPORTANT: If /boot is NOT on its tiny partition (running out of space), avoid explicitly purging old kernels. Consider them backup / fallback options when upgrade introduces problems (hardware driver modules, etc.).

Good read on kernel upgrade and preservation mechanism: How does apt on Ubuntu decide how many old kernels to keep

TL;DR: Take a look at /etc/apt/apt.conf.d/01autoremove-kernels file generated by postinstall hook (script).

Snippet from Ubuntu 20.04 LTS running 5.4.0 for a better understanding:

// DO NOT EDIT! File autogenerated by /etc/kernel/postinst.d/apt-auto-removal
APT::NeverAutoRemove
{
   "^linux-.*-5\.4\.0-28-generic$";
   "^linux-.*-5\.4\.0-29-generic$";
   "^linux-.*-5\.4\.0-31-generic$";
   "^kfreebsd-.*-5\.4\.0-28-generic$";
   "^kfreebsd-.*-5\.4\.0-29-generic$";
   "^kfreebsd-.*-5\.4\.0-31-generic$";
   "^gnumach-.*-5\.4\.0-28-generic$";
   "^gnumach-.*-5\.4\.0-29-generic$";
   "^gnumach-.*-5\.4\.0-31-generic$";
   "^.*-modules-5\.4\.0-28-generic$";
   "^.*-modules-5\.4\.0-29-generic$";
   "^.*-modules-5\.4\.0-31-generic$";
   "^.*-kernel-5\.4\.0-28-generic$";
   "^.*-kernel-5\.4\.0-29-generic$";
   "^.*-kernel-5\.4\.0-31-generic$";
};

Find the kernel package name

dpkg -l | grep linux-image

At the time of writing, on amd64/x86_64 architecture the image name was: linux-image-$(uname -r)-generic

For unsigned kernel images, like those created by nvidia drivers, the image could be: linux-image-unsigned-4.20.17-042017-generic

Remove (purge - remove all configuration files as well)

sudo apt-get purge linux-image-3.5.0-40-generic

If you want to do a deep clean (leftover package configs...), USE WITH CAUTION:

dpkg -l | awk '/^rc/ { print $2 }' | xargs apt-get purge -y
dpkg -l | awk '/^rc/ { print $2 }' | xargs dpkg -P

NOTE: For Ubuntu 18.04 LTS or later, you may also want to remove any linux-modules or linux-headers packages of the same version to save disk spaces / keep you system lean.

Update - 2020-05-21

Explicitly purging old kernels may cause unexpected results. Not recommended unless /boot is on its own tiny partition (out of space).

Refer to @ZAB 's approach to get rid of the latest kernel update which introduces bug (break things).

Ubuntu wiki on Removing old kernels

There is no (I personally haven't found) easy equivalent to Fedora/RHEL/CentOS approach by setting installonly_limit=2 to tell DNF or YUM to only keep latest 2 kernels.

Terry Wang
  • 9,775
  • Oh don't I need to do sudo update-grub after that? – Dave Sep 08 '13 at 21:15
  • The postrm script of the package will update grub for you when you remove (purge) it;-) – Terry Wang Sep 08 '13 at 21:18
  • Thanks Terry that fixed my problem. Now how do I keep kernel updates out of the update manager? – Dave Sep 09 '13 at 13:26
  • sudo apt-get update && apt-get dist-upgrade -y, not sure if you have linux-current-generic or linux-image-generic-lts-raring on non-LTS releases. – Terry Wang Sep 09 '13 at 13:34
  • 1
    This malicious solution will also remove the dependent package linux-image-generic-*. The system will stop getting kernel updates and older kernels will be removed by consequent autoremove call which will break you system completely. – ZAB May 20 '20 at 23:00
  • @ZAB Thanks for pointing out, will update the post with notes. I did discover the issue over the years when mixing hwe kernels and aws edge rolling kernels together. Unfortunately I have not found a Fedora / CentOS equivalent installonly_limit=2 approach. – Terry Wang May 21 '20 at 00:04
  • @TerryWang it is not about old kernels and it is not new issue. The Debian/Ubuntu kernel update infrastructure is built over dependencies. How do you think the particular kernel image is landed to your system? It is listed as dependency in a general kernel package. Check my answer below what steps was needed to remove the newest kernel version and preserve the system update facility. – ZAB May 21 '20 at 00:38
  • On Ubuntu (and Debian most likely - sorry I am not a Debian user any more) the kernel postinstall hook script generates /etc/apt/apt.conf.d/01autoremove-kernels, that explains how the default kernel upgrade and preservation mechanism works. Anyway, if disk space is NOT a concern, leave it to the system default. – Terry Wang May 21 '20 at 01:32
  • @TerryWang the question was how to remove the newest bugous kernel. The newest kernels are installed and updated as dependencies to the main distributive package linux-image-generic- and linux-generic-. If you just remove the newest kernel image and all the dependent packages, it will also cause removal of the main packages and everything will collapse later on, there will be no updates and already installed kernels will become orphaned and will be removed after consequent autoremove call. – ZAB May 21 '20 at 02:55
  • @ZAB Good point, committed the original question after such a long time ;-) So the best option from my point of view is actually leave the buggy kernel alone and use the fallback option, provided that you keep at least 2 kernels without needing to poke with the Debian way of kernel upgrade and its dependencies. – Terry Wang May 21 '20 at 06:35
  • @TerryWang yes, adding DEFAULT=saved GRUB_SAVEDEFAULT=true to /etc/default/grub run sudo update-grub reboot and choose the older working kernel version from advanced boot options submenu is also a possible workaround.. until maintainers fix the bug in repository. – ZAB May 21 '20 at 12:53
8

Don't blindly do what was suggested in the accepted answer. It will break your system. Read what apt utility tells you before pressing y.

The following steps was done to remove the buggy kernel 5.3.0-53 for example. The previous working kernel was 5.3.0-51.

First run sudo apt remove linux-image-5.3.0-53-generic --verbose-versions and read the output:

...
The following NEW packages will be installed:
  linux-image-unsigned-5.3.0-53-generic (5.3.0.53.109)
...

We don't need this package, press n and ask to remove it as well sudo remove linux-image-5.3.0-53-generic linux-image-unsigned-5.3.0-53-generic --verbose-versions now prints this:

....
The following packages will be REMOVED:
 linux-generic-hwe-18.04 (5.3.0.53.109)
 ....

This package is important, this is the main distributive package that provides us with kernel updates. We need to reinstall the previous version of it. So press y and find the previous good kernel version. In my case it is 5.3.0.51.104. Try to reinstall it sudo apt install linux-generic-hwe-18.04=5.3.0.51.104 --verbose-versions, prints this:

The following packages have unmet dependencies:
 linux-generic-hwe-18.04 : Depends: linux-image-generic-hwe-18.04 (= 5.3.0.51.104)  but 5.3.0.53.109 is to be installed
                           Depends: linux-headers-generic-hwe-18.04 (= 5.3.0.51.104) but 5.3.0.53.109 is to be installed

Let blacklist this newer version we don't like. Edit file /etc/apt/preferences and add this:

Package: linux-generic-hwe-18.04 linux-image-generic-hwe-18.04 linux-headers-generic-hwe-18.04
Pin: version 5.3.0.53.109
Pin-Priority: -1

Repeat sudo apt install linux-generic-hwe-18.04=5.3.0.51.104 --verbose-versions check the output, in my case everything seems fine, so I agree and reboot.

Pablo Bianchi
  • 15,657
ZAB
  • 253
  • 3
  • 11
2

I couldn't get a grub menu to appear during boot, but I wanted to get rid of the latest kernel - it was making firefox crash, and parcellite stopped working, nor could I run Timeshift restore. And I got no grub menu.

dpkg -l | grep linux-image

then pick a kernel and use (successively, if needed)

sudo apt-get purge linux-image-unsigned-5.4.0-81-generic

choose appropriate name from the dpkg -l list

sudo rm -r /lib/modules/5.4.0-81-generic

kernels may appear in various guises

sudo apt autoremove linux-headers-generic-hwe-18.04

warning screens may appear,telling you that you are removing the current kernel, and do you want to abort? (reply No)

muru
  • 197,895
  • 55
  • 485
  • 740
2
sudo apt-get remove linux-image-3.5.0-40*.

I will suggest you always keep at least one older kernel available, just in case you need to boot into it for reasons you may never expect right now.

crafter
  • 639
  • 4
  • 13
  • Ik that about keeping the kernels. Very important stuff. I have one other besides this one. Ty I'll try it tomorrow and post back. – Dave Sep 08 '13 at 21:14
0

Neither other answer on this question seems correct. The full answer is a mix of both of them with some further correction;

sudo apt-get purge *4.18.0-21*

Were 4.18.0-21 is the number of your kernel, use ls /boot to see it.

This is what did it for me (though had to run a similar command twice in order to get rid of /lib/modules/4.18.0-21-generic).

Use this command with caution to ensure you don't delete anything necessary.

As an interesting side note, in my case linux-headers-generic-hwe-18.04 was removed as well, and when I tried to re-install it, it wanted to install the latest kernel again, so did not do that. And, research then indeed provded it was part of the 4.18.0-21 kernel; https://packages.ubuntu.com/bionic-updates/linux-headers-generic-hwe-18.04