What's the purpose of using `sudo -S` explicitly?

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











up vote
3
down vote

favorite
2












From manpage of sudo:




-S, --stdin



Write the prompt to the standard error and read the password from the
standard input instead of using the terminal device. The password must
be followed by a newline character.




What is the purpose of using sudo -S instead of just sudo?



  • Is it correct thatsudo by default read password from standard input?


  • What is the purpose of "Write the prompt to the standard error"? Does sudo by default write it to the standard output?


  • Do they both require that the password must be followed by a newline character?


For example, in https://stackoverflow.com/a/39553081/156458, sudo -S true still requires typing in password, so how does it solve the original question in that post? I found that link when I searched for solution to Shall I run a sudo-required script in some shell configuration file?



Thanks.




Update:



The reply by J.Taylor said




sudo does not read the password from stdin by default - it reads it from the terminal interface.




I was wondering how to understand it in terms of implementation.



Is it correct that when a program reads from standard input, it reads from file descriptor 0 to which the standard input is always binded?



Why can't I tell whether sudo uses standard input or terminal when usingsudo without -S?



How can a program (such as sudo -S) achieve to read from terminal instead of standard input?







share|improve this question


























    up vote
    3
    down vote

    favorite
    2












    From manpage of sudo:




    -S, --stdin



    Write the prompt to the standard error and read the password from the
    standard input instead of using the terminal device. The password must
    be followed by a newline character.




    What is the purpose of using sudo -S instead of just sudo?



    • Is it correct thatsudo by default read password from standard input?


    • What is the purpose of "Write the prompt to the standard error"? Does sudo by default write it to the standard output?


    • Do they both require that the password must be followed by a newline character?


    For example, in https://stackoverflow.com/a/39553081/156458, sudo -S true still requires typing in password, so how does it solve the original question in that post? I found that link when I searched for solution to Shall I run a sudo-required script in some shell configuration file?



    Thanks.




    Update:



    The reply by J.Taylor said




    sudo does not read the password from stdin by default - it reads it from the terminal interface.




    I was wondering how to understand it in terms of implementation.



    Is it correct that when a program reads from standard input, it reads from file descriptor 0 to which the standard input is always binded?



    Why can't I tell whether sudo uses standard input or terminal when usingsudo without -S?



    How can a program (such as sudo -S) achieve to read from terminal instead of standard input?







    share|improve this question
























      up vote
      3
      down vote

      favorite
      2









      up vote
      3
      down vote

      favorite
      2






      2





      From manpage of sudo:




      -S, --stdin



      Write the prompt to the standard error and read the password from the
      standard input instead of using the terminal device. The password must
      be followed by a newline character.




      What is the purpose of using sudo -S instead of just sudo?



      • Is it correct thatsudo by default read password from standard input?


      • What is the purpose of "Write the prompt to the standard error"? Does sudo by default write it to the standard output?


      • Do they both require that the password must be followed by a newline character?


      For example, in https://stackoverflow.com/a/39553081/156458, sudo -S true still requires typing in password, so how does it solve the original question in that post? I found that link when I searched for solution to Shall I run a sudo-required script in some shell configuration file?



      Thanks.




      Update:



      The reply by J.Taylor said




      sudo does not read the password from stdin by default - it reads it from the terminal interface.




      I was wondering how to understand it in terms of implementation.



      Is it correct that when a program reads from standard input, it reads from file descriptor 0 to which the standard input is always binded?



      Why can't I tell whether sudo uses standard input or terminal when usingsudo without -S?



      How can a program (such as sudo -S) achieve to read from terminal instead of standard input?







      share|improve this question














      From manpage of sudo:




      -S, --stdin



      Write the prompt to the standard error and read the password from the
      standard input instead of using the terminal device. The password must
      be followed by a newline character.




      What is the purpose of using sudo -S instead of just sudo?



      • Is it correct thatsudo by default read password from standard input?


      • What is the purpose of "Write the prompt to the standard error"? Does sudo by default write it to the standard output?


      • Do they both require that the password must be followed by a newline character?


      For example, in https://stackoverflow.com/a/39553081/156458, sudo -S true still requires typing in password, so how does it solve the original question in that post? I found that link when I searched for solution to Shall I run a sudo-required script in some shell configuration file?



      Thanks.




      Update:



      The reply by J.Taylor said




      sudo does not read the password from stdin by default - it reads it from the terminal interface.




      I was wondering how to understand it in terms of implementation.



      Is it correct that when a program reads from standard input, it reads from file descriptor 0 to which the standard input is always binded?



      Why can't I tell whether sudo uses standard input or terminal when usingsudo without -S?



      How can a program (such as sudo -S) achieve to read from terminal instead of standard input?









      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 1 at 13:07

























      asked Mar 31 at 23:43









      Tim

      22.6k63224401




      22.6k63224401




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          8
          down vote



          accepted










          sudo does not read the password from stdin by default - it reads it from the terminal interface. Using sudo -S allows you to pipe the password in from another command/file like this: printf "yourpasswordn" | sudo -S nano /etc/apt/sources.list



          This could be used in a shell script to log in to sudo without being prompted for a password, but you need to be careful not to execute this kind of thing from the shell directly, because then your sudo password would be in the shell history.






          share|improve this answer






















          • Thanks. " it reads it from the terminal interface." But I always provide password to sudo from standard input. Isn't standard input connected to the terminal interface/emulator by default?
            – Tim
            Apr 1 at 0:55











          • By default, yes it is. The point of -S is when you're overriding the default somehow, such as by piping to sudo.
            – Joseph Sible
            Apr 1 at 3:39










          • @Joseph Usually a program accepts input from standard input (i.e. file descriptor 0). sudo doesn't do so. Does sudo open a fixed device file which represents the terminal?
            – Tim
            Apr 1 at 4:05







          • 6




            For the password prompt, yes. For example, echo abcd | sudo cat will let you type the password, rather than using abcd as the password.
            – Joseph Sible
            Apr 1 at 4:06










          • @JosephSible Thanks. (1) I was wondering what you meant by "For the password prompt, yes" ? (2) Why can't I tell if sudo uses standard input or terminal when using just sudo? How does a program use terminal instead of standard input?
            – Tim
            Apr 1 at 12:59











          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: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          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%2f434759%2fwhats-the-purpose-of-using-sudo-s-explicitly%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          8
          down vote



          accepted










          sudo does not read the password from stdin by default - it reads it from the terminal interface. Using sudo -S allows you to pipe the password in from another command/file like this: printf "yourpasswordn" | sudo -S nano /etc/apt/sources.list



          This could be used in a shell script to log in to sudo without being prompted for a password, but you need to be careful not to execute this kind of thing from the shell directly, because then your sudo password would be in the shell history.






          share|improve this answer






















          • Thanks. " it reads it from the terminal interface." But I always provide password to sudo from standard input. Isn't standard input connected to the terminal interface/emulator by default?
            – Tim
            Apr 1 at 0:55











          • By default, yes it is. The point of -S is when you're overriding the default somehow, such as by piping to sudo.
            – Joseph Sible
            Apr 1 at 3:39










          • @Joseph Usually a program accepts input from standard input (i.e. file descriptor 0). sudo doesn't do so. Does sudo open a fixed device file which represents the terminal?
            – Tim
            Apr 1 at 4:05







          • 6




            For the password prompt, yes. For example, echo abcd | sudo cat will let you type the password, rather than using abcd as the password.
            – Joseph Sible
            Apr 1 at 4:06










          • @JosephSible Thanks. (1) I was wondering what you meant by "For the password prompt, yes" ? (2) Why can't I tell if sudo uses standard input or terminal when using just sudo? How does a program use terminal instead of standard input?
            – Tim
            Apr 1 at 12:59















          up vote
          8
          down vote



          accepted










          sudo does not read the password from stdin by default - it reads it from the terminal interface. Using sudo -S allows you to pipe the password in from another command/file like this: printf "yourpasswordn" | sudo -S nano /etc/apt/sources.list



          This could be used in a shell script to log in to sudo without being prompted for a password, but you need to be careful not to execute this kind of thing from the shell directly, because then your sudo password would be in the shell history.






          share|improve this answer






















          • Thanks. " it reads it from the terminal interface." But I always provide password to sudo from standard input. Isn't standard input connected to the terminal interface/emulator by default?
            – Tim
            Apr 1 at 0:55











          • By default, yes it is. The point of -S is when you're overriding the default somehow, such as by piping to sudo.
            – Joseph Sible
            Apr 1 at 3:39










          • @Joseph Usually a program accepts input from standard input (i.e. file descriptor 0). sudo doesn't do so. Does sudo open a fixed device file which represents the terminal?
            – Tim
            Apr 1 at 4:05







          • 6




            For the password prompt, yes. For example, echo abcd | sudo cat will let you type the password, rather than using abcd as the password.
            – Joseph Sible
            Apr 1 at 4:06










          • @JosephSible Thanks. (1) I was wondering what you meant by "For the password prompt, yes" ? (2) Why can't I tell if sudo uses standard input or terminal when using just sudo? How does a program use terminal instead of standard input?
            – Tim
            Apr 1 at 12:59













          up vote
          8
          down vote



          accepted







          up vote
          8
          down vote



          accepted






          sudo does not read the password from stdin by default - it reads it from the terminal interface. Using sudo -S allows you to pipe the password in from another command/file like this: printf "yourpasswordn" | sudo -S nano /etc/apt/sources.list



          This could be used in a shell script to log in to sudo without being prompted for a password, but you need to be careful not to execute this kind of thing from the shell directly, because then your sudo password would be in the shell history.






          share|improve this answer














          sudo does not read the password from stdin by default - it reads it from the terminal interface. Using sudo -S allows you to pipe the password in from another command/file like this: printf "yourpasswordn" | sudo -S nano /etc/apt/sources.list



          This could be used in a shell script to log in to sudo without being prompted for a password, but you need to be careful not to execute this kind of thing from the shell directly, because then your sudo password would be in the shell history.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 1 at 0:54

























          answered Apr 1 at 0:49









          J. Taylor

          1,60211322




          1,60211322











          • Thanks. " it reads it from the terminal interface." But I always provide password to sudo from standard input. Isn't standard input connected to the terminal interface/emulator by default?
            – Tim
            Apr 1 at 0:55











          • By default, yes it is. The point of -S is when you're overriding the default somehow, such as by piping to sudo.
            – Joseph Sible
            Apr 1 at 3:39










          • @Joseph Usually a program accepts input from standard input (i.e. file descriptor 0). sudo doesn't do so. Does sudo open a fixed device file which represents the terminal?
            – Tim
            Apr 1 at 4:05







          • 6




            For the password prompt, yes. For example, echo abcd | sudo cat will let you type the password, rather than using abcd as the password.
            – Joseph Sible
            Apr 1 at 4:06










          • @JosephSible Thanks. (1) I was wondering what you meant by "For the password prompt, yes" ? (2) Why can't I tell if sudo uses standard input or terminal when using just sudo? How does a program use terminal instead of standard input?
            – Tim
            Apr 1 at 12:59

















          • Thanks. " it reads it from the terminal interface." But I always provide password to sudo from standard input. Isn't standard input connected to the terminal interface/emulator by default?
            – Tim
            Apr 1 at 0:55











          • By default, yes it is. The point of -S is when you're overriding the default somehow, such as by piping to sudo.
            – Joseph Sible
            Apr 1 at 3:39










          • @Joseph Usually a program accepts input from standard input (i.e. file descriptor 0). sudo doesn't do so. Does sudo open a fixed device file which represents the terminal?
            – Tim
            Apr 1 at 4:05







          • 6




            For the password prompt, yes. For example, echo abcd | sudo cat will let you type the password, rather than using abcd as the password.
            – Joseph Sible
            Apr 1 at 4:06










          • @JosephSible Thanks. (1) I was wondering what you meant by "For the password prompt, yes" ? (2) Why can't I tell if sudo uses standard input or terminal when using just sudo? How does a program use terminal instead of standard input?
            – Tim
            Apr 1 at 12:59
















          Thanks. " it reads it from the terminal interface." But I always provide password to sudo from standard input. Isn't standard input connected to the terminal interface/emulator by default?
          – Tim
          Apr 1 at 0:55





          Thanks. " it reads it from the terminal interface." But I always provide password to sudo from standard input. Isn't standard input connected to the terminal interface/emulator by default?
          – Tim
          Apr 1 at 0:55













          By default, yes it is. The point of -S is when you're overriding the default somehow, such as by piping to sudo.
          – Joseph Sible
          Apr 1 at 3:39




          By default, yes it is. The point of -S is when you're overriding the default somehow, such as by piping to sudo.
          – Joseph Sible
          Apr 1 at 3:39












          @Joseph Usually a program accepts input from standard input (i.e. file descriptor 0). sudo doesn't do so. Does sudo open a fixed device file which represents the terminal?
          – Tim
          Apr 1 at 4:05





          @Joseph Usually a program accepts input from standard input (i.e. file descriptor 0). sudo doesn't do so. Does sudo open a fixed device file which represents the terminal?
          – Tim
          Apr 1 at 4:05





          6




          6




          For the password prompt, yes. For example, echo abcd | sudo cat will let you type the password, rather than using abcd as the password.
          – Joseph Sible
          Apr 1 at 4:06




          For the password prompt, yes. For example, echo abcd | sudo cat will let you type the password, rather than using abcd as the password.
          – Joseph Sible
          Apr 1 at 4:06












          @JosephSible Thanks. (1) I was wondering what you meant by "For the password prompt, yes" ? (2) Why can't I tell if sudo uses standard input or terminal when using just sudo? How does a program use terminal instead of standard input?
          – Tim
          Apr 1 at 12:59





          @JosephSible Thanks. (1) I was wondering what you meant by "For the password prompt, yes" ? (2) Why can't I tell if sudo uses standard input or terminal when using just sudo? How does a program use terminal instead of standard input?
          – Tim
          Apr 1 at 12:59













           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f434759%2fwhats-the-purpose-of-using-sudo-s-explicitly%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

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

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay