If I do:
sudo cat /etc/resolv.conf | less
It will prompt me for the password, even though less (presumably) takes stdin. Over what fd's is the password prompt shown and how does it get the input back?
Server Fault is a question and answer site for system and network administrators. It only takes a minute to sign up.Sign up to join this community
Actually, a typical invocation of
sudo does not read the password from
stdin at all. Instead,
sudo will directly access the controlling terminal (a
pty, via the
/dev/tty special file) and output the prompt and read characters directly. This can be seen in the
tgetpass.c file in the
There are a few other scenarios:
askpassprogram is specified, e.g. in the
-Aparam, that program will be invoked.
sudoto read from
stdin, e.g. with the
-Sflag -- and it will also write the prompt to
stderr. This is the case where MadHatter's answer applies.
sudowill report an error:
no tty present and no askpass program specified
sudowill fall back to using
stderreven if it was not specifically requested. MadHatter's answer will also apply here.
The pipe connects
sudo cat's stdout to
less's stdin, so
sudo cat's stdin is unaffected, and able to receive the password.
As for the prompt, it goes out on
sudo cat's stderr; in bash, try redirecting that along with stdout, using
sudo cat /etc/resolv.conf |& less
and see how different the response is.