Linux Terminals & Pseudo Terminals


Terminal Devices (TTY) & Serial Devices (TTYS)

Terminal devices were historically just dumb terminals that could accept input and send it to a server, and then display the resonse. They basicall move characters to and from user space to a physical I/O device.

Serial devices can be found under the /dev folder. They are normally called something like /dev/ttyUSB* (USB to serial adaptors) or /dev/ttyS* etc. They correspond directly to a bit of hardware. Writing to the serial device's file will send that data out on the wire (RS232), and data received on the wire can be read from the device.

Why is the string "tty" used for serial devices? TTY is an abbreviation for "teletype" Its a historical thing where terminals where usually dumb - almost like typewriters. They just communicated to a server by sending the user input to the server and displaying the server's output (text only of course) - they moved text from user space to and from the serial device. Because serial was historically used by terminal devices, the serial devices are still refered to as TTY or terminal devices even though they are not usuall connected to terminals these days.

So this is why derial devices are called terminal devices and are generally refered to as TTYS<num>, the S indicating it is a serial terminal (because not all terminals need to use serial).

Pseudo Terminals

Can be explained pretty well just from the man page (emphasis mine) ;)

A pseudoterminal (sometimes abbreviated "pty") is a pair of virtual character devices that provide a bidirectional communication channel. One end of the channel is called the master; the other end is called the slave. The slave end of the pseudoterminal provides an interface that behaves exactly like a classical terminal. A process that expects to be connected to a terminal, can open the slave end of a pseudoterminal and then be driven by a program that has opened the master end. Anything that is written on the master end is provided to the process on the slave end as though it was input typed on a terminal. For example, writing the interrupt character (usually control-C) to the master device would cause an interrupt signal (SIGINT) to be generated for the foreground process group that is connected to the slave. Conversely, anything that is written to the slave end of the pseudoterminal can be read by the process that is connected to the master end. Pseudoterminals are used by applications such as network login services (ssh(1), rlogin(1), telnet(1)), terminal emulators, script(1), screen(1), and expect(1).

-- Linux Man Page - pty(7)

Take the following example:

As bash is forked, it is connected to the slave end of the PTY pair that the sshd controls. As bash displays the prompt, it outputs serial to the PTY slave end, which is output from the master end, read by sshd and sent to the user over the network. As the user replies, the replies are written by sshd to the master end of the PTY pair, which appears to the bash processes as input on its terminal (or what it thinks is a terminal - it doesn't know that it is a pseudo device). And so the process continues, with bash thinking it is reading and writing to a terminal device of some kind, when, in fact, it is being controlled by the sshd process, which is sending and receiving input from a remote user over the LAN or WAN.

The block in the middle is termios [Ref]. Termios is a general terminal interface that is provided to control asynchronous communications ports.