Look up the device from its tty file

devicestty

Here is from my Ubuntu

$ ls /dev/*tty*
/dev/tty    /dev/tty17  /dev/tty26  /dev/tty35  /dev/tty44  /dev/tty53  /dev/tty62      /dev/ttyS12  /dev/ttyS21  /dev/ttyS30
/dev/tty0   /dev/tty18  /dev/tty27  /dev/tty36  /dev/tty45  /dev/tty54  /dev/tty63      /dev/ttyS13  /dev/ttyS22  /dev/ttyS31
/dev/tty1   /dev/tty19  /dev/tty28  /dev/tty37  /dev/tty46  /dev/tty55  /dev/tty7       /dev/ttyS14  /dev/ttyS23  /dev/ttyS4
/dev/tty10  /dev/tty2   /dev/tty29  /dev/tty38  /dev/tty47  /dev/tty56  /dev/tty8       /dev/ttyS15  /dev/ttyS24  /dev/ttyS5
/dev/tty11  /dev/tty20  /dev/tty3   /dev/tty39  /dev/tty48  /dev/tty57  /dev/tty9       /dev/ttyS16  /dev/ttyS25  /dev/ttyS6
/dev/tty12  /dev/tty21  /dev/tty30  /dev/tty4   /dev/tty49  /dev/tty58  /dev/ttyprintk  /dev/ttyS17  /dev/ttyS26  /dev/ttyS7
/dev/tty13  /dev/tty22  /dev/tty31  /dev/tty40  /dev/tty5   /dev/tty59  /dev/ttyS0      /dev/ttyS18  /dev/ttyS27  /dev/ttyS8
/dev/tty14  /dev/tty23  /dev/tty32  /dev/tty41  /dev/tty50  /dev/tty6   /dev/ttyS1      /dev/ttyS19  /dev/ttyS28  /dev/ttyS9
/dev/tty15  /dev/tty24  /dev/tty33  /dev/tty42  /dev/tty51  /dev/tty60  /dev/ttyS10     /dev/ttyS2   /dev/ttyS29
/dev/tty16  /dev/tty25  /dev/tty34  /dev/tty43  /dev/tty52  /dev/tty61  /dev/ttyS11     /dev/ttyS20  /dev/ttyS3

I have 7 virtual consoles.

I have also opened some tabs inside gnome terminal, and several emacs (some running shell, but I have probably closed them all), and screen sessions (but now closed).

  1. what is the difference between /dev/tty, /dev/tty[[:digit:]]+,
    /dev/ttyprintk, and /dev/ttyS[[:digit:]]+?
  2. How can I find out which device each tty file corresponds to?
  3. Some said the device files for pseudo tty is /dev/pty, but why is
    there no pty file or dir here:

    $ ls /dev/*pty*
    ls: cannot access /dev/*pty*: No such file or directory
    
  4. Not sure what /dev/*pts* means:

    $ ls /dev/*pts* 
    0  1  10  11  12  13  14  15  16  17  18  2  20  21  22  23  24  26  3  4  5  6  7  8  9  ptmx
    

Thanks and regards!

Best Answer

What is a Terminal?

A terminal consists of a screen and keyboard that one uses to communicate remotely with a computer (the host). One uses it almost like it was a personal computer but the terminal is remote from its host computer that it communicates with (on the other side of the room or even on the other side of the world).

Question 1

  1. /dev/tty stands for the controlling terminal (if any) for the current process. To find out which tty's are attached to which processes use the ps -a command at the shell prompt (command line). Look at the tty column. For the shell process you're in, /dev/tty is the terminal you are now using.
  2. In Linux the PC monitor is called the console and has several device special files associated with it: tty0, tty1, tty2, etc. When you log in you are on tty1. To go to tty2 press Alt-F2. tty1, tty2, etc. are virtual terminals (sometimes called "virtual consoles"). You may log in to different virtual terminals and thus have a few different sessions with the computer going on at the same time. You switch between them using the Alt-F? key where "?" is the virtual-terminal number you want. The console is also known as /dev/tty0 and system messages may go to that device and display on your console. Only the system or the root user may write to /dev/tty0 to which /dev/console is sometimes linked.
  3. ttyprintk is a pseudo TTY driver, which allows users to make printk messages, via output to ttyprintk device.
  4. /dev/tty/S[[:digit:]] represents the serial ports. Each terminal is connected to a serial port on the host computer (often just a PC). The ports have names/numbers. The first few are: ttyS0, ttyS1, ttyS2, etc. These are represented by special files found in the /dev (device) directory. ttyS0 corresponds to COM1 in DOS or Windows. ttyS1 is COM2, etc.

Question 3 and Question 4 are related. Gilles has a wonderful explanation from here. Some of the excerpts from his answer are,

Pseudo-terminals are provided by a terminal emulator, which is an application. Some types of pseudo-terminals are:

  • GUI applications such as xterm, gnome-terminal, konsole, … transform keyboard and mouse events into text input and display output graphically in some font.
  • Multiplexer applications such as screen and tmux relay input and output from and to another terminal, to decouple text mode applications from the actual terminal.
  • Remote shell applications such as sshd, telnetd, rlogind, … relay input and output between a remote terminal on the client and a pty on the server.

Also, from the wiki page of pseudo terminal, I see the below information.

The master device file, which generally has a name of the form /dev/pty[p-za-e][0-9a-f], is the endpoint for communication with the terminal emulator. With this [p-za-e] naming scheme, there can be at most 256 tty pairs. Also, finding the first free pty master can be racy unless a locking scheme is adopted. For that reason, recent BSD operating systems, such as FreeBSD, implement Unix98 PTYs.[4]

BSD PTYs have been rendered obsolete by Unix98 ptys whose naming system does not limit the number of pseudo-terminals and access to which occurs without danger of race conditions. /dev/ptmx is the "pseudo-terminal master multiplexer". Opening it returns a file descriptor of a master node and causes an associated slave node /dev/pts/N to be created

So I suspect /dev/pty is not available which is why you see /dev/pts.

References

http://www.tldp.org/HOWTO/Text-Terminal-HOWTO-7.html

Linux: Difference between /dev/console , /dev/tty and /dev/tty0

http://lkml.iu.edu/hypermail/linux/kernel/1009.0/00333.html

Related Question