Differences between tty, Pty, and PTS in Linux

Source: Internet
Author: User
Tags telnet program

Basic concepts:
1> tty (collectively referred to as the terminal device ):
Tty is derived from teletypes or teletypewriters. Originally, it refers to a telex typewriter, which reads and sends messages through a printer keyboard through a serial line. Later, it was replaced by a keyboard and a display, so now it is more appropriate to call the terminal.
A terminal is a type of terminal device. It has multiple types and generally uses tty for short.
2> Pty (virtual terminal ):
But if we remotely telnet to the host or use xterm, do we still need a terminal to interact? Yes, this is the virtual terminal Pty (pseudo-TTY)
3> pts/ptmx (combining pts/ptmx to implement PTY ):
PTS (pseudo-terminal slave) is the implementation method of Pty. It is used with ptmx (pseudo-terminal master) to implement Pty.
Linux terminal:

In the special file directory/dev/of a Linux system device, there are generally the following types of special device files on the terminal:
1. Serial Port Terminal (/dev/ttysn)
Serial Port
Terminal) is a terminal device connected by a computer serial port. The computer regards each serial port as a character device. For some time, these serial port devices are usually called terminal devices because
At that time, it was used to connect to terminals. The device names corresponding to these serial ports are/dev/TTS/0 (or/dev/ttys0 ),
/Dev/TTS/1 (or/dev/ttys1). The device numbers are ),
() And so on, which correspond to COM1 and com2 in the DOS system respectively. To send data to a port, You can redirect the 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 the device connected to the ttys1 (com2) port. Can be connected to the serial port for the experiment.
2. Pseudo Terminal (/dev/PtY /)
The Pseudo Terminal is a pair of logical terminal devices (namely, the master and slave devices, and the operations on the master will be reflected on the slave ).
For example,/dev/ptyp3 and/dev/ttyp3 (or/dev/PtY/m3 and
/Dev/PtY/S3 ). They are not directly related to physical devices. If a program regards ptyp3 (master device) as a serial port device, it reads/
The write operation is reflected in another ttyp3 (Slave Device) corresponding to the logical terminal device. Ttyp3 is the logical device used by another program for read/write operations.
In this way, two processes
This logic device can communicate with each other, and one of the programs using ttyp3 thinks that they are communicating with a serial port. This is like pipeline operations between logical device pairs. For
Ttyp3 (S3), any program designed to use a serial port device can use this logical device. However, for the use of ptyp3 programs, it needs to be specially designed to use
Ptyp3 (m3) logical device.
For example, if someone uses a telnet program to connect to your computer online, the telnet program may start to connect to the device.
Ptyp2 (m2) (a Pseudo Terminal port ). At this time, a Getty program should run on the corresponding ttyp2 (S2) port. When Telnet obtains a character from the remote end
This character is passed
The getty program returns the "login:" string information to the network through S2, m2, and telnet. In this way, the login program and the telnet program will be connected
Through the "Pseudo Terminal" for communication. By using appropriate software, you can connect two or more Pseudo Terminal devices to the same physical serial port.
When using the Device File System
Filesystem) previously, in order to get a large number of special files on Pseudo Terminal devices, a complicated file name naming method was used. Because only 16 ttyp (ttyp0-ttypf) exist)
In order to obtain more logical device pairs, the device file uses characters such as Q, R, and s to replace P. For example, ttys8 and ptys8 are Pseudo Terminal Device pairs. However, this naming method currently
It is still used in Linux systems such as RedHat.
However, unix98 on Linux does not use the above method, but uses "Pty
Master mode, such as/dev/ptm3. Its corresponding end is automatically created as/dev/pts/3. In this way, a Pty Pseudo Terminal can be provided as needed. Directory
/Dev/PTS is a file system of the devpts type and can be seen in the list of loaded file systems. Although "file"/dev/pts/3 seems to be in the file system of the device
But it is actually a different file system.
That is: Telnet ---> ttyp3 (S3: slave) ---> ptyp3 (M3: Master) ---> Getty
========================================================== ======================================
Lab:
1. open one or N terminal windows under X
2. # ls/dev/Pt *
3. Close the terminal window under X and run it again. Compare the output information twice.
In the RHEL4 environment: the output is/dev/ptmx/dev/pts/1. One (master) to multiple (slave) conditions exist.
========================================================== ======================================
3. control terminal (/dev/tty)
If the current process has a control terminal (controlling terminal),/dev/tty is the device special file of the control terminal of the current process. You can run the "PS-Ax" command to check which control terminal the process is connected. For the shell you log on to,/dev/tty is the terminal you are using, and the device number is (5, 0 ). Run the "tty" command to check which actual terminal device it corresponds. /Dev/tty is similar to a connection to the actually used terminal device.
4. Console terminal (/dev/ttyn,/dev/console)
In Linux, a computer monitor is usually called a console ). It simulates a Linux terminal (term = Linux), and some special files of the device are associated with it: tty0, tty1, tty2, and so on. When you log on to the console, tty1 is used. With Alt + [F1-F6] keys, we can switch to tty2, tty3 and so on. Tty1-tty6 is called virtual terminal, and tty0 is an alias of the current virtual terminal, the information generated by the system will be sent to the terminal (also called the console terminal ). Therefore, no matter which virtual terminal is being used, the system information will be sent to the console terminal. You can log on to different virtual terminals, so that the system can have several different sessions at the same time. Only the system or Super User Root can write to/dev/tty0, that is, the following example:
1. # tty (view the current TTY)
/Dev/tty1
2. # echo "test tty0">/dev/tty0
Test tty0
5 virtual terminal (/dev/pts/N)
A Pseudo Terminal in xwindows mode.

