Linux – How /dev/tty and /dev/tty0 Are Used

deviceslinuxterminaltty

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

Best Answer

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