Gangmax Blog

What is tty/pty/pts

From here, here, here and here.

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. A pts is the slave part of a pty. A ptmx is the master part of a pty.

You can use “man pty” command to get more information about “pty/pts/ptmx”.

The “tty” is also a valid command which can be used to tell you the current terminal file name you are using. In another word, it can tell you the current terminal name. Let’s say when you are using “gnome-terminal” to execute some commands in different tabs and you get the following output by running “ps -ef” command:

1
2
3
4
5
6
7
8
9
10
11
12
UID        PID  PPID  C STIME TTY          TIME CMD
...
gang 23871 9105 0 09:16 pts/4 00:00:03 /usr/lib/jvm/java-8-oracle/bin/java -Dzookeeper.log.dir=. -Dzookeeper.root.logger=
root 24069 2 0 09:16 ? 00:00:00 [kworker/u4:0]
root 24350 2 0 09:18 ? 00:00:00 [kworker/0:2]
gang 24904 9883 0 09:20 ? 00:00:00 /usr/lib/ibus/ibus-engine-pinyin --ibus
gang 25030 22919 2 09:21 ? 00:00:47 /usr/lib/firefox/plugin-container /usr/lib/flashplugin-installer/libflashplayer.so
gang 26867 19823 0 09:42 pts/8 00:00:00 -zsh
root 29042 2 0 09:50 ? 00:00:00 [kworker/u4:1]
gang 29749 19831 0 09:51 pts/4 00:00:00 vi source/_posts/2017-02-21-what-is-tty-slash-pty-slash-pts.markdown
gang 29868 26867 0 09:55 pts/8 00:00:00 ps -ef
...

Note the “pts/4” and “pts/8” terminals. Now you can run “tty” command in each of your “gnome-terminal” tab:

1
2
3
4
5
6
# In tab 1.
> tty
/dev/pts/4
# In tab 2.
> tty
/dev/pts/8

Now you can see which process is started by which “gnome-terminal” tab.

Comments