Skip to content

Instantly share code, notes, and snippets.

@spyesx
Last active May 2, 2025 12:23
Show Gist options
  • Save spyesx/b4b1a8c4470f2893cac4e025fa6d332d to your computer and use it in GitHub Desktop.
Save spyesx/b4b1a8c4470f2893cac4e025fa6d332d to your computer and use it in GitHub Desktop.

Revisions

  1. spyesx revised this gist Sep 11, 2022. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -70,6 +70,18 @@ dpkg -L $package
    dpkg -S $file
    ```

    Find heavy files in `/boot/firmware`:

    ```bash
    find /boot/firmware -size +8M -exec ls -lgd {} \;
    ```

    Sometime you will find some `*.bak` files. If everything works well, then remove them.

    ```bash
    rm -rf /boot/firmware/*.bak
    ```

    Best, do not install Ubuntu on a RPi. This is a bad distribution.

    ## More documentation
  2. spyesx revised this gist Jun 30, 2022. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -70,6 +70,8 @@ dpkg -L $package
    dpkg -S $file
    ```

    Best, do not install Ubuntu on a RPi. This is a bad distribution.

    ## More documentation

    Dive into Ubuntu community documentation to [remove old kernel](https://help.ubuntu.com/community/RemoveOldKernels)
  3. spyesx created this gist Jun 30, 2022.
    75 changes: 75 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    # Remove old linux kernels

    ## Script way

    ```bash
    # Dry run to check
    bash remove-old-kernels.sh

    # Run
    bash remove-old-kernels.sh exec
    ```

    ## Debian way

    ```bash
    # Which kernel are you using?
    uname -r

    # Which kernels are installed ?
    dpkg --list | egrep 'linux-image|linux-headers'
    ```

    - `ii` – indicates packages that are currently installed
    - `iU` – package has been unpacked and will be used next reboot
    - `rc` – package already removed, but the configuration files are still present

    `autoremove` alone remove older kernels only, but any package that is not needed as a dependency of other packages along with its configuration files.

    ```bash
    # Will autoremove every packages that qualify for it
    apt-get autoremove --purge

    # Remove all configuration files that are still present despite the removed package
    dpkg --list | egrep 'linux-image|linux-headers' | grep rc | awk '{print $2}' | xargs apt purge -y

    # Remove and purge one particular package
    # Be careful not to remove your current kernel !
    yes | apt purge "linux-image-XXXXX"
    ```

    `autoremove` will only remove packages that are automatically installed. If you ever updated or added a kernel package manually autoremove will not remove it. If you ever "held" a kernel version autoremove will not remove it. If you're wondering why Ubuntu is filling up your boot partition with kernels you no longer use it's likely one of these two reasons.

    ```bash
    # Unhold all packages
    dpkg --get-selections | grep hold | awk '{ print $1, "install" }' | dpkg --set-selections

    # Mark all "manually installed" kernel packages as "automatically installed"
    for f in $(apt-mark showmanual | grep linux-); do
    apt-mark auto $f
    done

    # Remove all packages that are no longer needed
    apt-get -y autoremove --purge
    ```
    ## Raspberry Pi

    `/boot/firmware` could get full after an Ubuntu (for RPi) upgrade. Solution above still apply but there is also `linux-firmware-*` to consider.

    ```bash
    # list "firmware" packages
    dpkg --list | grep "firmware"
    ```

    ```bash
    # Show where files are installed. To confirm they are well in /boot/firmware
    package=linux-firmware-raspi
    dpkg -L $package

    # or to see which $file belongs to wich package (could be a long execution time)
    dpkg -S $file
    ```

    ## More documentation

    Dive into Ubuntu community documentation to [remove old kernel](https://help.ubuntu.com/community/RemoveOldKernels)
    24 changes: 24 additions & 0 deletions remove-old-kernels.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    #!/bin/bash
    # Run this script without any param for a dry run
    # Run the script with root and with exec param for removing old kernels after checking
    # the list printed in the dry run
    # From:

    uname -a
    IN_USE=$(uname -a | awk '{ print $3 }')
    echo "Your in use kernel is $IN_USE"

    OLD_KERNELS=$(
    dpkg --list |
    grep -v "$IN_USE" |
    grep -Ei 'linux-image|linux-headers|linux-modules' |
    awk '{ print $2 }'
    )
    echo "Old Kernels to be removed:"
    echo "$OLD_KERNELS"

    if [ "$1" == "exec" ]; then
    for PACKAGE in $OLD_KERNELS; do
    yes | apt purge "$PACKAGE"
    done
    fi