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

Tim

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?

J. Taylor

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 "yourpassword\n" | 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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What's the purpose of using a union with only one member?

From Dev

What is this pattern's name / purpose?

From Dev

What is the purpose of Guava's Verify?

From Dev

What's the purpose of partitioningBy

From Dev

What's the purpose of HTTP PUT?

From Dev

what's the purpose of factories?

From Dev

What's the purpose of varchar(0)

From Dev

What's the purpose of using semicolon in if condition?

From Dev

Access Modifiers - what's the purpose?

From Dev

What's the purpose of macros?

From Dev

What's the purpose of copy relocation?

From Dev

What's the purpose of glVertexPointer?

From Dev

In html what's the purpose of using 'step' attribute in input tag?

From Dev

What's the purpose of QString?

From Dev

What is the "|>" symbol's purpose in Elixir?

From Dev

What's the purpose of Shadowing?

From Dev

When using a JsonWriter, what's the purpose of WriteStartConstructor?

From Dev

What's the purpose of Kubernetes ServiceAccount

From Dev

What's the purpose of "true" in bash "if sudo true; then"

From Dev

what's the purpose of factories?

From Dev

What's wrong with using sudo?

From Dev

What is the purpose/effect of |\s? in ([\s\S]+|\s?)

From Dev

what is the purpose of cp -s?

From Dev

In html what's the purpose of using 'step' attribute in input tag?

From Dev

What's the purpose of QString?

From Dev

What's the purpose of Shadowing?

From Dev

what's the different with using $scope and explicitly using ctrl as namespace?

From Dev

What's the purpose or point of using modules?

From Dev

What's the purpose of using JQuery toggle() within filter()?