10

I like managing kernels my self, and I can't figure how to stop apt-get autoremove from deleting older kernels.

dbz2k
  • 125

3 Answers3

13

apt-get autoremove, removes files that have been installed automatically and are no longer needed. If you want autoremove not to remove some of them, you can mark them as installed manually. In your case: sudo apt-mark manual package_name.

If you have installed a mainline kernel, you need to mark 3 packages:
linux-headers-version
linux-headers-version-generic
linux-image-version-generic

If you are using the official kernel, you need to mark one more package:
linux-image-extra-version-generic.

The only thing you have to do is find the correct version and replace the word in the package name. Of course all three or four versions have to be the same.

Stormlord
  • 6,807
4

https://help.ubuntu.com/community/RemoveOldKernels#GUI_Way explains how to mark a kernel as automatically installed. You can do the opposite. Here is an example command:

First I run the command without super user rights:

apt-mark manual '^linux-.*-5\.0\.0-38(-generic)?$'

apt mark tells me:

linux-image-unsigned-5.0.0-38-generic can not be marked as it is not installed.
linux-image-5.0.0-38-generic set to manually installed.
linux-headers-5.0.0-38-generic can not be marked as it is not installed.
linux-modules-extra-5.0.0-38-generic set to manually installed.
linux-modules-5.0.0-38-generic set to manually installed.
E: Could not create temporary file for /var/lib/apt/extended_states - mkstemp (13: Permission denied)
E: Failed to write temporary StateFile /var/lib/apt/extended_states

This looks fine, therefore I run the command with sudo.

If you want to know what the '^linux-.*-5\.0\.0-38(-generic)?$' does visit https://regex101.com/r/Oi9ixx/1

Guest
  • 41
0

If you don't mind using a GUI, there are programs to manage this problem. For years the go-to one was Ubuntu Tweak, but it recently became abandonware. However, it's still available and functioning in the LTS versions of Ubuntu. For information on how to install it in 16.04, see the answer to this question.

Ubuntu Tweak's Janitor function will list all the software than can be removed. If you don't wish to remove the kernels, simply don't check the boxes by them, and use the program to remove everything else.