8

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

dbz2k
  • 105
  • 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. – Christopher Angulo-Bertram May 30 '16 at 00:53
  • I updated the questions. I been googling but i can't figure out on how to stop ubuntu from deleting older kernels. – dbz2k May 30 '16 at 01:01
  • What i really cant understand is, why you want to keep more than two old kernels around? – Videonauth May 30 '16 at 01:06
  • because It will delete my mainline kernel sooner or later when we get kernel update. – dbz2k May 30 '16 at 01: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. – Christopher Angulo-Bertram May 30 '16 at 01:23
  • Yea I know but the issue is that command will also remove some stuff I don't mind being removed like old packages that I don't need. – dbz2k May 30 '16 at 01:29
  • I don't think Ubuntu removes old kernels automatically. So how about editing your question to add some detail about what you really mean - like how to get autoremove's functionality but keep the kernels. – Organic Marble May 30 '16 at 02:21
  • do you have any idea on how to fix this issue though? – dbz2k May 30 '16 at 02:24

3 Answers3

9

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,317
  • 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 May 30 '16 at 11:55
  • If i have mainline kernel installed from here http://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 May 30 '16 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 May 30 '16 at 23:21
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.

Organic Marble
  • 23,641
  • 15
  • 70
  • 122