Remove all kernel packages except the currently used one

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












How can I automatically delete all other kernels which I'm not using?



So, here's my situation:



  • Using different versions of Ubuntu / CentOS

  • Automatic updates were on -> Linux downloaded many new kernel versions

  • Now I want to delete all other kernel versions, except the one I'm currently using

Code:



#find out current kernel version:
uname -r

#check which versions are downloaded to computer
dpkg --get-selections | grep linux-image

#delete desired kernel
sudo apt-get purge [KERNEL]

#autoremove
sudo apt-get autoremove


There can be easily more than 15 kernels in one linux, and I have more than 50 linuxes which I have to go through.










share|improve this question























  • Check the second answer on this post: askubuntu.com/questions/2793/… I think that is what you are looking for.
    – blametheadmin
    Feb 3 '17 at 8:26














up vote
3
down vote

favorite












How can I automatically delete all other kernels which I'm not using?



So, here's my situation:



  • Using different versions of Ubuntu / CentOS

  • Automatic updates were on -> Linux downloaded many new kernel versions

  • Now I want to delete all other kernel versions, except the one I'm currently using

Code:



#find out current kernel version:
uname -r

#check which versions are downloaded to computer
dpkg --get-selections | grep linux-image

#delete desired kernel
sudo apt-get purge [KERNEL]

#autoremove
sudo apt-get autoremove


There can be easily more than 15 kernels in one linux, and I have more than 50 linuxes which I have to go through.










share|improve this question























  • Check the second answer on this post: askubuntu.com/questions/2793/… I think that is what you are looking for.
    – blametheadmin
    Feb 3 '17 at 8:26












up vote
3
down vote

favorite









up vote
3
down vote

favorite











How can I automatically delete all other kernels which I'm not using?



So, here's my situation:



  • Using different versions of Ubuntu / CentOS

  • Automatic updates were on -> Linux downloaded many new kernel versions

  • Now I want to delete all other kernel versions, except the one I'm currently using

Code:



#find out current kernel version:
uname -r

#check which versions are downloaded to computer
dpkg --get-selections | grep linux-image

#delete desired kernel
sudo apt-get purge [KERNEL]

#autoremove
sudo apt-get autoremove


There can be easily more than 15 kernels in one linux, and I have more than 50 linuxes which I have to go through.










share|improve this question















How can I automatically delete all other kernels which I'm not using?



So, here's my situation:



  • Using different versions of Ubuntu / CentOS

  • Automatic updates were on -> Linux downloaded many new kernel versions

  • Now I want to delete all other kernel versions, except the one I'm currently using

Code:



#find out current kernel version:
uname -r

#check which versions are downloaded to computer
dpkg --get-selections | grep linux-image

#delete desired kernel
sudo apt-get purge [KERNEL]

#autoremove
sudo apt-get autoremove


There can be easily more than 15 kernels in one linux, and I have more than 50 linuxes which I have to go through.







ubuntu apt linux-kernel package-management






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 3 '17 at 21:59









Gilles

523k12610461576




523k12610461576










asked Feb 3 '17 at 8:01









NoobPenguin

163




163











  • Check the second answer on this post: askubuntu.com/questions/2793/… I think that is what you are looking for.
    – blametheadmin
    Feb 3 '17 at 8:26
















  • Check the second answer on this post: askubuntu.com/questions/2793/… I think that is what you are looking for.
    – blametheadmin
    Feb 3 '17 at 8:26















Check the second answer on this post: askubuntu.com/questions/2793/… I think that is what you are looking for.
– blametheadmin
Feb 3 '17 at 8:26




Check the second answer on this post: askubuntu.com/questions/2793/… I think that is what you are looking for.
– blametheadmin
Feb 3 '17 at 8:26










3 Answers
3






active

oldest

votes

















up vote
2
down vote













On Ubuntu you can use purge-old-kernels to purge old kernels , to install it:



For ubuntu 16.04



sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F430BBA5
sudo add-apt-repository ppa:byobu/ppa
sudo apt-get update
sudo apt-get install byobu


Also you can install it without adding the ppa: sudo apt-get install byobu



For Ubuntu versions lower than 16.04



sudo add-apt-repository ppa:bikeshed/ppa
sudo apt-get update
sudo apt-get install bikeshed


Run the following command to keeping the latest 2 kernel :



sudo purge-old-kernels


You can specify the number n of kernel to keep:



sudo purge-old-kernels --keep n


n=1:



sudo purge-old-kernels --keep 1



On centOS install yum-utils package :



yum install yum-utils


To keep the latest n kernels , run:



package-cleanup --oldkernels --count=n


n=1:



package-cleanup --oldkernels --count=1





share|improve this answer



























    up vote
    0
    down vote













    Be aware, this command removes all kernel images and headers of major release 4, but not the running one.



    apt purge $(dpkg-query -W -f='$binary:Packagen' 'linux-image-4*' 'linux-headers-4*' | grep -v $(uname -r))





    share|improve this answer



























      up vote
      0
      down vote













      You can try the below command. Before you execute apt-get purge, just double check the output of your grep to not contain your current kernel version



      dpkg --list | grep linux-image | awk 'print $2' | grep -v linux-image-`uname -r` | xargs apt-get purge





      share|improve this answer




















        Your Answer








        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "106"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f342199%2fremove-all-kernel-packages-except-the-currently-used-one%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote













        On Ubuntu you can use purge-old-kernels to purge old kernels , to install it:



        For ubuntu 16.04



        sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F430BBA5
        sudo add-apt-repository ppa:byobu/ppa
        sudo apt-get update
        sudo apt-get install byobu


        Also you can install it without adding the ppa: sudo apt-get install byobu



        For Ubuntu versions lower than 16.04



        sudo add-apt-repository ppa:bikeshed/ppa
        sudo apt-get update
        sudo apt-get install bikeshed


        Run the following command to keeping the latest 2 kernel :



        sudo purge-old-kernels


        You can specify the number n of kernel to keep:



        sudo purge-old-kernels --keep n


        n=1:



        sudo purge-old-kernels --keep 1



        On centOS install yum-utils package :



        yum install yum-utils


        To keep the latest n kernels , run:



        package-cleanup --oldkernels --count=n


        n=1:



        package-cleanup --oldkernels --count=1





        share|improve this answer
























          up vote
          2
          down vote













          On Ubuntu you can use purge-old-kernels to purge old kernels , to install it:



          For ubuntu 16.04



          sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F430BBA5
          sudo add-apt-repository ppa:byobu/ppa
          sudo apt-get update
          sudo apt-get install byobu


          Also you can install it without adding the ppa: sudo apt-get install byobu



          For Ubuntu versions lower than 16.04



          sudo add-apt-repository ppa:bikeshed/ppa
          sudo apt-get update
          sudo apt-get install bikeshed


          Run the following command to keeping the latest 2 kernel :



          sudo purge-old-kernels


          You can specify the number n of kernel to keep:



          sudo purge-old-kernels --keep n


          n=1:



          sudo purge-old-kernels --keep 1



          On centOS install yum-utils package :



          yum install yum-utils


          To keep the latest n kernels , run:



          package-cleanup --oldkernels --count=n


          n=1:



          package-cleanup --oldkernels --count=1





          share|improve this answer






















            up vote
            2
            down vote










            up vote
            2
            down vote









            On Ubuntu you can use purge-old-kernels to purge old kernels , to install it:



            For ubuntu 16.04



            sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F430BBA5
            sudo add-apt-repository ppa:byobu/ppa
            sudo apt-get update
            sudo apt-get install byobu


            Also you can install it without adding the ppa: sudo apt-get install byobu



            For Ubuntu versions lower than 16.04



            sudo add-apt-repository ppa:bikeshed/ppa
            sudo apt-get update
            sudo apt-get install bikeshed


            Run the following command to keeping the latest 2 kernel :



            sudo purge-old-kernels


            You can specify the number n of kernel to keep:



            sudo purge-old-kernels --keep n


            n=1:



            sudo purge-old-kernels --keep 1



            On centOS install yum-utils package :



            yum install yum-utils


            To keep the latest n kernels , run:



            package-cleanup --oldkernels --count=n


            n=1:



            package-cleanup --oldkernels --count=1





            share|improve this answer












            On Ubuntu you can use purge-old-kernels to purge old kernels , to install it:



            For ubuntu 16.04



            sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F430BBA5
            sudo add-apt-repository ppa:byobu/ppa
            sudo apt-get update
            sudo apt-get install byobu


            Also you can install it without adding the ppa: sudo apt-get install byobu



            For Ubuntu versions lower than 16.04



            sudo add-apt-repository ppa:bikeshed/ppa
            sudo apt-get update
            sudo apt-get install bikeshed


            Run the following command to keeping the latest 2 kernel :



            sudo purge-old-kernels


            You can specify the number n of kernel to keep:



            sudo purge-old-kernels --keep n


            n=1:



            sudo purge-old-kernels --keep 1



            On centOS install yum-utils package :



            yum install yum-utils


            To keep the latest n kernels , run:



            package-cleanup --oldkernels --count=n


            n=1:



            package-cleanup --oldkernels --count=1






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 4 '17 at 20:08









            GAD3R

            24.6k1749104




            24.6k1749104






















                up vote
                0
                down vote













                Be aware, this command removes all kernel images and headers of major release 4, but not the running one.



                apt purge $(dpkg-query -W -f='$binary:Packagen' 'linux-image-4*' 'linux-headers-4*' | grep -v $(uname -r))





                share|improve this answer
























                  up vote
                  0
                  down vote













                  Be aware, this command removes all kernel images and headers of major release 4, but not the running one.



                  apt purge $(dpkg-query -W -f='$binary:Packagen' 'linux-image-4*' 'linux-headers-4*' | grep -v $(uname -r))





                  share|improve this answer






















                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    Be aware, this command removes all kernel images and headers of major release 4, but not the running one.



                    apt purge $(dpkg-query -W -f='$binary:Packagen' 'linux-image-4*' 'linux-headers-4*' | grep -v $(uname -r))





                    share|improve this answer












                    Be aware, this command removes all kernel images and headers of major release 4, but not the running one.



                    apt purge $(dpkg-query -W -f='$binary:Packagen' 'linux-image-4*' 'linux-headers-4*' | grep -v $(uname -r))






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 4 '17 at 10:28









                    ingopingo

                    61944




                    61944




















                        up vote
                        0
                        down vote













                        You can try the below command. Before you execute apt-get purge, just double check the output of your grep to not contain your current kernel version



                        dpkg --list | grep linux-image | awk 'print $2' | grep -v linux-image-`uname -r` | xargs apt-get purge





                        share|improve this answer
























                          up vote
                          0
                          down vote













                          You can try the below command. Before you execute apt-get purge, just double check the output of your grep to not contain your current kernel version



                          dpkg --list | grep linux-image | awk 'print $2' | grep -v linux-image-`uname -r` | xargs apt-get purge





                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            You can try the below command. Before you execute apt-get purge, just double check the output of your grep to not contain your current kernel version



                            dpkg --list | grep linux-image | awk 'print $2' | grep -v linux-image-`uname -r` | xargs apt-get purge





                            share|improve this answer












                            You can try the below command. Before you execute apt-get purge, just double check the output of your grep to not contain your current kernel version



                            dpkg --list | grep linux-image | awk 'print $2' | grep -v linux-image-`uname -r` | xargs apt-get purge






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 26 at 20:15









                            skashyap

                            11




                            11



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f342199%2fremove-all-kernel-packages-except-the-currently-used-one%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown






                                Popular posts from this blog

                                How to check contact read email or not when send email to Individual?

                                Displaying single band from multi-band raster using QGIS

                                How many registers does an x86_64 CPU actually have?