Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

Apologies if this is an abstract question - I'll try to be as specific as possible.

When I'm at the bash shell and switch to a different account via su - foo, I'm prompted for a password. The characters I type at this password prompt are hidden from the screen with no indication of how many characters I'm typing or what they are. How is bash (or Linux in general) doing this?

share|improve this question

marked as duplicate by Gilles, Scott, don_crissti, Jeff Schaller, slm Mar 18 at 0:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Who says bash does this job? Obviously, it is su doing this... – glglgl Mar 17 at 19:39
Good point. I edited the question title. – Mike B Mar 17 at 22:15
up vote 8 down vote accepted

What you type is displayed in the terminal because the terminal "echoes" it back. When asking for password, the echoing is turned off. See also help read and its -s option.

share|improve this answer

I believed that su probably opened /dev/tty, changed the terminal driver settings to not echo, and then read from /dev/tty's file descriptor.

To test this belief, I ran strace -o su.out su - on my Arch linux laptop. The relevant part of the strace output:

ioctl(0, SNDCTL_TMR_CONTINUE or TCSETSF, {B38400 opost isig icanon -echo ...}) = 0
write(2, "Password: ", 10)              = 10
read(0, "hahanotthis\n", 511)                = 7
ioctl(0, SNDCTL_TMR_STOP or TCSETSW, {B38400 opost isig icanon echo ...}) = 0

I was not 100% wrong. su does in fact read from stdin, but it does turn off echoing on stdin using a terminal control ioctl(). After I put in the password, su turns echoing back on, again with ioctl() system call.

I know that some other programs, the ftp client in particular, do use /dev/tty to read passwords, which means you can't put a password on the command line, or in a "here document", you have to use some shenanigans.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.