The meaning of TTY
TTY stands for 'Teletypewriter' (or 'Teleprinter'), which is a physical device used to send and receive typed messages from a communication channel.
Back in the day teletypewriters were mainly used to communicate with a computer to write stuff on a piece of paper or for dialing the phone.

In computing, however, TTY does not refer to this physical device; because they serve a similar purpose - receiving and sending textual messages, they both share the same name.
What is TTY?
TTY is used as an interface for the user to communicate with the computer's operating system.
In Linux, you can use the tty command in order to get the current tty file your terminal uses to receive user input and display them.
$ tty
/dev/tty1 # An example of a possible output
π Note:
You may get two kinds of outputs/dev/pts/Xor/dev/ttyX(where X is a non negative number).
Why there are two kinds of files and what is the difference? We will
uncover that shortly.
What is /dev/tty1?
Every /dev/ttyX file is a special file which acts as a buffer containing input data. It is displayed as a simple file on the file system but its actually a special kind of file named Character Device - a file used as an interface for a device, such as printers, headphones, serial ports and in our case, terminal devices used to display information.
What happens when I enter some text
Let's say you open bash and start typing something.
In this example we assume that stdin and stdout are attached to a terminal session, for example - /dev/tty1.
-
Input flow: The written text is caught by the OS and passed to the
terminal driver(a part in the OS which handles input and output) which then writes the input data to the TTY file -/dev/tty1. -
Output flow: Bash reads the data from its
stdinfile, that is the tty file -/dev/tty1, and sends the corresponding data tostdout(which is also/dev/tty1) where the OS and the terminal driver process it to showcase the information on the screen.
Input Flow - Reading typed data
[User Input] ---> [OS & Terminal Driver] ---> [/dev/tty1] ---> [Bash Shell (stdin)]
Output Flow - Displaying the result
[Bash Shell (stdout)] ---> [/dev/tty1] ---> [OS & Terminal Driver] ---> [Physical Terminal]
π Note:
If you were to check what bash writes to, you'll sometimes see that it writes to/dev/ttyand not to a specific tty file like '/dev/tty1'. How is that?
Well,/dev/ttyrefers to the controlling terminal of the current process, thats why the output is still displayed to the same terminal.
Pseudoterminals
Aside from tty, there is another way to achieve a similar IO channel, called "Pseudoterminals" ('pty' in short).
Pseudoterminals are built from two 'virtual devices', a pseudoterminal master and a pseudoterminal slave.
The pseudoterminal slave and master act as a bidirectional pipe - data can be transferred in either direction.
Interestingly, the pseudoterminal slaves appears just like a standard terminal and provides an identical interface to a physical terminal.
What is the difference between TTY and PTY?
The core difference between pty and tty is that pty achieves IO communication with another software, where tty achieves IO communication with a connected hardware.
PTY may be created by any program at any time while tty is created by the operating system where a new hardware is connected.
Where is PTY used?
Pseudoterminals are mainly used for two purposes:
- Help operate terminal-oriented programs (like vi, ssh, tmux etc..) on another host.
- As a way to grant duplex communication between a pair of processes (We will focus less on this).
As a simple example, lets take sshd (ssh server). When a user remotely connects to a ssh server running on another host, it forks and creates a new pseudoterminal slave for this specific session. That pseudoterminal slave acts just like a terminal - getting user input through the internet and sending back corresponding data.
A simple diagram to visualize it:
[Remote Client] ---> (Network) ---> [sshd (Master)] <---> [/dev/pts/X (Slave)] <---> [Bash Shell]
Pseudoterminals slaves will appear under /dev/pts/ in your linux filesystem (pts stands for 'pseudoterminal slave'). So by running the tty linux commnand you may see a physical terminal or a pseudoterminal like the following:
In conclusion, Both tty and pty grant an interface for duplex communication. TTY is created automatically by the OS and connects to a connected hardware where PTY may be created by any program to achieve communication with another software.
This article hopes to make the difference more clear and user friendly, those who seek to learn the subject more deeply, may read chapters 62 and 64 on the great book 'The Linux Programming Interface by Michael Kerrisk'.
Thank you for reading!

Top comments (0)