8

I can see the difference between /dev/tty and /dev/tty0 by testing the provided method from this question. But I really wonder about the practical usage of those devices (like situations they will be used).

  • @0xC0000022L Somehow, I cannot relate tty with sda because unlike sda, tty has 3 different categories (tty, tty0, tty[1, .., N]). – Ron Vince Sep 14 '15 at 7:47
  • I think the analogy provided by @0xC0000022L is quite misleading. Both /dev/sda and /dev/sda1 designate specific block devices (one happens to be a portion of the other one). /dev/tty, on the other hand, is a virtual device related to no specific other tty. – Celada Sep 15 '15 at 12:55
15

/dev/tty is the controlling tty of the current process, for any process that actually opens this special file. It isn’t necessarily a virtual console device (/dev/ttyn), and can be a , a serial port, etc. If the controlling tty isn’t a virtual console, then the process has not to interact with console devices even if its pseudotty is actually implemented on the system console. E. g. for a shell in a terminal emulator under locally-running X server, said programs form such chain of interactions as:

   Unix shell
     ⇕ /dev/pts/2 (≡ /dev/tty for its processes)
 kernel pty driver
     ⇕ /dev/ptmx
 terminal emulator
     ⇕ X Window protocol
   X server
     ⇕ /dev/tty7 (≡ /dev/tty for the server)
 system console
zxc↿⇂[_̈░░]
    user

Use of /dev/tty by userland programs includes:

  • Write something to the controlling terminal, ignoring all redirections and pipes;
  • Make an ioctl() – see tty_ioctl(4);
  • For example, detach from the terminal (TIOCNOTTY).

/dev/tty0 is the currently active (i. e. visible on the monitor) virtual console of the operating system. This special file unlikely is used significantly by system software, but /dev/console is virtually an “alias” for tty0 and /dev/console has much use by syslog daemons and, sometimes, by the kernel itself.

Experiment to show the difference: run a root shell on tty3 (Ctrl+Alt+F3) or in a terminal emulator. Now

# sleep 2; echo test >/dev/tty

then quickly Ctrl+Alt+F2, wait for two seconds, and Ctrl+Alt+whatever back. Where do you see the output?
And now the same test for /dev/tty0.

  • Your answer has no difference with the answer provided by the question that I linked in my question. What I concern about is the process of how kernel uses them together. – Ron Vince Sep 14 '15 at 21:26
  • @Ron Vince: updated. – Incnis Mrsi Sep 14 '15 at 22:51
  • Is it mandatory for a process to write to /dev/tty instead of their specific /dev/tty[1, .., N]? – Ron Vince Sep 15 '15 at 4:49
  • @Ron Vince: Again, a process executed within a SSH session (technically, a child of sshd), or in a xterm window, hasn’t its specific /dev/tty n. It doesn’t interact with devices of the system console, because it’s attached to a pseudo-terminal device. This does not preclude it from opening /dev/tty n if it’s a root process, or the same user in logged on tty n. About to add some points about “specific tty” to the answer. – Incnis Mrsi Sep 15 '15 at 12:39
  • Let's say I use xterm which is a terminal emulator. Is this the chain (xterm --> /dev/tty --> /dev/pty --> /dev/tty[1, .., N] --> shell)? For /dev/tty0, I think I'm clear about it. Output from any processes and kernel will be sent to the /dev/tty[1, .., N] that tty device driver handle now. This is the chain (kernel/process --> /dev/tty0 --> current /dev/tty[1, .., N]) – Ron Vince Sep 16 '15 at 15:23
2

/dev/tty is the controlling tty for any process. This may be your shell.

If your process does not have a controlling tty, /dev/tty is not available, this e.g. is true for daemons.

If your process has a controlling tty, then /dev/tty is a driver alias to the real tty driver your process is using for stdin, stdout or stderr.

See man -s7d tty for more information. Note that the section where this manual is in may differ between different OS and should not be confused with the man page in section 1. So check e.g. man -k tty.

  • "alias to the real tty driver your process is using". Is the quoted text refers to tty[1, .., N]? – Ron Vince Sep 14 '15 at 8:25
  • Driver alias is something different than just alias. It is a different driver but it directs work to whatever your process is really connected to. – schily Sep 14 '15 at 8:31
  • If I understand correctly (also refer to tldp.org/HOWTO/Text-Terminal-HOWTO-7.html#ss7.3), /dev/tty is the interface for the current process (currently processed by CPU) to /dev/tty[1, .., N]. Meanwhile, /dev/tty0 is to output data that do not belong to specific /dev/tty[1, .., N], therefore output to /dev/tty[1, .., N] that belongs to the current process. If those are correct, I wonder when will the /dev/tty0 is actually used. – Ron Vince Sep 14 '15 at 8:56
  • If you don't have an own house, a possible alternate entry into that house would not give you a house. – schily Sep 14 '15 at 9:10
  • So, /dev/tty0 is to output data to superuser current shell session (associated to /dev/tty[1, .., N]). – Ron Vince Sep 14 '15 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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