- Terminal device in linux
- 1. Serial port terminal (/ dev / ttysn)
- 2, pseudo terminal (/ dev / pty /)
- 3, control terminal (/ dev / tty)
- 4, console terminal (/ dev / ttyn, / dev / console)
- Difference Between a Terminal, Shell, TTY, and Console
- Linux Terminal
- Linux Console
- Linux Shell
- TTY – Teletypewriter
- PTS – Pseudo Terminal Slave
Terminal device in linux
In the Linux system, the terminal is a character type device, which has a variety of types, usually using TTY to refer to various types of terminal devices. TTY is the abbreviation of Teletype, and TeleType is the earliest terminal device, very like electrical typewriters, produced by TeleType. The following types of terminal devices are included in Linux.
1. Serial port terminal (/ dev / ttysn)
Serial Port Terminal is a terminal device connected to a computer serial port. Computers treat each serial port as a character device. The device names corresponding to these serial ports are / dev / ttys0 (or / dev / tts / 0), / dev / ttys1 (or / dev / tts / 1), etc., the device number is (4, 0), (4, 0), ( 4, 1), etc..
On the command line, the standard output redirect to the port name can be sent to the port name, for example, type in the command line prompt: echo test > /dev/ttyS1 Will send the word «test» to the device connected to the TTYS1 port.
The current USB-serial converter is also very common, and its corresponding device node is usually / dev / ttyusb0, / dev / ttyUSB1, etc.
2, pseudo terminal (/ dev / pty /)
Pseudo-end PTY (Pseudo Terminal) is a pair of logical terminal devices, and there is a pair of device files, such as / dev / ptyp3, and / dev / ttyp3, they are not directly related to the actual physical device. If a program treats TTYP3 as a serial port device, it will react on the TTYP3 corresponding to the logical terminal device, while TTYP3 is as a serial port device, but it is The read / write operation of this port will react on TTYP3 corresponding to the logical terminal device, and TTYP3 is another logic device for reading and writing. Thus, two programs can communicate with each other through this logic device, and the program using TTYP3 will consider whether they are communicating with a serial port.
Take Telnet as an example, if someone is connected to the Linux system using the Telnet program, the Telnet program may begin to connect to device PTYP2, while a Getty program runs on the corresponding TTYP2 port. When Telnet gets a character from the distal end, the character passes the PTYP2, TTYP2 to the Getty program, and the Getty program returns «Login:» string information via TTYP2, PTYP2, and Telnet programs. In this way, the login program and the Telnet program communicate via a pseudo terminal. By using the appropriate software, two or more pseudo-terminal devices can be connected to the same physical serial port.
The current Linux version often uses the PTS (Pseudo-Terminal Slave) to fit the PTMX (Pseudo-Terminal Master). Directory / DEV / PTS is a file system with type DEVPTS and can be seen in the list of loaded file system. / DEV / PTMX is a primary device number 5, a secondary device number 2 character device, which is used to create an Master / slave pair. When a process is opened / dev / ptmx, it will get a MASTER’s file descriptor, and each open file descriptor corresponds to a separate master, and corresponds to a PTS, which is incorporated into PTSName as a parameter. You can get the path to the PTS.
3, control terminal (/ dev / tty)
If the current process has a control terminal, then / dev / tty is a device special file for the control terminal of the current process. Can use commands ps -ax To see which control terminals are connected to which control terminal, use the command «TTY» to see which actual terminal device corresponding to it. / dev / tty Some Links similar to the actual terminal device, for example:
[[email protected]]# ps ax PID TTY STAT TIME COMMAND 1 ? Ss 0:02 /sbin/init 2 ? S 0:00 [kthreadd] 3 ? S 0:24 [ksoftirqd/0] 5 ? S< 0:00 [kworker/0:0H]168 ? S 0:01 [jbd2/sda1-8] 169 ? S< 0:00 [ext4-rsv-conver] 170 ? S< 0:00 [kworker/1:1H] 284 ? S< 0:00 [kworker/3:1H] 312 ? S 0:00 upstart-udev-bridge --daemon 321 ? S< 0:00 [kworker/0:1H] 326 ? Ss 0:00 /lib/systemd/systemd-udevd --daemon 419 ? Ss 0:00 dbus-daemon --system --fork 433 ? S< 0:00 [rpciod] 459 ? S< 0:00 [nfsiod] 491 ? Ss 0:00 rpc.idmapd1193 ? Ssl 0:12 NetworkManager 1206 ? Sl 0:00 /usr/lib/policykit-1/polkitd --no-debug 1331 tty4 Ss+ 0:00 /sbin/getty -8 38400 tty4 1335 tty5 Ss+ 0:00 /sbin/getty -8 38400 tty5 1336 ? Ssl 0:10 thermald --no-daemon --dbus-enable 1346 tty2 Ss+ 0:00 /sbin/getty -8 38400 tty2 1347 tty3 Ss+ 0:00 /sbin/getty -8 38400 tty3 1351 tty6 Ss+ 0:00 /sbin/getty -8 38400 tty6 1410 ? Ss 0:00 /usr/sbin/sshd -D 1412 ? Ss 0:00 atd 1416 ? Ss 0:00 /usr/sbin/xinetd -dontfork -pidfile /var/run/xinetd.pid -stayalive -inetd_compat -inetd_ipv6 1418 ? Ss 0:01 cron 1419 ? Ss 0:54 /usr/sbin/irqbalance
The kernel thread (such as kthreadd) and user space’s daemon (such as udevd) are no control terminals, so its TTY column target is «?».
4, console terminal (/ dev / ttyn, / dev / console)
On UNIX systems, commonly referred to as a computer display terminal console (console). It emulates a terminal (TERM = Linux) for Linux type, and there are some special device files associated with it: tty0, tty1, tty2 like. When the user logs on the console, using tty1. Use Alt + [F1 ~ F6] key combination, we can switch to above tty2, tty3 like to go. tty1 ~ tty6 other virtual terminal is called, and information tty0 is currently used an alias virtual terminal, generated by the system will be sent to the terminal. Therefore, regardless of which virtual terminal is currently in use, the system information will be sent to the console terminal. Users can log on to different virtual terminals up, thus allowing the system while there are several different sessions period. Only the system root or superuser can write to / dev / tty0.
In Linux, you may start the specified command line output terminal of the current system in the following format:
device refers to the terminal device may be, tty0 (front virtual terminal), ttyX (X-th virtual terminal), ttySx (X-th serial), lp0 (first parallel port) and the like.
It refers to the generation of options provided for the device, which depends on the specific device driver. For serial devices, as used to define the parameters of:
Baud rate, parity, number of bits, the format BBBBPN, wherein a baud rate of BBBB, P represents the parity (n / o / e), N represents the number of bits, the default options are 9600n8.
The user can set the plurality of core console command line simultaneously, so that the output will be displayed on all the console, and when the user open () call to open / dev / console, a console will return as the last current value. E.g:
console=ttyS1, 9600 console=tty0
When defining the two console, and call open () to open / dev / console, using the virtual terminal tty0. But the core message is displayed simultaneously on tty0 VGA virtual terminal and serial port ttyS1. Simply put, we can put / dev / console regarded as core console tty file interface, the device number is 0x0501, when it calls tty_open (), it will escape the actual terminal equipment.
By looking at / proc / tty / drivers file you can know what type of tty devices exist and what drivers are loaded into the kernel, this file contains a different tty current existence of a list of drivers, including the driver name, the default node name, driver major number, the driver views number range used, and the type of tty driver. For example, the following example is given a / proc / tty / drivers file:
[[email protected]]# cat /proc/tty/drivers /dev/tty /dev/tty 5 0 system:/dev/tty /dev/console /dev/console 5 1 system:console /dev/ptmx /dev/ptmx 5 2 system /dev/vc/0 /dev/vc/0 4 0 system:vtmaster ttyprintk /dev/ttyprintk 5 3 console serial /dev/ttyS 4 64-111 serial pty_slave /dev/pts 136 0-1048575 pty:slave pty_master /dev/ptm 128 0-1048575 pty:master unknown /dev/tty 4 1-63 console
Difference Between a Terminal, Shell, TTY, and Console
Terminal, Shell, TTY, and Console are terminologies that are often confusing and loosely used to mean the same thing. But it’s hardly the case. There are nuanced differences that exist between each of these components.
In this guide, we flesh out the differences between these terminologies as they are used in UNIX/Linux systems.
Linux Terminal
A terminal is an environment that accepts text input and displays the output of the command executed. In a nutshell, it’s a text input/output environment, which allows you to run a shell and execute commands.
A terminal window is a text-only window that allows you to run commands in a shell. It is also synonymously referred to as a terminal emulator.
Examples of terminal emulators include Xterm, Tmux, gnome-terminal, xfce4-terminal, and Terminator to mention a few.
Linux Console
A console is a physical terminal or device with panels and computer controls, which facilitates interaction with the system by providing access to the terminal. To put this into context, a console usually comprises a keyboard and mouse in a traditional PC. These are then plugged into the main system in order to issue commands to the operating system.
Other examples of consoles include gaming consoles such as Xbox and PlayStation.
Linux Shell
A Shell is a command-line interpreter. It is an environment that allows you to run programs and shell scripts and display the output on the terminal. A shell sits between the user and the kernel and is responsible for the execution of commands and control management.
If you are logging into a headless system or server, the shell is the primary interface that you see once a connection is established. Examples of widely used shells include Bash shell (/bin/bash), Korn shell (ksh), Z Shell (zsh), and PowerShell in Windows systems.
To know the type of shell you are running, run the following command:
From the following output, you can see we are running the BASH shell.
TTY – Teletypewriter
TTY is an acronym for Teletype. It is derived from the Teletypewriter which is an obsolete electromechanical device used for typing and sending messages in the 1800s.
In Linux/UNIX, TTY refers to a standard terminal device such as the console that is attached to your server. Essentially, it is a device that lets you interact with the system by inputting data and viewing the output.
To check the TTY number you are currently connected to, run the w command
PTS – Pseudo Terminal Slave
PTS (Pseudo Terminal Slave) is a session used when connecting to another Linux system via SSH or using an SSH client such as Putty. To list the currently active PTS sessions on your system, again, run the w command, which lists both the tty number as well as the PTS remote connections.
The following output displays the default TTY session (tty7) and a PTS connection (pts/1) which is a remote SSH connection to the Linux system.
In this guide, we have drawn comparisons between Terminal, Shell, TTY, and Console. It’s our hope that you can comfortably distinguish these four components.