当前位置: 首页>>技术教程>>正文


tty – 为什么有这么多虚拟控制台?

,

问题描述

有这么多虚拟控制台的原因是什么?

我会理解是否有一个以防 GUI 崩溃但除了默认值之外还有 6 个?它们是干什么用的?除了 GUI 冻结时,我什至看不到它们中的任何一个的用途。

最佳思路

简短的回答:为什么不呢? (请暗示微笑)

更冗长的:我想这主要是一个历史问题。在我用 VC 支持启动的第一个 linux 中,有 6 个虚拟控制台配置了 getty,我真的忘了是什么时候(我想是 1990 年左右)。然后,当您启动图形环境(手动,使用 startx )时,它会在第一个免费 VC 上自行打开,恰好是 #7。而且我当时的大部分工作仍然在 VC 上:编辑速度要快得多,有时我使用的 VC 比标准 6 还要多,my laptop was not exactly a graphic monster

例如,我曾经运行三个编辑器(一个程序,它的输入数据,一个描述它的 TeX 文件),一个用于编译的 VC,另一个用于阅读手册,另一个通过 telnet 连接到我的邮件服务器。

我怀疑仍然使用六个虚拟控制台的基本原理是让每个人都在 #7 上使用图形 VC,因此您可以在手册上写“Ctrl-Alt-F7”而不是“Ctrl-Alt-Fx,其中 x 是第一个免费 VC”。

作为旁注,您可以(我想——从未尝试过)削减 VC。简单地做

sudo bash -c "echo 'manual' >> /etc/init/tty6.override"

停止 VC#6, following the upstart manual

次佳思路

根据 Unix 哲学,所有 /dev/tty* 条目都称为 device file 。它们充当用户和实际设备之间的链接,但它们的用途可能略有不同。

虚拟控制台如何分配

console(4) manual

\\n