6 Other Types
In Linux, there are many other types of special files on terminal devices for many different character devices. For example,/dev/ttyin terminal devices for ISDN devices. I will not go into details here.
FAQ: terminal and console
Rrom: http://blog.footoo.org /? P = 73
Posted on Tuesday, November 28 th, 2006 by Cliff
Wu Jin (cliffwoo@gmail.com)
Footoo opensource lab
Since many of my friends have never been very clear about the concepts of terminals, I wrote this FAQ to help you understand these concepts. If this is not the case, please send a letter to us.
Q: What is/dev/console?
A:/dev/console refers to the console, which is a device that interacts with the operating system. The system directly outputs some information to the console. Currently, users can log on to the console only in single-user mode.

Q: What is/dev/tty?
A: tty devices include virtual consoles, serial ports, and Pseudo Terminal devices.
/Dev/tty indicates the current tty deviceInput echo "Hello">/dev/tty in the current terminal, which will be directly displayed in the current terminal.
Q: What is/dev/TTYs?
A:/dev/TTYs * is a serial terminal device.
Q: What is/dev/Pty?
A:/dev/Pty * is a Pseudo Terminal. The so-called Pseudo Terminal is a logical terminal device and is mostly used to simulate terminal programs. For example, we open a terminal in X Window and log on to the Linux host using telnet or SSH in windows, the Pty device is used at this time (to be precise, the Pty slave device is used ).
Q:/dev/tty0 and/dev/tty1... What is/dev/tty63? What are the differences between them?
A:/dev/tty0 indicates the current virtual console, while/dev/tty1 indicates the first virtual console. For example, when Alt + F2 is used for switching, the system's virtual console is/dev/tty2, and the current console points to/dev/tty2
Q: How do I determine the current terminal (or console )?
A: Use the TTY command to determine the current terminal or console.
Q: Is/dev/console a symbolic link to/dev/tty0?
A:
At present, most of the texts refer to/dev/console as the link to/dev/tty0 (including "Linux kernel source code Scenario Analysis"), but this is inaccurate. Internal
Core documentation. Before 2.1.71,/dev/console can be linked to/dev/tty0 or other tty * based on different system settings.
All are controlled by the kernel. Currently, you can log on to/dev/console only in single-user mode (you can enter the TTY command in single-user mode for confirmation ).
Q: What is the difference between/dev/tty0 and/dev/FB?
A: You can use/dev/tty0 to access the video card in a system not enabled by the framebuffer device.
Q: What text can I refer to for the differences between a terminal and a console?
A: refer to "terminal devices" in documents such as documents and devices.txt"
. In addition, section 8.7 of Linux kernel source code scenario analysis and operating systems: Design and
In implementation, section 3.9 (section 3rd in version 3.8) provides a good introduction to the concepts and history of terminal devices. In addition
Operating system also introduces terminal devices.
The author of implementation is the same, so the text content is roughly the same. Operating Systems: Design
And Implementation divides terminal devices into three categories, while modern operating
System divides terminal hardware devices into two categories. The difference is that the former uses X terminal as a category.
PS:

