How to delete older kernels Fedora 28
FedoraForum.org - Fedora Support Forums and Community
Results 1 to 15 of 15
  1. #1
    Join Date
    Apr 2018
    Location
    USA
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to delete older kernels Fedora 28

    My boot partition is 99% full. I can not figure out how to delete the older kernels. I have changed the dnf.conf file to only keep 2. But there is still 3. Please help. Thanks.

  2. #2
    Join Date
    Sep 2009
    Posts
    2,208
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: How to delete older kernels Fedora 28

    First, decide which kernel you want to delete:
    Code:
    $ sudo dnf list installed kernel
    Installed Packages
    kernel.x86_64                           4.16.4-200.fc27                            @@commandline
    kernel.x86_64                           4.16.5-200.fc27                            @@commandline
    kernel.x86_64                           4.16.6-200.fc27                            @@commandline
    kernel.x86_64                           4.16.6-202.fc27                            @@commandline
    kernel.x86_64                           4.16.7-200.fc27                            @@commandline
    Be sure you are not running the kernel you plan on removing:
    Code:
    $ uname -r
    4.16.7-200.fc27.x86_64
    Usually, it would be the oldest one. But there are multiple packages to delete. Notice how the version gets merged into the kernel name:
    Code:
    $ sudo dnf list installed kernel*4.16.4-200.fc27.x86_64
    Installed Packages
    kernel.x86_64                                  4.16.4-200.fc27                     @@commandline
    kernel-core.x86_64                             4.16.4-200.fc27                     @@commandline
    kernel-devel.x86_64                            4.16.4-200.fc27                     @@commandline
    kernel-modules.x86_64                          4.16.4-200.fc27                     @@commandline
    kernel-modules-extra.x86_64                    4.16.4-200.fc27                     @@commandline
    The remove command is:
    Code:
    $ sudo dnf remove kernel*4.16.4-200.fc27.x86_64
    Dependencies resolved.
    ================================================================================================
     Package                     Arch          Version                   Repository            Size
    ================================================================================================
    Removing:
     kernel                      x86_64        4.16.4-200.fc27           @@commandline          0  
     kernel-core                 x86_64        4.16.4-200.fc27           @@commandline         58 M
     kernel-devel                x86_64        4.16.4-200.fc27           @@commandline         48 M
     kernel-modules              x86_64        4.16.4-200.fc27           @@commandline         26 M
     kernel-modules-extra        x86_64        4.16.4-200.fc27           @@commandline        2.1 M
    
    Transaction Summary
    ================================================================================================
    Remove  5 Packages
    
    Freed space: 135 M
    Is this ok [y/N]:
    dd_wizard

  3. #3
    Join Date
    Apr 2018
    Location
    USA
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: How to delete older kernels Fedora 28

    Thank You. It was erroring out but then out of the blue I decided to drop into a bash shell and it worked! Thanks Again. Mike

  4. #4
    Join Date
    Jul 2018
    Location
    krupka
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: How to delete older kernels Fedora 28

    Will this automaticaly take care of the grub boot menu?

  5. #5
    Join Date
    Apr 2018
    Location
    USA
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: How to delete older kernels Fedora 28

    I don't remember if it did, it has been awhile. You can always update-grub if it does not.

  6. #6
    PabloTwo's Avatar
    PabloTwo is offline "Registered User" T-Shirt Winner
    Join Date
    Mar 2007
    Location
    Seville, FL
    Posts
    7,978
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)

    Re: How to delete older kernels Fedora 28

    Quote Originally Posted by terpentijn
    Will this automaticaly take care of the grub boot menu?
    Yes.

  7. #7
    Join Date
    Jul 2018
    Location
    krupka
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: How to delete older kernels Fedora 28

    Hey, nice. I did not know fedora uses the same syntax for upgrading the grub menu as ubuntu/debian

  8. #8
    PabloTwo's Avatar
    PabloTwo is offline "Registered User" T-Shirt Winner
    Join Date
    Mar 2007
    Location
    Seville, FL
    Posts
    7,978
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)

    Re: How to delete older kernels Fedora 28

    You can always update-grub if it does not.
    Hey, nice. I did not know fedora uses the same syntax for upgrading the grub menu as ubuntu/debian
    Well, no, it does not.
    Code:
    $ which update-grub
    /usr/bin/which: no update-grub in (/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin)

  9. #9
    Join Date
    Dec 2017
    Location
    Sosnowiec, PL
    Age
    37
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: How to delete older kernels Fedora 28

    Maybe something like this?
    Code:
    #!/bin/bash
    ### NEWEST=? maybe some of users could produce something better ###
    # NEWEST=$(rpm -qa --last kernel | head -n1 | awk '{print $1}')
    NEWEST=$(rpm -q kernel | tail -n1)
    CURRENTKERNEL="kernel-$(uname -r)"
    OLDKERN="$(rpm -qa ^kernel* | grep -v "$(uname -r)")"
    COUNT=$(rpm -q kernel | wc -l)
    GREEN="\033[1;32m"
    RED="\033[1;31m"
    NC="\033[0m"
    SEPARATOR=$(perl -E "print '-' x 60")
    
    if [ "$COUNT" -gt 1 ]; then
      if ! [ "$NEWEST" == "$CURRENTKERNEL" ]; then
        echo "We're not running newest kernel, exiting ..."
      else
        echo -e "\nCurrent kernel:\n$SEPARATOR" "$GREEN"
        echo -e "$CURRENTKERNEL"
        echo -e "$NC""$SEPARATOR\n"
        echo -e "Kernels to remove:\n$SEPARATOR" "$RED"
        printf '%s\n' "$OLDKERN"
        echo -e "$NC""$SEPARATOR\n"
        echo -n "Do you want to proceed? (y/n) "
        read response
        if [ "$response" == "y" ]; then
          dnf remove $OLDKERN
          if [ "$?" == 0 -a -x /usr/sbin/grub2-mkconfig ]; then
            /usr/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg
          fi
        else
          echo "Cancelled. Exiting ..."
          exit 0
        fi
      fi
    else
      echo "There's nothing to do!"
    fi

    System must run with the newest kernel. Command grub2-mkconfig is performed only if necessary (if dnf remove ends with success).

    Use on your own responsibility ;]
    Last edited by marcin82; 28th July 2018 at 07:38 PM.
    marcin82

  10. #10
    Join Date
    Aug 2007
    Posts
    541
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: How to delete older kernels Fedora 28

    You can always create a one line alias in .bashrc so you can use the shorter command as I have done.

    Code:
    $ type update-grub
    update-grub is aliased to `grub2-mkconfig -o /boot/grub2/grub.cfg'
    $ grep grub ~/.bashrc
    alias update-grub="grub2-mkconfig -o /boot/grub2/grub.cfg"

  11. #11
    Join Date
    Aug 2007
    Posts
    541
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: How to delete older kernels Fedora 28

    marcin82 there is a major problem with your script. The command rpm -qa kernel does not order kernels from newest to oldest. In my case it appears to be a random order.

    Code:
    $ yum list installed kernel
    Installed Packages                                                                                                                                                     
    kernel.x86_64                                                               3.10.0-327.el7                                                                    @anaconda
    kernel.x86_64                                                               3.10.0-327.22.2.el7                                                               @updates 
    kernel.x86_64                                                               3.10.0-514.2.2.el7                                                                @updates 
    kernel.x86_64                                                               3.10.0-862.2.3.el7                                                                @updates
    
    $ uname -r
    3.10.0-862.2.3.el7.x86_64
    3.10.0-862.2.3 is the newest kernel and appears last in the list of installed kernels as it is the most recent. I am using Centos 7.5 which still uses yum.

    However with rpm -qa kernel.

    Code:
    $ rpm -qa kernel 
    kernel-3.10.0-327.el7.x86_64
    kernel-3.10.0-862.2.3.el7.x86_64
    kernel-3.10.0-327.22.2.el7.x86_64
    kernel-3.10.0-514.2.2.el7.x86_64
    $ rpm -qa kernel | head -n1
    kernel-3.10.0-327.el7.x86_64
    The first kernel in the list is actually the oldest, installed by Anaconda when the system was first installed from the iso. The current kernel 862 is second in this list, followed by the second oldest. Therefore rpm -qa kernel simply outputs kernels in a random order.

    It would be much better to use dnf list installed kernel | tail -n1 as dnf does list packages in install order as it actually does the installing and knows the install times. To filter out the kernel version use awk.

    Code:
    dnf list installed kernel 2>/dev/null | tail -n1 | awk '/kernel.x86_64/ {print $2}'
    Last edited by amiga; 27th July 2018 at 09:32 PM.

  12. #12
    Join Date
    Dec 2017
    Location
    Sosnowiec, PL
    Age
    37
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: How to delete older kernels Fedora 28

    Yes, I've faced with this sort of problems, but can't even imaging all variants

    It would be much better to use dnf list installed kernel | tail -n1 as dnf does list packages in install order as it actually does the installing and knows the install times.
    This is very good idea, but there is also available to play with rpm tags and sort command (less handy) ;]
    Code:
    [root@fedora ~]# rpm -qa kernel --queryformat '%{name}-%{version}\n' | sort -nr | head -n1
    kernel-4.17.7
    For comparison - sorting data on another (non rpm) system - just your kernels - "quick equivalent" for %{name}-%{version} filtering:
    Code:
    cat plik | sed 's/.el7.*//' | sort -nr
    kernel-3.10.0-862.2.3
    kernel-3.10.0-514.2.2
    kernel-3.10.0-327.22.2
    kernel-3.10.0-327
    EDIT:
    [...] as it actually does the installing and knows the install times.
    Sometimes this may generate problems, because eg. older kernel may be installed by user later than newer, etc.

    EDIT 2:
    There could be many more exceptions, eg. the same version with differ package %{release} number, naming conventions, custom kernel names, etc. It's all not so simple ...

    EDIT 3:


    I've tested on CentOS 7.5 with two kernels (after yum distro-sync) and looks like rpm -q kernel | tail -n1 should be sufficient command. Kernels are ordered as should be:
    Last edited by marcin82; 28th July 2018 at 07:41 PM.
    marcin82

  13. #13
    Join Date
    Oct 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: How to delete older kernels Fedora 28

    Quote Originally Posted by loonymike
    My boot partition is 99% full. I can not figure out how to delete the older kernels. I have changed the dnf.conf file to only keep 2. But there is still 3. Please help. Thanks.
    This is what I do:

    - make sure I reboot to the latest kernel to verify it work with it (especially proprietary wireless and/or graphics drives modules)
    - Issue this command:

    sudo dnf remove $(rpm -qa kernel* | sed "/`uname -r/d")

  14. #14
    Join Date
    Aug 2007
    Posts
    541
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: How to delete older kernels Fedora 28

    You are missing the second backtick after uname -r .

    sudo dnf remove $(rpm -qa kernel* | sed "/`uname -r/d")
    This is one reason why many prefer the newer format as brackets are more visible such as below. Also it is better to use the same form everywhere for consistency.

    Code:
    sudo dnf remove $(rpm -qa kernel* | sed "/$(uname -r)/d")
    Last edited by amiga; 27th August 2018 at 10:20 AM.

  15. #15
    Join Date
    Jun 2004
    Location
    Maryland, US
    Posts
    7,702
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Re: How to delete older kernels Fedora 28

    Quote Originally Posted by amiga
    .....
    However with rpm -qa kernel.

    Code:
    $ rpm -qa kernel 
    kernel-3.10.0-327.el7.x86_64
    kernel-3.10.0-862.2.3.el7.x86_64
    kernel-3.10.0-327.22.2.el7.x86_64
    kernel-3.10.0-514.2.2.el7.x86_64
    $ rpm -qa kernel | head -n1
    kernel-3.10.0-327.el7.x86_64
    The first kernel in the list is actually the oldest, installed by Anaconda when the system was first installed from the iso. The current kernel 862 is second in this list, followed by the second oldest. Therefore rpm -qa kernel simply outputs kernels in a random order.
    You can use the "--last" option with rpm -qa, then the packages will be in time order (newest to top):
    Code:
    rpm -qa --last kernel
    kernel-4.17.19-200.fc28.x86_64                Sun 26 Aug 2018 01:40:56 AM EDT
    kernel-4.17.17-200.fc28.x86_64                Sat 25 Aug 2018 04:40:59 AM EDT
    kernel-4.17.14-202.fc28.x86_64                Wed 22 Aug 2018 03:20:21 AM EDT

Similar Threads

  1. Replies: 8
    Last Post: 29th November 2009, 01:34 AM
  2. Fedora KVM, clock drift, time and older x86_64 kernels
    By RobertThiem in forum Using Fedora
    Replies: 0
    Last Post: 19th January 2009, 11:18 PM
  3. Where to find older fedora kernels ?
    By cuban_cigar in forum Using Fedora
    Replies: 1
    Last Post: 29th July 2008, 12:21 AM
  4. Where do older kernels go?
    By bazzoon in forum Using Fedora
    Replies: 2
    Last Post: 27th March 2008, 09:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •