Is it possible to get the output of only sudo command?











up vote
0
down vote

favorite












I want to write the output of only sudo command to some file i.e. the line which I get on terminal after writing sudo i.e.



[sudo] password for user:


If I use sudo ls > file or sudo ls 2> file then I get the stdout and stderr respectively of the whole command. Also I can't use sudo > file or sudo 2> file, then I will get error that I should use some options or command after sudo. So, is it possible to achieve?










share|improve this question
























  • For the avoidance of XY Problem solving, it seems prudent to ask why you'd want to capture this prompt from sudo
    – roaima
    Dec 3 at 21:22










  • @roaima, that was just for knowledge.
    – Debian_yadav
    Dec 4 at 11:36















up vote
0
down vote

favorite












I want to write the output of only sudo command to some file i.e. the line which I get on terminal after writing sudo i.e.



[sudo] password for user:


If I use sudo ls > file or sudo ls 2> file then I get the stdout and stderr respectively of the whole command. Also I can't use sudo > file or sudo 2> file, then I will get error that I should use some options or command after sudo. So, is it possible to achieve?










share|improve this question
























  • For the avoidance of XY Problem solving, it seems prudent to ask why you'd want to capture this prompt from sudo
    – roaima
    Dec 3 at 21:22










  • @roaima, that was just for knowledge.
    – Debian_yadav
    Dec 4 at 11:36













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to write the output of only sudo command to some file i.e. the line which I get on terminal after writing sudo i.e.



[sudo] password for user:


If I use sudo ls > file or sudo ls 2> file then I get the stdout and stderr respectively of the whole command. Also I can't use sudo > file or sudo 2> file, then I will get error that I should use some options or command after sudo. So, is it possible to achieve?










share|improve this question















I want to write the output of only sudo command to some file i.e. the line which I get on terminal after writing sudo i.e.



[sudo] password for user:


If I use sudo ls > file or sudo ls 2> file then I get the stdout and stderr respectively of the whole command. Also I can't use sudo > file or sudo 2> file, then I will get error that I should use some options or command after sudo. So, is it possible to achieve?







sudo io-redirection






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 at 11:37

























asked Dec 3 at 16:32









Debian_yadav

1,3603922




1,3603922












  • For the avoidance of XY Problem solving, it seems prudent to ask why you'd want to capture this prompt from sudo
    – roaima
    Dec 3 at 21:22










  • @roaima, that was just for knowledge.
    – Debian_yadav
    Dec 4 at 11:36


















  • For the avoidance of XY Problem solving, it seems prudent to ask why you'd want to capture this prompt from sudo
    – roaima
    Dec 3 at 21:22










  • @roaima, that was just for knowledge.
    – Debian_yadav
    Dec 4 at 11:36
















For the avoidance of XY Problem solving, it seems prudent to ask why you'd want to capture this prompt from sudo
– roaima
Dec 3 at 21:22




For the avoidance of XY Problem solving, it seems prudent to ask why you'd want to capture this prompt from sudo
– roaima
Dec 3 at 21:22












@roaima, that was just for knowledge.
– Debian_yadav
Dec 4 at 11:36




@roaima, that was just for knowledge.
– Debian_yadav
Dec 4 at 11:36










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










With sudo -S, the sudo utility will write its prompt to standard error, and will read the password from standard input. You can capture the output from standard error with 2>filename.



Note that you still have to supply a command for sudo to execute and that the standard error of this command would also be redirected to the file, unless you do some file descriptor juggling:



sudo -C 4 -S sh -c 'ls non-existant 2>&3' 3>&2 2>sudo.stderr


This would redirect the output of sudo to sudo.stderr while still allowing the executed command to write to the original standard error stream. We do this by opening file descriptor 3 as a copy of standard error, and then explicitly writing to this file descriptor in our command. For this to work, we have to ask sudo to not close file descriptor 3 (which it would do by default). This may require that an administrator has enabled the closefrom_override option in the sudoers file.



Note that there are likely subtle security concerns with the above command.






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%2f485723%2fis-it-possible-to-get-the-output-of-only-sudo-command%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    With sudo -S, the sudo utility will write its prompt to standard error, and will read the password from standard input. You can capture the output from standard error with 2>filename.



    Note that you still have to supply a command for sudo to execute and that the standard error of this command would also be redirected to the file, unless you do some file descriptor juggling:



    sudo -C 4 -S sh -c 'ls non-existant 2>&3' 3>&2 2>sudo.stderr


    This would redirect the output of sudo to sudo.stderr while still allowing the executed command to write to the original standard error stream. We do this by opening file descriptor 3 as a copy of standard error, and then explicitly writing to this file descriptor in our command. For this to work, we have to ask sudo to not close file descriptor 3 (which it would do by default). This may require that an administrator has enabled the closefrom_override option in the sudoers file.



    Note that there are likely subtle security concerns with the above command.






    share|improve this answer



























      up vote
      3
      down vote



      accepted










      With sudo -S, the sudo utility will write its prompt to standard error, and will read the password from standard input. You can capture the output from standard error with 2>filename.



      Note that you still have to supply a command for sudo to execute and that the standard error of this command would also be redirected to the file, unless you do some file descriptor juggling:



      sudo -C 4 -S sh -c 'ls non-existant 2>&3' 3>&2 2>sudo.stderr


      This would redirect the output of sudo to sudo.stderr while still allowing the executed command to write to the original standard error stream. We do this by opening file descriptor 3 as a copy of standard error, and then explicitly writing to this file descriptor in our command. For this to work, we have to ask sudo to not close file descriptor 3 (which it would do by default). This may require that an administrator has enabled the closefrom_override option in the sudoers file.



      Note that there are likely subtle security concerns with the above command.






      share|improve this answer

























        up vote
        3
        down vote



        accepted







        up vote
        3
        down vote



        accepted






        With sudo -S, the sudo utility will write its prompt to standard error, and will read the password from standard input. You can capture the output from standard error with 2>filename.



        Note that you still have to supply a command for sudo to execute and that the standard error of this command would also be redirected to the file, unless you do some file descriptor juggling:



        sudo -C 4 -S sh -c 'ls non-existant 2>&3' 3>&2 2>sudo.stderr


        This would redirect the output of sudo to sudo.stderr while still allowing the executed command to write to the original standard error stream. We do this by opening file descriptor 3 as a copy of standard error, and then explicitly writing to this file descriptor in our command. For this to work, we have to ask sudo to not close file descriptor 3 (which it would do by default). This may require that an administrator has enabled the closefrom_override option in the sudoers file.



        Note that there are likely subtle security concerns with the above command.






        share|improve this answer














        With sudo -S, the sudo utility will write its prompt to standard error, and will read the password from standard input. You can capture the output from standard error with 2>filename.



        Note that you still have to supply a command for sudo to execute and that the standard error of this command would also be redirected to the file, unless you do some file descriptor juggling:



        sudo -C 4 -S sh -c 'ls non-existant 2>&3' 3>&2 2>sudo.stderr


        This would redirect the output of sudo to sudo.stderr while still allowing the executed command to write to the original standard error stream. We do this by opening file descriptor 3 as a copy of standard error, and then explicitly writing to this file descriptor in our command. For this to work, we have to ask sudo to not close file descriptor 3 (which it would do by default). This may require that an administrator has enabled the closefrom_override option in the sudoers file.



        Note that there are likely subtle security concerns with the above command.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 3 at 17:13

























        answered Dec 3 at 17:01









        Kusalananda

        120k16225367




        120k16225367






























            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%2f485723%2fis-it-possible-to-get-the-output-of-only-sudo-command%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

            List directoties down one level, excluding some named directories and files

            list processes belonging to a network namespace

            list systemd RuntimeDirectory mounts