The cart is empty
  • Access to a root account or a user with sudo privileges.
  • CentOS 7 installed.

Step 1: Check Current Kernels

First, determine which kernels are currently installed on your system. Open a terminal and run the following command:

rpm -q kernel

This command will list all installed kernels. It's recommended to keep at least one older kernel as a backup in case of issues with the newer one.

Step 2: Configure YUM

CentOS 7 uses the YUM package manager for software installation and updates, including kernels. YUM allows configuration for automatic removal of old kernels. Open the YUM configuration file in an editor:

sudo nano /etc/yum.conf

In the yum.conf file, find the line containing installonly_limit. This value determines how many kernel versions the system will keep. The recommended value is 2, meaning the system will keep the current kernel and one previous. If this line doesn't exist, add it to the end of the file:

installonly_limit=2

Save the file and close the editor.

Step 3: Automatic Cleanup Using YUM

YUM comes with the yum-utils plugin, which includes the package-cleanup tool for easy removal of unnecessary packages, including old kernels. If yum-utils is not yet installed, install it using the command:

sudo yum install yum-utils

After installation, you can run package-cleanup with the --oldkernels parameter and set the count of kernels you want to keep. If you've set installonly_limit=2, you can run:

sudo package-cleanup --oldkernels --count=2

This command will remove all kernels except the last two.

Step 4: Automating Cleanup

To automate this process, you can set up a cron job that will regularly execute the kernel removal command.

  1. Open crontab for editing:
    sudo crontab -e
    ​
  2. Add a line defining how often you want the cleanup to run. For example, to run it once a week:
    0 2 * * 0 sudo package-cleanup --oldkernels --count=2
    ​

 

This command will execute the cleanup every Sunday at 2:00 AM.

Save and close crontab. Cron will now automatically run the cleanup according to the set schedule.

 

By setting up automatic removal of old kernels on CentOS 7, you can maintain a clean /boot partition and prevent it from filling up, ensuring smooth and trouble-free system updates. Always ensure you keep at least one functional older kernel as a backup in case of emergencies.