Linux based terminal console tty pty introduction

  • 2020-10-07 18:57:20
  • OfStack

1. Basic concepts

1, tty

tty: Derived from teletypes, or teletypewriters, it originally referred to teletypewriters, which read and sent messages over a serial line using a printer keyboard, which was later replaced by a keyboard and display, so it is now more appropriate to call them terminals. Terminal is a type of character device, it has a variety of types, usually using tty to refer to the various types of terminal devices.

2, pty

pty (pseudo terminal, virtual terminal) : But if we remote telnet to the host or use xterm, don't we also need one terminal interaction? Yes, this is pty(pseudo-ES24en)

3, pts/ptmx

pts/ptmx(pts/ptmx in combination with pty) : pts(ES39en-ES40en slave) is the implementation method of pty, and pseudo-ES45en master) is used in conjunction with pty.

2. Terminal special equipment files

Under the device special file directory /dev/ of Linux system, terminal Special device file 1 generally has the following types:

1. Serial port terminal (/dev/ttySn)

Serial port terminal (Serial Port Terminal) is a terminal device connected using a computer serial port. The computer treats each serial port as a 1-character device. There was a time when these serial port devices were often referred to as terminal devices because their greatest use was to connect terminals. The device names corresponding to these serial ports are /dev/tts/0(or /dev/ttyS0), /dev/tts/1(or /dev/ttyS1), etc. The device Numbers are (4,0), (4,1), respectively, corresponding to COM1 and COM2 under the DOS system. To send data to a port, you can redirect standard output to these special file names on the command line. For example, at the command line prompt, type echo test > /dev/ttyS1 sends the word "test" to a device connected to port ttyS1(COM2).

2. False terminal (/dev/pty/)

Pseudo terminals (Pseudo Terminal) are pairs of logical terminal devices (that is, master and slave devices, and operations on master are reflected on slave).
For example/dev ptyp3 and/dev/ttyp3 (or in the device file system are respectively/dev/pty/m3 and/dev pty/s3). They are not directly related to the actual physical device. If a program views ptyp3(master device) as a serial port device, its read/write operations on that port are reflected on the other ttyp3(slave device) corresponding to that logical terminal device. ttyp3, on the other hand, is a logical device used by another program for read and write operations. A host A communicates with the login program of host A through "pseudo terminal".

3. Control terminal (/dev/tty)

Control terminal refers to a terminal controller capable of controlling one or several sets of equipment.
If the current process has a control terminal (Controlling Terminal), then /dev/tty is the device special file for the control terminal of the current process. You can use the command "ps? ax" to see which control terminal the process is connected to. For shell on which you are logged in, /dev/tty is the terminal you are using and the device number is (5,0). Use the command "tty" to see which actual terminal device it corresponds to. /dev/tty is somewhat similar to 1 connection to the actual end device being used.

4. Console terminal (/dev/ttyn, /dev/console)

In THE Linux system, the computer display is often referred to as the console terminal (Console). It simulates a terminal of type Linux (TERM=Linux) and has a number of device special files associated with it: tty0, tty1, tty2, etc. When you log in from the console, you use tty1. When using the Alt+[F1 -- F6] key combination, we can switch to tty2, tty3, etc. tty1, tty6, etc., are called virtual terminals. tty0 is the name of the virtual terminal being used. The information generated by the system is sent to the virtual terminal (also known as the console terminal). So no matter which virtual terminal you are currently using, system information is sent to the console terminal. /dev/console is the console. It is a device that interacts with the operating system and outputs 1 piece of information directly to the console. Users are currently allowed to log into the console only in single-user mode.

5. Virtual terminal (/dev/pts/n)

Pseudo terminal in X windows mode (under graphical interface). For example, I use konsole under Kubuntu, which is the virtual terminal I use. Using the tty command, I can see /dev/pts/1.

6. Other types

The Linux system also has many other kinds of terminal device special files for many different character devices. Examples include /dev/ttyIn terminal devices for ISDN devices, etc. I won't repeat it here.

tty devices include virtual consoles, serial ports, and pseudo-terminal devices. /dev/tty represents the current tty device, enter echo "hello" in the current terminal > /dev/tty, will be displayed directly in the current terminal

Linux system under the terminal, console, tty, pty brief introduction, although it is the basic content but very important, you must understand 1


Related articles: