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?
file-descriptorslinuxsudoterminal
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?
Best Answer
Actually, a typical invocation of
sudo
does not read the password fromstdin
at all. Instead,sudo
will directly access the controlling terminal (atty
orpty
, via the/dev/tty
special file) and output the prompt and read characters directly. This can be seen in thetgetpass.c
file in thesudo
source.There are a few other scenarios:
askpass
program is specified, e.g. in the-A
param, that program will be invoked.sudo
to read fromstdin
, e.g. with the-S
flag -- and it will also write the prompt tostderr
. This is the case where MadHatter's answer applies.tty
availablevisiblepw
flag insudoers
),sudo
will report an error:no tty present and no askpass program specified
sudo
will fall back to usingstdin
andstderr
even if it was not specifically requested. MadHatter's answer will also apply here.