Software & AppsOperating SystemLinux

Why Doesn’t apt-get autoremove Remove Old Kernels? And How to Make it Do So

Ubuntu 4

In the world of Linux, apt-get autoremove is a command that is used to remove packages that were automatically installed to satisfy dependencies for other packages and are now no longer needed. However, you may have noticed that it doesn’t remove old kernels by default. In this article, we’ll explore why this happens and how you can make apt-get autoremove remove old kernels.

Quick Answer

By default, apt-get autoremove does not remove old kernels because they are considered manually installed packages. This is a safety measure to prevent the accidental removal of kernels that you may have installed intentionally. However, you can modify the configuration files or use tools like linux-purge to enable the automatic removal of old kernels.

Understanding Why apt-get autoremove Doesn’t Remove Old Kernels

By default, apt-get autoremove does not remove old kernels because it considers them as manually installed packages. This is a safety measure to prevent the removal of kernels that you may have installed intentionally.

The logic behind this is found in the configuration files /etc/apt/apt.conf.d/01autoremove-kernels and /etc/apt/apt.conf.d/01autoremove.

In /etc/apt/apt.conf.d/01autoremove-kernels, you’ll find that apt is instructed to never autoremove kernels. This is done by another script file, /etc/kernel/postinst.d/apt-auto-removal, which checks if you have manually installed two chosen kernels, typically the first and the current one. If so, apt-autoremove will only remove the older versions that were not manually installed.

In /etc/apt/apt.conf.d/01autoremove, there are lines that specify packages that should never be autoremoved. These include firmware, kernel packages, modules, and tools.

How to Make apt-get autoremove Remove Old Kernels

While the default behavior is to retain old kernels, there are ways to make apt-get autoremove remove them. However, it’s important to proceed with caution as modifying these settings can have unintended consequences.

Method 1: Modifying Configuration Files

You can comment out the lines in /etc/apt/apt.conf.d/01autoremove that specify the packages that should never be autoremoved. This will allow apt-autoremove to remove the kernels. However, do this at your own risk and ensure you understand the implications.

Method 2: Marking Kernel Packages as Automatically Installed

Another reason why apt-get autoremove may not remove old kernels is if the kernel packages are marked as manually installed. This can happen in earlier releases of Ubuntu, especially if the kernels were installed using the Software Updater.

In this case, you can try marking the kernel packages as automatically installed using the following command:

sudo apt-mark auto $(apt-mark showmanual | grep -E "^linux-([[:alpha:]]+-)+[[:digit:].]+-[^-]+(|-.+)$")

Then, run sudo apt-get autoremove --purge to remove the old kernels.

In the above command, apt-mark auto is used to mark a package as automatically installed. The grep command is used to filter out the kernel packages.

Method 3: Using Tools like linux-purge

Alternatively, you can use tools like linux-purge to selectively remove old kernels, even if they were manually installed.

Conclusion

In summary, apt-get autoremove does not remove old kernels by default because they are considered manually installed packages. However, by modifying the configuration files or using other tools, you can enable the automatic removal of old kernels. Always remember to proceed with caution when modifying system files and ensure you have a backup of your data.

Why doesn’t `apt-get autoremove` remove old kernels by default?

apt-get autoremove doesn’t remove old kernels by default because they are considered manually installed packages. This is a safety measure to prevent the accidental removal of kernels that users may have installed intentionally.

How can I make `apt-get autoremove` remove old kernels?

There are a few ways to make apt-get autoremove remove old kernels. One method is to modify the configuration file /etc/apt/apt.conf.d/01autoremove and comment out the lines that specify the packages that should never be autoremoved. Another method is to mark the kernel packages as automatically installed using the apt-mark command. You can also use tools like linux-purge to selectively remove old kernels, even if they were manually installed.

What are the implications of modifying the configuration files to enable the removal of old kernels?

Modifying the configuration files to enable the removal of old kernels can have unintended consequences. It’s important to proceed with caution and understand the implications. Removing old kernels may impact the stability or functionality of your system, especially if you rely on specific kernel versions for compatibility with certain hardware or software. Always make sure you have a backup of your data before making any changes.

How can I mark kernel packages as automatically installed using the `apt-mark` command?

You can mark kernel packages as automatically installed using the following command: sudo apt-mark auto $(apt-mark showmanual | grep -E "^linux-([[:alpha:]]+-)+[[:digit:].]+-[^-]+(|-.+)$"). This command will mark the kernel packages as automatically installed, allowing apt-get autoremove to remove them.

Are there any tools available to remove old kernels?

Yes, there are tools available like linux-purge that can help you selectively remove old kernels, even if they were manually installed. These tools provide a more streamlined and user-friendly approach to managing and removing old kernels.

Leave a Comment

Your email address will not be published. Required fields are marked *