What the purpose of /dev/tty?

See: https://stackoverflow.com/questions/6170598/can-anyone-explain-to-me-what-the-purpose-of-dev-tty


You can start with the POSIX spec. From there, read about the "controlling terminal" of a process.

But just for example... /dev/tty is how a command like "ssh" can read your password even if its standard input comes from somewhere else:

tar cf - . | ssh dest 'tar xf -'

Open in new window


If ssh decides to prompt you for a password, it will read it from /dev/tty instead of stdin.

Conceptually, /dev/tty is "the keyboard and text terminal". More or less.

Let's say my "terminal-file" of the current session is /dev/pts/1. In such a case, then what's the difference between "/dev/pts/1" and "/dev/tty"? And if they are basically the same, then why  "/dev/tty" is used instead of "/dev/pts/1"?

And:

/dev/tty is how a command like "ssh" can read your password even if its standard input comes from somewhere else

Let's say the standard input comes from somewhere else, so let's say we have:

FD 0 <- file
FD 1 -> /dev/pts/1
FD 2 -> /dev/pts/1

How I see it: the fact that the standard input comes from somewhere else doesn't mean that /dev/pts/1 can not be read? The password comes from the keyboard and /dev/pts/1 represents i.a. the keyboard, right? So I still don't see what exactly the purpose is of /dev/tty?

@noci: I know you know the answer, but I don't understand your explanation so I've made this post so maybe other people can explain it to me in a way that I understand it.
Maarten BruinsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
You can maybe just think of /dev/tty being like a link to whatever the actual terminal device is for that particular session, be it a /dev/pts/1 type of terminal, or /dev/tty1 or /dev/ttyS0, etc, etc.

So you next question then was...

Let's say the standard input comes from somewhere else, so let's say we have:

FD 0 <- file
FD 1 -> /dev/pts/1
FD 2 -> /dev/pts/1

How I see it: the fact that the standard input comes from somewhere else doesn't mean that /dev/pts/1 can not be read?

True, but how do you know that the terminal to read is /dev/pts/1 and not say, /dev/pts/5 or something. The FD 0 was redirected to the file by the shell before the program started to run, so how do you know which terminal to try and read from? You don't, but you can just read from /dev/tty and it will always be whatever FD 0 was going to be before the redirection.
0
Maarten BruinsAuthor Commented:
Thanks a lot! That's clear to me.
0
Maarten BruinsAuthor Commented:
Now noci just said something to me in another thread and that confuses me again ;). Let's say the shell is bash. Then I start with a bash process:

FD 0 (standard input)   <- /dev/pts/1 (terminal-file/keyboard)
FD 1 (standard output)  -> /dev/pts/1 (terminal-file/keyboard)
FD 2 (standard error)   -> /dev/pts/1 (terminal-file/keyboard)

When "less" is seen by bash, these file descriptors will be forked first right? Now you're saying:

The FD 0 was redirected to the file by the shell before the program started to run,

How I have to see this, because if less starts with a fork of the bash-process then "/dev/pts/1" is known, right?
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
less starts with a fork
No, it doesn't. The fork happens first, but then it is still bash code that is running in the child process. So then after the fork, bash sets up the redirection, and then it calls "exec" to load and run the less code.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Maarten BruinsAuthor Commented:
Thanks a lot! Sounds all logical, I'll go back to noci with it and then later on I'll close this question. What you're saying, totally makes sense to me. I still have to convince noci ;).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.