What do pty and tty mean?
Asked Answered
W

5

312

I noticed many mentions of pty and tty in some open source projects, could someone tell me what do they mean and what is the difference between them?

Wage answered 13/12, 2010 at 6:19 Comment(2)
One might want to read the pty(7) man page (like in this link), where pty/ttys are described as pre-created pairs of files representing master/slave end of a pseudo-terminal (see the description about BSD style).Detonate
In the past I wrote an article about the PTY in the Linux world : rkoucha.fr/tech_corner/pty_pdip.htmlDerron
E
286

tty originally meant "teletype" and "pty" means "pseudo-teletype".

In UNIX, /dev/tty* is any device that acts like a "teletype", i.e: a terminal. (Called teletype because that's what we had for terminals in those benighted days.)

A pty is a pseudotty, a device entry that acts like a terminal to the process reading and writing there, but is managed by something else. They first appeared (as I recall) for X Window and screen and the like, where you needed something that acted like a terminal but could be used from another program.

Educator answered 13/12, 2010 at 6:21 Comment(8)
I think ptys where added to Unix primarily to support Telnet, rsh and rlogin.Arose
@larsmans, we had telnet (at least) before there was a pty -- in fact, telnet and I both got into computing the same year, 1969. I was kinda off in the DECNET world when rsh showed up in UNIX, so I'm not sure of the oder there. In any case, that's part of "and the like".Educator
I know the Telnet protocol is older than ptys, but the present Unix Telnet program appeared in 4.2BSD from 1983; also the first Unix with ptys.Arose
yeah, but the telnet program doesn't use a pty. xterm does.Educator
@CharlieMartin What do you mean the pty is something "that acted like a terminal?" Why would we want something like that? And how does another program make use of this pty? My guess is that the pty relays commands or something to the for-real terminal (tty) for the program. Is this correct? If not, ignore my guess and please answer the first part of my comment.Lasley
Think about a terminal as an object: it connects something on one end to stdin and stdout on the other. A real TTY connects to a physical terminal. a PTY connect to a program, eg, xterm, or a command window, or a shell window. It then lies to the program and says it really is so a terminal, honest. Before PTYs you connected programs like this with pipes, but pipes have significant differences, like no flow control. PTYs appeared to solve this.Educator
@CharlieMartin "A real TTY connects to a physical terminal". What about the Linux Console? For example, in an Ubuntu system, there are Linux Consoles on Ctrl-Alt-F{1..6} and they are connected to /dev/tty{1..6}. The Linux Console is not a physical terminal, yet it is connected to a ttyN (not to a ptyN). Am I missing something here?Nonetheless
Only that you pretty much can't say anything definite about LINUX that won't have a weird edge case. In this case the Linux console is a kernel feature that uses a /dev/tty dev entry but then gets connected to a bunch of things to get out to the user. en.wikipedia.org/wiki/Linux_consoleEducator
F
277

A tty is a terminal (it stands for teletype - the original terminals used a line printer for output and a keyboard for input!). A terminal is a basically just a user interface device that uses text for input and output.

A pty is a pseudo-terminal - it's a software implementation that appears to the attached program like a terminal, but instead of communicating directly with a "real" terminal, it transfers the input and output to another program.

For example, when you ssh in to a machine and run ls, the ls command is sending its output to a pseudo-terminal, the other side of which is attached to the SSH daemon.

Footton answered 13/12, 2010 at 6:25 Comment(11)
Could you give a little bit more "precise" example of PTYs ? I still didn't understand where they come in, and how they even are called. ThanksAttempt
Can someone help on when and how *nix based operating system creates this pseudo terminals.Maloney
@darth_coder: They're created when an application requests one. That happens when you do things like open a new graphical terminal window or log in remotely.Footton
@Footton can you name kernel modules or daemon responsible for spawning a terminal.Maloney
@darth_coder: Any application can do so. sshd and xterm are two typical examples.Footton
@Footton - Is KDE's konsole considered to be a pseudo terminal? Based on your answer it is.Anamorphosis
@Motivated: Technically konsole is a program that operates the master side of a pseudo-terminal pair (the actual pseudo-terminal is a kernel object), but yes it definitely uses a pseudo-terminal.Footton
@Footton - It isn't clear to me. I have read posts such as (askubuntu.com/questions/331432/terminal-emulator-vs-terminal) however the responses differ. For example, is a text terminal the same as a console? If both text terminals and consoles are considered to be terminals (tty), do they also employ pseudo-terminals or are pseudo-terminals limited to just terminal emulators?Anamorphosis
The difference comes down to whether the kernel itself provides a driver for the terminal, or a userspace program implements it instead (which uses a pseudo-terminal as the kernel device). On Linux, "console" refers to the kernel driver which drives the normal video and keyboard as a terminal (so that does not use a pseudo-terminal). As mentioned, sshd also uses pseudo-terminals because it needs to provide local programs a terminal interface where the input and output goes to the other side of the network connection.Footton
@Footton - Do you mean to say a text terminal and console (which i assume are the same time) are provided by a driver in the kernel? If yes, unless i have misunderstood (askubuntu.com/questions/331432/terminal-emulator-vs-terminal/…), it suggests that the text terminal/console used an emulator as well. If yes, wouldn't it be a pseudo-terminal?Anamorphosis
@Footton - To clarify, is a terminal emulator such as KDE's konsole a pseudo terminal?Anamorphosis
R
26

tty: teletype. Usually refers to the serial ports of a computer, to which terminals were attached.

pty: pseudoteletype. Kernel provided pseudoserial port connected to programs emulating terminals, such as xterm, or screen.

Redpoll answered 13/12, 2010 at 6:23 Comment(0)
C
22

If you run the mount command with no command-line arguments, which displays the file systems mounted on your system, you’ll notice a line that looks something like this:

none on /dev/pts type devpts (rw,gid=5,mode=620)

This indicates that a special type of file system, devpts, is mounted at /dev/pts. This file system, which isn’t associated with any hardware device, is a “magic” file system that is created by the Linux kernel. It’s similar to the /proc file system

Like the /dev directory, /dev/pts contains entries corresponding to devices. But unlike /dev , which is an ordinary directory, /dev/pts is a special directory that is created dynamically by the Linux kernel. The contents of the directory vary with time and reflect the state of the running system. The entries in /dev/pts correspond to pseudo-terminals (or pseudo-TTYs, or PTYs).

Linux creates a PTY for every new terminal window you open and displays a corresponding entry in /dev/pts. The PTY device acts like a terminal device - it accepts input from the keyboard and displays text output from the programs that run in it. PTYs are numbered, and the PTY number is the name of the corresponding entry in /dev/pts.

For example, if the new terminal window’s PTY number is 7, invoke this command from another window:

echo ‘I am a virtual di ’ > /dev/pts/7

The output appears in the new terminal window. You can try to exchange the 7 for another number, and, depending on the numbers given to your open terminals, you will see the output on another terminal window. /dev/pts is the bus (the post office) to do this!

Copernicus answered 14/11, 2015 at 2:40 Comment(0)
S
21

A tty is a physical terminal-teletype port on a computer (usually a serial port).

A Teletype tty can also be emulated by a computer program running as a module in kernel space.

The word teletype is a shorting of the telegraph typewriter, or teletypewriter device from the 1930s - itself a replacement for the telegraph encoding machines of the 1830s and 1840s.

Teletypewriter
TTY - Teletypewriter 1930s

A pty is a pseudo-teletype port provided by a computer Operating System Kernel to connect user land terminal emulation software programs such as ssh, xterm, or screen.

enter image description here  PTY - PseudoTeletype

A terminal is simply a computer's user interface that uses text for input and output.


OS Implementations

These use pseudo-teletype ports however, their naming and implementations have diverged a little.

Linux mounts a special file system devpts on /dev (the 's' presumably standing for serial) that creates a corresponding entry in /dev/pts for every new terminal window you open, e.g. /dev/pts/0


macOS/FreeBSD also use the /dev file structure however, they use a numbered TTY naming convention ttys for every new terminal window you open e.g. /dev/ttys002


Microsoft Windows still has the concept of an LPT port for Line Printer Terminals within it's Command Shell for output to a printer.

Sestina answered 9/11, 2019 at 13:48 Comment(2)
The second image you provided is a VT100 video terminal. This is still a physical TTY (albeit that TTY manufacturers in the 1970s had moved on from printers to video displays), and definitely not a PTY (terminal in software). It's worth noting that old-school video terminals like the DEC VT100 you've pictured cannot run software, let alone an OS, since they're not computers.Chericheria
@dwk Well spotted regarding the VT100 video terminal. I've updated the photo.Sestina

© 2022 - 2024 — McMap. All rights reserved.