Before kernel version 1.1.54 the number of virtual consoles was compiled into the kernel (in tty.h: #define NR_CONSOLES 8) and could be changed by editing and recompiling. Since version 1.1.54 virtual consoles are created on the fly, as soon as they are needed.

\\n

预系统系统中的动态创建通过 /etc/default/console-setup 变量 ACTIVE_CONSOLES 控制。从 console-setup(5) manual

\\n

ACTIVE_CONSOLES Specifies the device files in /dev of the virtual\\n terminals to be configured. File name wild-cards (*, ?) are allowed.\\n On Linux usually you can set this to /dev/tty[1-6] and on FreeBSD a\\n sensible value is /dev/ttyv[0-8]. You can assign to this variable also\\n the special value guess. It will cause setupcon(1) to attempt to guess\\n the active virtual consoles by looking in configuration files such as\\n /etc/inittab and /etc/ttys. This guessing is not always reliable.

\\n

Systemd 配置依赖于 /etc/systemd/logind.conf 变量 NAutoVTs 。引自 logind.conf documentation

\\n

NAutoVTs=\\n Takes a positive integer. Configures how many virtual terminals (VTs) to allocate by default that, when switched to and are previously unused, “autovt” services are automatically spawned on. These services are instantiated from the template unit autovt@.service for the respective VT TTY name, for example, autovt@tty4.service. By default, autovt@.service is linked to getty@.service. In other words, login prompts are started dynamically as the user switches to unused virtual terminals. Hence, this parameter controls how many login “gettys” are available on the VTs. If a VT is already used by some other subsystem (for example, a graphical login), this kind of activation will not be attempted. Note that the VT configured in ReserveVT= is always subject to this kind of activation, even if it is not one of the VTs configured with the NAutoVTs= directive. Defaults to 6. When set to 0, automatic spawning of “autovt” services is disabled.

\\n

为您的环境减少适当的变量并重新启动将有助于减少 VT 的数量。可以通过以下命令手动删除这些设备:

sudo bash -c 'for num in $(seq 10 63 ) ; do MAKEDEV -v -d "tty$num" ; done'

然而,这有点毫无意义——设备文件不占用任何空间,也不影响系统性能。

另见 https://askubuntu.com/a/27975/295286

虚拟控制台一般信息

/dev/tty123 这样的设备文件可用于通过常规 shell 与系统交互。这些是虚拟终端。 Ubuntu系统默认打开TTY 1到6,但可以手动打开更多,例如:

sudo openvt -c 9 -s "bash"

这将在 /dev/tty9 上以 root 身份打开 bash 并立即将您切换到该 TTY(如果在本地终端上完成,但如果您通过远程 ssh 会话这样做,在这种情况下您可能需要使用 chvt 9 ,其中 9 是 /dev/tty9这个例子)。\u3000要在没有 sudo 的情况下作为非 root 执行相同操作,您必须更改要打开的 tty 的所有权。例如,

$ sudo chown admin_user:tty /dev/tty8 
$ openvt -c 8 -l 

这将使我在 tty8 上使用我的默认 shell 登录。然而,在我的测试中,这似乎只能通过 TTY 工作,所以如果你通过终端模拟器这样做,那可能不起作用(我收到 Couldn't get a file descriptor referring to the console\n 错误,只有 sudo openvt -c 8 -l 从模拟器为我工作)

从历史的角度来看,在创建所有花哨的终端多路复用器之前,这是一个有用的功能 – 您可以在这些控制台上生成多达 62 个命令(可能是服务器),并让它们运行,同时在 63rd 中工作。如今,这可能不是必需的。

默认创建多个设备文件。活动的是 1 到 6,但 one can alter their number 通过编辑 /etc/default/console-setup ,特别是这部分:

# Setup these consoles.  Most people do not need to change this.
ACTIVE_CONSOLES="/dev/tty[1-6]"

ttyS* 设备

这些是用于串行控制台的设备文件,也称为 UART(也称为 RS-232)。在过去,许多计算机系统都会带有串行端口。如今,这主要可以在服务器类型的计算机上找到。多个 ttyS 设备可用于 RS-232 集线器,它允许连接多个设备以通过 USB 或以太网进行管理。

对于电气工程师(这是我在大学主修的专业),我们使用串行端口与微控制器和开发板(例如 Arduino)进行交互。单端口计算机,例如 Raspberry Pi,也使用它。现在,使用 USB 转 UART 电缆建立串行连接,并通过 screen 在终端中打开控制台,如下所示:

screen /dev/ttyUSB0 115200 # the number is baud rate or speed

请注意,Arduino 和 Raspberry 将创建自己的设备文件用于串行连接,/dev/ttyUSB0/dev/ttyACM0

vcs* 设备

根据 man vcs,这些是控制台内存设备。简而言之,如果您执行 sudo cat /dev/vcs1 ,您将看到 /dev/tty1 屏幕上的内容,至少达到该终端中的最大行数。

第三种思路

为什么不多呢?在一个中运行 tophtop(提供 cpu/内存使用静态),在 2 或 3 中运行 ssh 到其他系统,在另一个中运行一些自动化任务,保持一个空闲以防万一,再登录多个用户,运行 cmatrix on一,在另一个启动GUI –

startx -- :1

(一些图形有这个错误)

http://mostlylinux.wordpress.com/troubleshooting/ttysessions/

当您真正开始使用此功能时,7 个 tty(默认为第 7 个)似乎很少 – 您可以添加更多。

减少数量:How can I reduce the number of TTYs?

第四种思路

实用性是真实的。在真正的 multi-user Linux 系统中可以同时运行多个程序,如果每个程序都在自己的虚拟终端中运行,您可以检查每个程序。当您登录到每一个时,如果需要,您可以以不同的用户身份登录。现在,诚然,许多人以 single-user 的视角来到 Linux,但自从 Linux 诞生于 multi-user 以来,这种能力从未被抛弃。

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/12975.html,未经允许,请勿转载。