programmingnetworking

Kept 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

← Back to P Back to the glossary