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).

Ron Vince
  • 1,442
  • @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 07: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

2 Answers2

18

/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.

Incnis Mrsi
  • 1,988
  • 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 04: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
  • @Ron Vince: A diagram for a local X terminal emulator case drawn. Although is has little to do with initial question. – Incnis Mrsi Sep 16 '15 at 17:44
  • I really like your answer! Hope to see you continue to contribute here. – Tim Nov 30 '18 at 03:54
  • /dev/tty0 is permission denied , dude… – dotbit Dec 11 '19 at 22:33
  • @dotbit: denied to whom, to root (uid=0)? What does the id command report? – Incnis Mrsi Jan 21 '20 at 17:57
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.

schily
  • 19,173
  • "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 08: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 08:31
  • If I understand correctly (also refer to http://www.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 08: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 09: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 09:11
  • What do you mean by that? I do not understand. – Ron Vince Sep 14 '15 at 09:12
  • If I tell you "look for the key on the keyboard", what do you do if there is no key on the keyboard? – schily Sep 14 '15 at 09:17
  • Ask you "Are you sure that is keyboard?". – Ron Vince Sep 14 '15 at 09:22
  • Mistake. /dev/tty0 is not to output data to superuser current shell session (associated to /dev/tty[1, .., N]) but any current shell session. Now, I wonder how /dev/tty0 works for shell session that login remotely. Is the current remote shell session will receive the messages as the current local shell session? – Ron Vince Sep 14 '15 at 09:42
  • @Ron Vince: /dev/tty0 opens to the currently active virtual console. No shell session (or any process at all) is required to run on that console. On a pure server box without any keyboard input (but configured to use VCs, not alternative implementations of the system console) the destination will likely be tty1. – Incnis Mrsi Jan 21 '20 at 18:21