Pseudoterminal
The kernel device pair that lets a terminal window, SSH session, or tmux pane pretend to be a 1970s hardware terminal, and why programs behave differently when they are not attached to one.
A pseudoterminal, or PTY, is a pair of kernel devices that imitates a physical terminal. One end, the controller, is held by a terminal emulator, Secure Shell server, or multiplexer such as tmux. The other end, the subsidiary, appears to the programs running inside as an ordinary terminal device such as /dev/pts/3. Everything typed into the window goes into one end and comes out the other as if from a keyboard; everything the program prints travels back to be drawn.
The design exists because Unix grew up with real terminals. A teletype, the "TTY" in the name, was a typewriter connected to the computer by a serial line, later replaced by video terminals such as the DEC VT100 of 1978. The kernel's terminal driver handled those lines, and programs were written against it. When terminals became windows on a graphical desktop, the kernel kept the same interface and let software play the hardware's role.
Between the two ends sits the line discipline, which provides behavior people assume is part of the shell:
- Cooked mode. Input is buffered until Enter, and Backspace edits the line before the program ever sees it.
- Special keys. Ctrl-C sends SIGINT, Ctrl-Z sends SIGTSTP to suspend, and Ctrl-D marks end of input. The first two are delivered by the terminal driver as signals to the foreground process group.
- Echo. Typed characters are printed back, except at password prompts that turn echo off.
- Raw mode. Editors, full-screen tools, and any Terminal User Interface switch to raw mode to receive every keystroke immediately and draw the whole screen themselves.
Programs control the screen with escape sequences, byte strings inherited from the VT100 and standardized as ANSI codes, which move the cursor, set colors, and clear lines. Modern terminal emulators add extensions such as true color, hyperlinks, and inline images. When a window is resized, the kernel updates the PTY's size and sends SIGWINCH so programs can redraw.
Programs often check whether they are attached to a terminal using isatty. Many behave differently when they are not: ls prints one name per line, grep --color=auto drops color, and the C library switches standard output from line buffering to block buffering. That is why output piped into another command, as in a Unix Pipeline, can arrive in bursts, and why tools that run interactive programs automatically, such as expect or a test harness, must allocate a PTY to see normal behavior.
Terminals also carry sessions. Closing a terminal sends SIGHUP to its processes, which is why long jobs started in a terminal die when it closes unless run under nohup, tmux, or as a Daemon. Each Process has a controlling terminal, and the shell's job control uses it to move jobs between foreground and background.
See also1
Linked from11
- ANSI Escape CodeSystems & Tooling
The in-band byte sequences, starting with the escape character, that tell a terminal to move the cursor, change color, or clear the screen, inherited from 1970s video terminals and still how every command-line tool draws.
- Command-Line InterfaceSystems & Tooling
Operating a computer by typing commands and reading text replies, the interface inherited from teletypes that remains the fastest, most scriptable, and least discoverable way to control software.
- DaemonSystems & Tooling
A long-running background process managed by the operating system's service supervisor.
- Device FileSystems & Tooling
Special files that represent hardware and kernel services, letting programs read a disk, a mouse, or a stream of random bytes with the same calls they use on ordinary files.
- Line EndingSystems & Tooling
The invisible characters marking where a line of text ends, split three ways since the teletype era between Unix, Windows, and classic Mac OS, and still breaking scripts and diffs.
- Remote DesktopNetworks & Distribution
Controlling a computer from somewhere else by streaming its screen to a distant machine and sending keyboard and mouse input back, used for IT support, remote work and cloud gaming, and a favorite entry point for attackers when left exposed.
- Secure ShellSystems & Tooling
The encrypted protocol for logging into and running commands on remote machines, authenticated by key pairs, that also tunnels other traffic and underpins Git.
- Serial PortSystems & Tooling
The old, simple connection that sends data one bit at a time, inherited from teleprinters and modems, gone from consumer computers but still the last-resort console for servers, network gear, and embedded boards.
- ShellSystems & Tooling
The interactive command interpreter, and a programming language with unusually sharp edges.
- Terminal MultiplexerSystems & Tooling
A program that runs many terminal sessions inside one window, splits it into panes, and keeps the sessions alive on a server after the connection drops, so long jobs and remote work survive disconnection.
- Terminal User InterfaceGraphics & Rendering
An interface rendered as text in a terminal, and the design conventions that make command-line tools humane.