basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    Cleaning up old kernels, headers, and modules

    Every kernel update in Ubuntu adds image, module, and header packages — together they take 400–600 MB across /boot, /lib/modules, and /usr/src. Normally apt autoremove --purge clears them out, but manually marked packages, leftover rc entries, and a 100% full /boot need manual cleanup.

    What ships with a kernel and how many APT keeps

    A new kernel in Ubuntu installs alongside the old one under its own version number instead of overwriting it. Each version can include up to four packages across three directories:

    Package Location Purpose Approximate size
    linux-image-<version> /boot/vmlinuz-*, /boot/initrd.img-* kernel image and initial ramdisk 80–150 MB
    linux-modules-<version> /lib/modules/<version> core kernel drivers 100–150 MB
    linux-modules-extra-<version> /lib/modules/<version> extra drivers (networking, USB, filesystems) 150–250 MB
    linux-headers-<version> /usr/src/linux-headers-* C header files for building modules (DKMS) 100–150 MB

    Unversioned metapackages track the current release — linux-generic, linux-image-generic, linux-headers-generic (cloud images often use linux-virtual or linux-kvm). Never remove them, or the server stops getting kernel updates.

    APT decides which kernels to keep via /etc/kernel/postinst.d/apt-auto-removal, which writes protected versions into /etc/apt/apt.conf.d/01autoremove-kernels:

    01autoremove-kernels protects from removal:
      ├─ the currently running kernel (uname -r)
      ├─ the latest installed kernel (if newer than running)
      └─ one previous kernel as a fallback if boot fails

    After a reboot, two kernels remain: the running one and one fallback. Before a reboot following an update, up to three are protected.

    Standard cleanup

    If /boot isn’t completely full yet and old kernels came from normal updates, apt autoremove --purge handles it.

    1. Check the running kernel and installed packages

      uname -r
      # 6.8.0-87-generic
      
      dpkg -l 'linux-image-*' 'linux-headers-*' 'linux-modules-*' | grep -E '^(ii|rc)'
      # ii  linux-headers-6.8.0-85           6.8.0-85.85  all    Header files related to Linux kernel version 6.8.0
      # ii  linux-headers-6.8.0-85-generic   6.8.0-85.85  amd64  Linux kernel headers for version 6.8.0 on 64 bit x86 SMP
      # ii  linux-image-6.8.0-83-generic     6.8.0-83.83  amd64  Signed kernel image generic
      # ii  linux-image-6.8.0-85-generic     6.8.0-85.85  amd64  Signed kernel image generic
      # ii  linux-image-6.8.0-87-generic     6.8.0-87.87  amd64  Signed kernel image generic
      # ii  linux-image-generic              6.8.0-87.88  amd64  Generic Linux kernel image

      Status ii means fully installed; rc means removed without --purge, leaving config files behind. If /var/run/reboot-required indicates a newer kernel is waiting (Is a reboot needed), reboot first so uname -r switches to it.

    2. Preview what APT plans to remove

      Run a dry run (An irreversible command gets a dry run first):

      sudo apt autoremove --purge --dry-run

      Check The following packages will be REMOVED:. Make sure it doesn’t list the current uname -r version, the linux-image-generic / linux-generic metapackages, or any services you need.

    3. Remove old packages and their configs

      sudo apt autoremove --purge

      --purge drops config files and dpkg entries in one pass, avoiding leftover rc states. At the end, apt updates the boot menu via update-grub.

    When autoremove leaves old kernels behind

    Sometimes 4–5 old versions or leftover directories stay on disk even after apt autoremove --purge. There are three usual causes.

    Packages marked as manually installed

    If a specific kernel version was installed with apt install linux-image-6.8.0-… or carried over from a release upgrade, APT treats it as requested by you and won’t remove it.

    List manually marked kernel packages:

    apt-mark showmanual | grep -E '^linux-(image|headers|modules)-'
    # linux-image-6.8.0-83-generic
    # linux-headers-6.8.0-83-generic
    # linux-image-generic

    Keep the unversioned metapackages (linux-image-generic, linux-headers-generic, linux-generic) marked manual. Switch old versioned packages to auto, preview with a dry run, then remove:

    sudo apt-mark auto linux-image-6.8.0-83-generic linux-headers-6.8.0-83-generic
    sudo apt autoremove --purge --dry-run
    sudo apt autoremove --purge

    Leftover packages in rc state

    If old kernels were previously removed with plain apt autoremove (without --purge), rc entries remain in dpkg. List and purge them:

    dpkg -l 'linux-*' | awk '/^rc/ {print $2}'
    # linux-image-6.8.0-79-generic
    # linux-modules-6.8.0-79-generic
    
    dpkg -l 'linux-*' | awk '/^rc/ {print $2}' | xargs -r sudo dpkg --purge

    Leftover directories in /lib/modules or /usr/src

    When third-party modules were built against a kernel via DKMS, dpkg leaves the non-empty directory behind and prints directory '/lib/modules/…' not empty so not removed.

    Compare directories on disk against installed packages:

    ls /lib/modules
    # 6.8.0-79-generic  6.8.0-85-generic  6.8.0-87-generic
    
    dpkg -S /lib/modules/6.8.0-79-generic
    # dpkg-query: no path found matching pattern /lib/modules/6.8.0-79-generic

    If dpkg -S finds no owner and the version doesn’t match uname -r, remove the directory:

    sudo rm -rf /lib/modules/6.8.0-79-generic

    Check /usr/src/linux-headers-* the same way: any directory not owned by a package in dpkg -S can be removed.

    When /boot is 100% full and apt fails

    On a server with a separate 500 MB–1 GB /boot partition, filling it locks up apt itself: an interrupted upgrade tries to build initrd.img, fails with No space left on device, and apt autoremove refuses to run because of unmet dependencies.

    Break the deadlock directly with dpkg, which skips apt’s pending queue:

    1. Find the oldest inactive kernel

      df -h /boot
      # Filesystem      Size  Used Avail Use% Mounted on
      # /dev/sda2       974M  924M     0 100% /boot
      
      uname -r
      # 6.8.0-85-generic
      
      dpkg -l 'linux-image-*' | awk '/^ii/ {print $2}'
      # linux-image-6.8.0-79-generic
      # linux-image-6.8.0-83-generic
      # linux-image-6.8.0-85-generic
      # linux-image-6.8.0-87-generic
      # linux-image-generic

      Pick the oldest version that is neither running (uname -r) nor the newest — here 6.8.0-79-generic.

    2. Purge one old kernel with dpkg

      sudo dpkg --purge linux-image-6.8.0-79-generic linux-modules-extra-6.8.0-79-generic linux-modules-6.8.0-79-generic

      If linux-modules-extra-… isn’t installed (cloud images using -virtual or -kvm), drop it from the command. Removing one kernel frees 80–150 MB in /boot and around 300 MB in /lib/modules.

    3. Finish the interrupted update and clean the rest

      sudo apt --fix-broken install
      sudo apt autoremove --purge

      --fix-broken install finishes building initrd.img for the new kernel and updates GRUB, and autoremove --purge clears the remaining old versions.

    Keep old kernels from piling up again

    In automatic updates, cleanup is controlled by Unattended-Upgrade::Remove-Unused-Kernel-Packages "true"; in /etc/apt/apt.conf.d/52unattended-upgrades-local (unattended-upgrades). For it to trigger on time, the server has to reboot after a new kernel arrives (Automatic-Reboot "true" or a manual reboot per Is a reboot needed): otherwise the old kernel stays running and APT won’t delete it.

    If it doesn’t work

    apt autoremove --purge prints 0 to remove, but dpkg -l still lists five old kernels

    Cause
    The server hasn’t been rebooted after updates, or specific versioned linux-image-* and linux-headers-* packages are marked as manually installed (manual).
    Fix
    Check /var/run/reboot-required (Is a reboot needed) and reboot the server. If it’s already running the latest kernel, mark the old versioned packages as auto with sudo apt-mark auto … and run apt autoremove --purge again.

    /boot is 100% full, and both apt autoremove and apt --fix-broken install fail with No space left on device

    Cause
    The previous kernel upgrade aborted while building initrd.img, so apt insists on finishing that install first, but there’s no free space to build it.
    Fix
    Remove one oldest inactive kernel directly with sudo dpkg --purge linux-image-<version> linux-modules-extra-<version> linux-modules-<version> (omit -extra if not installed), then run sudo apt --fix-broken install and sudo apt autoremove --purge.

    dpkg warns directory '/lib/modules/…' not empty so not removed during removal

    Cause
    The directory still holds kernel modules built outside the package — via DKMS or by hand.
    Fix
    Verify the version doesn’t match uname -r and belongs to no package (dpkg -S /lib/modules/<version>), then remove it with sudo rm -rf /lib/modules/<version>.

    Space in /boot didn’t come back after apt autoremove, and dpkg -l shows packages in rc state

    Cause
    The command ran without --purge: binaries were removed, but config files and entries stayed in the dpkg database.
    Fix
    Purge rc packages with dpkg -l 'linux-*' | awk '/^rc/ {print $2}' | xargs -r sudo dpkg --purge.

    New kernel versions stopped arriving after cleanup

    Cause
    You removed the linux-image-generic or linux-generic metapackage along with old versions, which is what pulls in newer builds.
    Fix
    Reinstall the metapackage: sudo apt install linux-generic (or linux-virtual — match what the server used before).

    How to check

    uname -r
    # 6.8.0-87-generic
    
    dpkg -l 'linux-image-*' 'linux-headers-*' 'linux-modules-*' | grep -E '^(ii|rc)'
    # ii  linux-headers-6.8.0-85           6.8.0-85.85  all    …
    # ii  linux-headers-6.8.0-85-generic   6.8.0-85.85  amd64  …
    # ii  linux-headers-6.8.0-87           6.8.0-87.87  all    …
    # ii  linux-headers-6.8.0-87-generic   6.8.0-87.87  amd64  …
    # ii  linux-image-6.8.0-85-generic     6.8.0-85.85  amd64  …
    # ii  linux-image-6.8.0-87-generic     6.8.0-87.87  amd64  …
    # ii  linux-image-generic              6.8.0-87.88  amd64  …
    
    ls /lib/modules
    # 6.8.0-85-generic  6.8.0-87-generic
    
    df -h /boot /

    dpkg -l lists only two kernel versions (running and one fallback) plus the metapackage, no rc lines remain, /lib/modules holds only those two directories, and /boot and / show reclaimed space.