How Linux uses /dev/tty and /dev/tty0
/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/tty
n), and can be a pty, 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
.
/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
.