How to Stop Ubuntu from Automatically Removing Old Kernel Versions (apt-get autoremove)

Ubuntu Linux handles system kernel updates beautifully, installing new kernels alongside the old ones so you can easily reboot into a previous version if a new update breaks your hardware drivers. To prevent these old kernels from filling up your tiny /boot partition, Ubuntu periodically runs an automated background script (often tied to unattended-upgrades or a cron job calling apt-get autoremove) to permanently delete outdated kernels, usually keeping only the two most recent versions.

While this is excellent housekeeping for 99% of users, it can be disastrous if you are running highly specialized, proprietary hardware or custom-compiled software that only works on one specific, older kernel version. If Ubuntu’s background script automatically purges that specific “golden” kernel, your system will be forced to boot into a newer version, instantly breaking your specialized environment upon the next restart.

You can use the apt-mark command to explicitly flag specific kernel packages, forcing the autoremove utility to completely ignore them during its automated cleanup sweeps.

How to Prevent Old Kernels from Being Removed

You must place a “hold” on the specific kernel version you want to preserve.

  1. Open your terminal application (Ctrl+Alt+T) or SSH into your server.
  2. First, find the exact name of the kernel you want to protect. You can list all installed kernels by running:
    dpkg --list | grep linux-image
  3. Identify the specific version string (e.g., linux-image-6.5.0-25-generic).
  4. Use the apt-mark hold command to lock both the image and the headers for that version. Type the following and press Enter:
    sudo apt-mark hold linux-image-6.5.0-25-generic linux-headers-6.5.0-25-generic
  5. The terminal will respond with a confirmation message stating that the packages are now “set on hold.”

When the system eventually runs its apt-get autoremove background scripts, it will look at your held packages, see the restriction, and completely skip over them. That specific kernel will remain installed and available in your GRUB boot menu indefinitely, ensuring your specialized hardware always has the precise environment it requires.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.