0%

linux 终端 控制台 pty tty

TTY

说明

  • 为了解读的可信度,尽量会从官方文档、man手册、源码等方面引用
  • Linux不同发行版、不同内核版本等会略有差异
  • 防止一些链接失效或不可访问,会把一些关键信息摘录

tty command

man tty - print the file name of the terminal connected to standard input

tty 作为Linux命令 - 打印当前连接的终端连接名(terminal)到标准输出,也就是说tty始终链接指向的是当前终端

通过ssh登录远程某Linux Server

1
2
3
4
5
root@remote ~ # tty
/dev/pts/4

root@remote ~ # echo "hello" > /dev/pts/4
hello

本地Server接显示器(基于后面知识的不停展开补充此样例)

1
2
3
4
5
local ~# tty
/dev/tty1

local ~# echo "hello" > /dev/tty1
hello

注意: 输出中的细微区别 /dev/pts/* /dev/tty*

virtual-console vc virtual-terminal vt tty vcs pty ptmx pts

virtual-console vc virtual-terminal vt tty

首先先从Wiki - Vitrual console入手

  • virtual console(vc)也被称之为virtual terminal(vt), vc和vt是一个概念; 是面向用户的由键盘、显示器等输入/输出设备组成的一个概念组合
    • chvt命令可用来进行控制台切换
    • openvt在新的vc里运行程序
    • deallocvt关闭vc

从Linux内核官方文档看到关于虚拟控制台virtual-consoles部分https://www.kernel.org/doc/html/latest/admin-guide/devices.html?highlight=console#virtual-consoles-and-the-console-device

1
2
3
Virtual consoles are full-screen terminal displays on the system video monitor. Virtual consoles are named /dev/tty#, with numbering starting at /dev/tty1; /dev/tty0 is the current virtual console. /dev/tty0 is the device that should be used to access the system video card on those architectures for which the frame buffer devices (/dev/fb*) are not applicable. Do not use /dev/console for this purpose.

The console device, /dev/console, is the device to which system messages should be sent, and on which logins should be permitted in single-user mode. Starting with Linux 2.1.71, /dev/console is managed by the kernel; for previous versions it should be a symbolic link to either /dev/tty0, a specific virtual console such as /dev/tty1, or to a serial port primary (tty*, not cu*) device, depending on the configuration of the system.
  • 虚拟控制台就是系统的终端回显,文件命名为/dev/tty#(万物皆文件),从/dev/tty1开始
  • /dev/tty0比较特殊,永远指向当前虚拟控制台
  • /dev/console 用户模式下系统发送消息用;从内核2.1.71开始,/dev/console由内核管理;之前的版本根据系统配置可能链接到/dev/tty0或/dev/tty1等

这时很容易产生另外的疑问: 虚拟控制台从/dev/tty1开始,最大是多少?为什么由tty命名?

  • 一些教材基于历史Linux系统原因,经常提到可通过Ctrl+Alt-F[1-7]可进行控制台切换,其中/dev/tty[1-6]为文本控制台,/dev/tty7是图形控制台,会让很多人停留在最大支持/dev/tty7

人肉看下ls /dev/tty*看了下最大到/dev/tty63,另外从内核文件看到也是63,include/uapi/linux/vt.h

1
2
3
4
define MIN_NR_CONSOLES 1       /* must be at least 1 */
#define MAX_NR_CONSOLES 63 /* serial lines start at 64 */
/* Note: the ioctl VT_GETSTATE does not work for
consoles 16 and higher (since it returns a short) */

但是实际chvt 8 Ctrl+Alt+F8测试发现黑屏,只显示光标. 为什么呢?

这个定义只是规定了最大63,但实际相关服务还是依据传统进行的默认配置;如果确实要增加,还需:

/etc/systemd/logind.conf

1
2
[Login]
#NAutoVTs=7 # 按需变更

/lib/systemd/system/getty-static.service 按需添加getty@ttyN.service

1
ExecStart=... ... getty@tty7.service getty@tty8.service

也可以即时生效

1
systemctl start getty@ttyN.service

更多,这个链接怎么来的? less /lib/systemd/system/getty@.service

  • TTY 来源于 teletypewriter(电传打字机),算是最早的计算机终端?
vcs

man vcs vcs, vcsa - virtual console memory

1
2
3
4
5
6
7
8
9
10
DESCRIPTION
/dev/vcs0 is a character device with major number 7 and minor number 0, usually of mode 0644 and owner root.tty. It refers to the memory of the currently displayed virtual console terminal.
... ...

EXAMPLE
You may do a screendump on vt3 by switching to vt1 and typing

cat /dev/vcs3 >foo

... ...
pty ptmx pts

man pty pty - pseudoterminal interfaces

1
2
3
4
5
6
7
* 伪终端是一对虚拟的用于双向通信的字符设备,一头为mater,一头为slave
* 通常用于网络登录服务,例如ssh rlogin telnet; 或终端仿真,例如xterm script screen tmux unbuffer expect
* 两种类型的伪终端: BSD-style(弃用) 、UNIX 98 pseudoterminals(System V-style)
* /proc/sys/kernel/pty/max #动态调整大小限制,之前是内核配置CONFIG_UNIX98_PTYS
* /proc/sys/kernel/pty/nr #当前伪终端数量
* /dev/ptmx UNIX 98 master clone device
* /dev/pts/* UNIX 98 slave devices

man pts ptmx, pts - pseudoterminal master and slave

1
pty的具体实现,当一个进程(应用)打开/dev/ptmx时会返回:一个pseudoterminal master文件描述符(PTM),一个创建在/dev/pts目录下的pseudoterminal slave(PTS)设备
内核文档中关于硬件文件的描述

https://www.kernel.org/doc/Documentation/admin-guide/devices.txt

摘录部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
5 char   Alternate TTY devices
0 = /dev/tty Current TTY device
1 = /dev/console System console
2 = /dev/ptmx PTY master multiplex
3 = /dev/ttyprintk User messages via printk TTY device
64 = /dev/cua0 Callout device for ttyS0
...
255 = /dev/cua191 Callout device for ttyS191

(5,1) is /dev/console starting with Linux 2.1.71. See
the section on terminal devices for more information
on /dev/console.

136-143 char Unix98 PTY slaves
0 = /dev/pts/0 First Unix98 pseudo-TTY
1 = /dev/pts/1 Second Unix98 pseudo-TTY
...

These device nodes are automatically generated with
the proper permissions and modes by mounting the
devpts filesystem onto /dev/pts with the appropriate
mount options (distribution dependent, however, on
*most* distributions the appropriate options are
"mode=0620,gid=<gid of the "tty" group>".)
StackExchange中支持最多的一个回答: terminal console shell等的不同

https://unix.stackexchange.com/questions/4126/what-is-the-exact-difference-between-a-terminal-a-shell-a-tty-and-a-con

在UNIX术语中,最精简的概括

  • terminal = tty = text input/output environment
  • console = physical terminal
  • shell = command line interpreter

字面翻译: console更多指的物理主机自身的输入输出设备, terminal指的是其它地方通过网络连接到主机的输入输出设备

$TERM

man stty stty - change and print terminal line settings

man toe