Only 2410 of 2.6 is called ttysac0, and 9200 is called ttys0.

========================================================== ==========================================
Tty is short for teletype. Teletype is one of the earliest terminal devices, such as a telex typewriter (or ?), It is produced by teletype. The device name is stored in the special file directory/dev/. The special device files on the terminal generally include the following types:
1. Serial Port Terminal (/dev/ttysn)
Serial Port
Terminal) is a terminal device connected by a computer serial port. The computer regards each serial port as a character device. For some time, these serial port devices are usually called terminal devices because
At that time, it was used to connect to terminals. The device names corresponding to these serial ports are/dev/TTS/0 (or/dev/ttys0),/dev/TTS/1 (or/dev
/Ttys1) and so on. The device numbers are () and () respectively, which correspond to COM1 and com2 in the DOS system. To send data to a port, you can mark
The quasi-output can be redirected to these special file names. For example, at the command line prompt, type: echo test>
/Dev/ttys1 sends the word "test" to the device connected to the ttys1 (com2) port.
2. Pseudo Terminal (/dev/PtY /)
Pseudo Terminal
Terminal) is a pair of logical terminal devices, such as/dev/ptyp3 and/dev/ttyp3 (or/dev/PtY/m3 and
/Dev/PtY/S3 ). They are not directly related to physical devices. If a program regards ttyp3 as a serial port device, its read/write operations on the port are reflected in the logic
The other above (ttyp3) of the terminal device pair ). Ttyp3 is the logical device used by another program for read/write operations. In this way, the two programs can communicate with each other through this logical device.
A program using ttyp3 considers itself communicating with a serial port. This is like pipeline operations between logical device pairs.
For ttyp3 (S3), any program designed to use a serial port device can use this logical device. However, for programs that use ptyp3, a dedicated design is required to use the ptyp3 (m3) logical device.
Example
For example, if someone uses a telnet program to connect to your computer online, the telnet program may start to connect to the device ptyp2 (m2) (a Pseudo Terminal port ). In this case
A Getty program should run on the corresponding ttyp2 (S2) port. When Telnet obtains a character from the remote end, the character is passed to the Getty program through m2 and S2.
The getty program returns the "login:" string information to the network through S2, m2, and telnet. In this way, the login program and the telnet program use the "Pseudo Terminal"
Communication. By using appropriate software, you can connect two or more Pseudo Terminal devices to the same physical serial port.
Before using device filesystem, HP-UX AIX and others used complicated naming methods to get a large number of special files for pseudo-terminal devices.
3. control terminal (/dev/tty)
If the current process has a control terminal (controlling
Terminal), then/dev/tty is the device special file of the control terminal of the current process. You can run the "Ps" command
-Ax to check which control terminal the process is connected. For the shell you log on to,/dev/tty is the terminal you are using, and the device number is (5, 0 ). Run the "tty" command to view it.
The actual terminal device. /Dev/tty is similar to a connection to the actually used terminal device.
4. Console terminal (/dev/ttyn,/dev/console)
In
In Unix systems, a computer monitor is usually called a console ). It simulates a Linux terminal (term = Linux) and has some device features.
Related to special files: tty0, tty1, and tty2. When you log on to the console, tty1 is used. With Alt + [F1-F6] keys, we can switch
Tty2 and tty3. Tty1
-Tty6 is called a virtual terminal, while tty0 is an alias of the currently used virtual terminal. information generated by the system is sent to the terminal. Therefore, no matter which virtual terminal is being used
Messages are sent to the console.
You can log on to different virtual terminals, so that the system can have several different sessions at the same time. Only the system or Super User Root can write data to/dev/tty0,
5. Other Types
There are many other types of special files for terminal devices for many different character devices. For example,/dev/ttyin terminal devices for ISDN devices. I will not repeat it here

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.