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

  • Can you expand on this? Not really sure what you want, there is no real question here, more os a statement that scripts don't work and you like to control them yourself. Commented May 30, 2016 at 0:53
  • I updated the questions. I been googling but i can't figure out on how to stop ubuntu from deleting older kernels.
    – dbz2k
    Commented May 30, 2016 at 1:01
  • What i really cant understand is, why you want to keep more than two old kernels around?
    – Videonauth
    Commented May 30, 2016 at 1:06
  • because It will delete my mainline kernel sooner or later when we get kernel update.
    – dbz2k
    Commented May 30, 2016 at 1:10
  • My understanding is that unless you do sudo apt-get autoremove the kernels will stay in place. At least that is my understanding based on a different question about how to remove them. Commented May 30, 2016 at 1:23

3 Answers 3


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:

If you are using the official kernel, you need to mark one more package:

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.

  • 1
    Thanks but is there any way to stop apt-autoremove to never to delete old kernel. Is there a config file that I could edit?
    – dbz2k
    Commented May 30, 2016 at 11:55
  • If i have mainline kernel installed from here kernel.ubuntu.com/~kernel-ppa/mainline There are already marked as manually installed for me? do I need to anything else to stop them from getting removed from apt-get autoremove.
    – dbz2k
    Commented May 30, 2016 at 17:07
  • 1
    No, manually installed files are not removed by autoremove. That's all you have to do. You can check file flags by typing sudo apt-mark showmanual or showauto respectively. apt-mark will give you the corresponding list of installed files.
    – Stormlord
    Commented May 30, 2016 at 23:21

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


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.

You must log in to answer this question.

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