pty
acronymKept in this browser · My desk
Stands for: pseudo-terminal
A pseudo-terminal: a pair of virtual devices whose slave end behaves exactly like a terminal and whose master end a program drives, so that what the master writes reaches the other side as if typed.
It is how Expect talks to programs written for a person at a keyboard: spawn starts each one on a pty set up like the user's terminal, so passwords, prompts, echo and line editing behave as they would interactively (pty(7); expect(1), spawn).
What it is, and what it is not. The Linux manual describes a pseudoterminal, usually shortened to pty, as a pair of virtual character devices forming a bidirectional channel. One end is the master, the other the slave. The slave presents exactly the interface of a classical terminal, so a program that expects a terminal can open it and be driven by whatever holds the master: anything written to the master arrives at the slave as typed input, and anything the slave's program writes can be read at the master. Even the special keys work, because the terminal layer interprets them: writing the interrupt character, usually control-C, to the master raises SIGINT in the foreground process group on the slave. Data flows asynchronously, as it would on a physical line, so there can be a small delay between a write on one side and its effect on the other. A pty is not a shell (the shell is just a program that may run on the slave), not a terminal emulator (the emulator is the program on the master side that draws characters), and not a serial line, although it imitates one.
Why programs behave differently on one. The value of a pty is the terminal discipline it brings, set through the termios interface. In canonical mode, the default, input reaches the program a line at a time, after the user has had the chance to edit it with the erase and kill characters; ECHO controls whether typed characters are echoed, which is how a password prompt hides what is typed; ISIG turns the interrupt, quit and suspend characters into signals; and the output flag ONLCR maps a newline to carriage return plus newline, which is why text captured through a pty ends each line in a carriage return and a line feed. Programs also look at whether their input is a terminal and change behaviour: they prompt, they page, they colour, they buffer differently. The Python documentation says of pty.spawn that it is often used to baffle programs which insist on reading from the controlling terminal, and that is the whole reason Expect exists: spawn starts each program on a pty initialized like the user's terminal, so prompts, echo and line editing behave as in an interactive session.
Two interfaces, and how Linux allocates them. Historically there were two pseudoterminal interfaces, from BSD (Berkeley Software Distribution) Unix and from System V. BSD ptys were precreated pairs named /dev/ptyXY for the master and /dev/ttyXY for the slave, X from a 16-letter set such as p to z and a to e and Y a hexadecimal digit, so /dev/ptyp1 pairs with /dev/ttyp1, and a program found a free one by trying to open masters until one succeeded. The first version of the Single UNIX Specification standardized an interface based on System V, which Linux calls UNIX 98 pseudoterminals; BSD-style ptys have been deprecated since Linux 2.6.4, and the option that provides them, CONFIG_LEGACY_PTYS, has been off by default in the mainline kernel since 2.6.30. The modern sequence is posix_openpt, which opens the master clone device /dev/ptmx (character device major 5, minor 2, normally mode 0666 and owned by root), then grantpt and unlockpt on the master, then ptsname to learn the slave's name and open to open it; each open of /dev/ptmx yields an independent master whose slave appears as a numbered file under /dev/pts. The BSD helpers openpty, login_tty and forkpty wrap the same steps, forkpty starting a child whose controlling terminal is the new slave. The number of ptys is limited: up to Linux 2.6.3 at compile time, with a maximum of 2048 and a default of 256; since 2.6.4 through /proc/sys/kernel/pty/max, with /proc/sys/kernel/pty/nr counting those in use. Since kernel 3.4 every mount of the devpts filesystem keeps its own numbering, which is what lets each container have its own /dev/pts, with a global default of kernel.pty.max = 4096 and kernel.pty.reserve = 1024 kept for the initial mount namespace.
Where engineers meet it. The manual lists the users: remote login services such as ssh, rlogin and telnet, terminal emulators such as xterm, and script, screen, tmux, unbuffer and expect. For sshd the master side is the network: data read from the master is sent to a client that has a real terminal or an emulator. The Secure Shell (SSH) connection protocol, Request for Comments (RFC) 4254 of January 2006, makes the allocation an explicit request: a pty-req message on a session channel carries the TERM value (vt100 in the example), the width and height in characters (80 and 24 in the examples) and in pixels, and the encoded terminal modes, and a later window-change message reports a resized window. The OpenSSH client asks for a pty for interactive sessions only when it has a terminal itself; -t forces one, which screen-based programs need, -T disables it, and without a pty the session is transparent and safe for binary data, while the escape characters work only with one. On the server, sshd_config's PermitTTY, yes by default, decides whether pty allocation is allowed at all. Containers repeat the choice: docker container run -t allocates a pseudo-terminal. And script makes a typescript of a terminal session, with timing data that scriptreplay can play back.
In network automation. Most command-line automation of routers, switches and firewalls is pty work. Paramiko's get_pty requests a pseudo-terminal, by default a vt100 of 80 by 24, usually right before invoke_shell opens an interactive shell; its documentation says it is neither necessary nor desirable for a single command run with exec_command. That split explains a familiar set of symptoms. On an interactive channel with a pty, a device behaves as it does for a person: it echoes each command, ends lines with carriage return and line feed, and pages long output at the terminal height until something answers the more prompt, which is why scripts send a command such as terminal length 0 first. On an exec channel without a pty, many devices run the command as a batch and return plain output, and some refuse. Netmiko and Expect are, at bottom, machinery for driving the first case reliably.
What goes wrong: sharing a terminal. A terminal is a channel of authority, and a pty is how privileged sessions avoid sharing one. The TIOCSTI ioctl inserts a byte into a terminal's input queue as if typed. If a command running as another user shares the terminal of the session that started it, as su and sudo historically did, a malicious program can use it to push characters into the parent shell's input, to be executed with the original user's rights once the program exits. The util-linux su manual says plainly that su sharing a terminal with the original session is potentially vulnerable to privilege escalation through TIOCSTI or TIOCLINUX command injection, and offers two defences: su -c, which starts a new session with no controlling terminal, or --pty, which gives the session its own pseudoterminal. The sudoers manual describes the same attack, a program injecting commands into the user's terminal or keeping a background process with access to it after exiting, and the use_pty flag that prevents it by running the command in a new pseudo-terminal; it has been on by default since sudo 1.9.14. The kernel closed the gap from its side as well: since Linux 6.2, TIOCSTI may require the CAP_SYS_ADMIN capability when the dev.tty.legacy_tiocsti sysctl is set to false. Recording has its own edge: script has been able to log input as well as output since version 2.35, and an input log records keystrokes that echo hid, passwords included.
Numbers worth keeping. /dev/ptmx is character device 5,2; a default of 4096 ptys system-wide, 1024 of them reserved for the initial namespace; a classic terminal request of vt100 at 80 by 24; BSD ptys deprecated since Linux 2.6.4; use_pty on by default from sudo 1.9.14; TIOCSTI restricted from Linux 6.2.
Also known as: pseudo-terminal, pseudoterminal, pseudo terminal, pty master, pty slave
Sources
- pty(7), Linux man-pages 6.19: a pair of virtual character devices; the slave end behaves exactly like a classical terminal and a program drives it from the master end (read 2026-10-27)
- expect(1), release 5.45.4: spawn uses a pty, initialized the same way as the user's tty and then made sane (read 2026-10-27)
- pty(7), Linux man-pages 6.19, re-read in full (read 2026-11-12): control-C written to the master raises SIGINT for the slave's foreground process group; asynchronous data flow; BSD and System V APIs, SUSv1 standardizing the System V one; UNIX 98 pseudoterminals; BSD-style ptys deprecated since Linux 2.6.4 and CONFIG_LEGACY_PTYS off by default since 2.6.30; the limit of 2048 with a default of 256 up to 2.6.3, then /proc/sys/kernel/pty/max and nr; /dev/ptyXY and /dev/ttyXY names; users such as ssh, telnet, xterm, script, screen, tmux and expect
- pts(4), Linux man-pages (read 2026-11-12): /dev/ptmx, the pseudoterminal multiplexer, character device major 5 minor 2, mode 0666, root:root; each open gives an independent master and creates a slave in /dev/pts; grantpt and unlockpt before opening the slave; terminal emulators and sshd as the main users
- posix_openpt(3), Linux man-pages (read 2026-11-12): opens an unused pseudoterminal master; flags O_RDWR and O_NOCTTY
- openpty(3), Linux man-pages (read 2026-11-12): openpty returns master and slave descriptors and can set terminal attributes and window size; login_tty makes a descriptor the controlling terminal of a new session; forkpty combines openpty, fork and login_tty
- Linux kernel documentation, Devpts Filesystem (read 2026-11-12): each devpts mount is distinct, with its own pty indices; /dev/pts/ptmx; kernel.pty.max = 4096 globally and kernel.pty.reserve = 1024 for the initial mount namespace; the per-instance max mount option, added in kernel 3.4
- termios(3), Linux man-pages (read 2026-11-12): canonical mode, with input made available line by line and line editing; ECHO; ISIG generating signals from INTR, QUIT and SUSP; ONLCR mapping NL to CR-NL on output
- TIOCSTI(2const), Linux man-pages 6.19 (read 2026-11-12): inserts the given byte in the terminal's input queue; since Linux 6.2 it may require CAP_SYS_ADMIN when the dev.tty.legacy_tiocsti sysctl is false
- su(1), util-linux (read 2026-11-12): --pty creates a pseudoterminal for the session; a su sharing a terminal with the original session is potentially vulnerable to privilege escalation through TIOCSTI or TIOCLINUX command injection; -c starts a new session without a controlling terminal
- sudoers(5), sudo project (read 2026-11-12): use_pty runs the command in a new pseudo-terminal so that a malicious program cannot inject commands into the user's terminal or keep access to it; on by default from sudo 1.9.14
- IETF, RFC 4254, The Secure Shell (SSH) Connection Protocol, January 2006, Standards Track (read 2026-11-12): section 6.2, the pty-req channel request with the TERM value, width and height in characters and pixels and the encoded terminal modes; section 6.7, window-change
- OpenBSD manual, ssh(1) (read 2026-11-12): ssh requests a pseudo-terminal for interactive sessions only when the client has one; -t forces allocation and -T disables it; without a pty the session is transparent for binary data
- OpenBSD manual, sshd_config(5) (read 2026-11-12): PermitTTY specifies whether pty allocation is permitted; the default is yes
- Docker documentation, docker container run (read 2026-11-12): -t, --tty, allocate a pseudo-TTY
- Python documentation, pty, Pseudo-terminal utilities (read 2026-11-12): pty.fork and pty.spawn; spawn is often used to baffle programs which insist on reading from the controlling terminal; Unix only and platform dependent
- Paramiko documentation, Channel (read 2026-11-12): get_pty(term='vt100', width=80, height=24) requests a pseudo-terminal, usually before invoke_shell, and is neither necessary nor desirable for exec_command
- script(1), util-linux (read 2026-11-12): makes a typescript of a terminal session in raw form with optional timing for scriptreplay; since version 2.35, input and output can be logged