3

Running the following command in Bash 4, sudo prompts for a password:

$ echo '' | sudo true
[sudo] password for mike:

(I'm not interested in piping my password to sudo; this is a demonstration of a behavior I found while troubleshooting.)

It's been my understanding that TTY detection takes place through inspection of the file descriptor for standard in. The tty program supports this:

$ tty
/dev/pts/26
$ echo '' | tty
not a tty

...but sudo seems to be using some other mechanism. setsid has the intended effect, so my guess is that this is somehow tied to the session.

$ setsid sudo true
sudo: no tty present and no askpass program specified

So what is sudo up to?

2
  • 1
    sudo doesn't read passwords from stdin unless you add -S -- The -S (stdin) option causes sudo to read the password from the standard input instead of the terminal device. The password must be followed by a newline character.
    – Jeff Schaller
    Commented Jun 19, 2018 at 20:17
  • 1
    relatedly, sudo opens the TTY directly, if it can. trace pathnames.h _PATH_TTY through to src/tgetpass.c's tgetpass() function, which calls tty_present() which tries to open _PATH_TTY and checks for success.
    – Jeff Schaller
    Commented Jun 19, 2018 at 20:26

1 Answer 1

2

It opens /dev/tty (as mentioned by Jeff Schaller, using the _PATH_TTY macro), which provides access to the controlling terminal, whichever it is, if there is one. See what relations are between my current controlling terminal and `/dev/tty`? for details.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .