Linux 上 TTY 的起源

07-17 1542阅读

注:机翻,未校对。


What is a TTY on Linux? (and How to Use the tty Command)

Linux 上 TTY 的起源

What does the tty command do? It prints the name of the terminal you’re using. TTY stands for “teletypewriter.” What’s the story behind the name of the command? That takes a bit more explaining.

tty 命令有什么作用?它会打印您正在使用的终端的名称。TTY 代表 “电传打字机”。命令名称背后的故事是什么?这需要更多的解释。

Teleprinters From the 1800s

1800 年代的电传打印机

In the 1830s and 1840s, machines known as teleprinters were developed. These machines could send typed messages “down the wire” to distant locations. The messages were typed by the sender on a keyboard of sorts. They were printed on paper at the receiving end. They were an evolutionary step in telegraphy, which had previously relied on Morse and similar codes.

在 1830 年代和 1840 年代,开发了称为电传打印机的机器。这些机器可以将键入的消息 “向下” 发送到遥远的地方。这些消息是由发件人在某种键盘上键入的。它们在接收端打印在纸上。它们是电报的进化步骤,以前电报依赖于摩尔斯电码和类似的电码。

Messages were encoded and transmitted, then received, decoded, and printed. There were several techniques used to encode and decode the messages. The most famous, and one of the most prolific, was patented in 1874 by Émile Baudot, for whom the baud rate is named. His character encoding scheme pre-dated ASCII by 89 years.

对消息进行编码和传输,然后接收、解码和打印。有几种技术用于编码和解码消息。最著名也是最多产的专利之一,于 1874 年由 Émile Baudot 获得专利,波特率就是以他的名字命名的。他的字符编码方案比 ASCII 早 89 年。

Baudot’s encoding eventually became the closest thing to a standard in teleprinter encoding, and it was adopted by most manufacturers. Baudot’s original hardware design had only five keys, similar to piano keys. The operator was required to learn a particular key combination for each letter. Eventually, the Baudot encoding system was coupled to a traditional keyboard layout.

Baudot 的编码最终成为最接近电传打印机编码标准的东西,并被大多数制造商采用。Baudot 最初的硬件设计只有五个键,类似于钢琴键。操作员需要学习每个字母的特定组合键。最终,Baudot 编码系统与传统的键盘布局相结合。

To mark that advancement, the machines were named teletypewriters. This was shortened to teletypes and eventually to TTYs. So that’s where we get the acronym TTY from, but what has telegraphy got to do with computing?

为了纪念这一进步,这些机器被命名为电传打字机。这被缩短为电传打字机,并最终被缩短为 TTY。这就是我们得到首字母缩略词 TTY 的地方,但电报与计算有什么关系?

ASCII and Telex

ASCII 和电传

When ASCII arrived in 1963, it was adopted by the teletype manufacturers. Despite the invention and widespread use of the telephone, teletypes were still going strong.

当 ASCII 于 1963 年问世时,它被电传打字机制造商采用。尽管电话已经发明并被广泛使用,但电传打字机仍然很强大。

Telex was a worldwide network of teletypes that allowed written messages to be sent around the globe. They were the principal means of transmitting written messages in the period following World War II up to the fax machine boom of the 1980s.

电传是一个全球性的电传打字机网络,允许在全球范围内发送书面信息。它们是二战后至 1980 年代传真机热潮期间传输书面信息的主要手段。

Computers were evolving too. They were becoming capable of interacting with users in real time, and of supporting multiple users. The old batch method of working became insufficient. People didn’t want to wait 24 hours or longer for their results. Making stacks of punched cards and waiting overnight for results was no longer acceptable.

计算机也在不断发展。它们能够与用户实时交互,并支持多个用户。旧的批处理工作方法变得不够用。人们不想等待 24 小时或更长时间才能获得结果。制作成堆的打孔卡片并等待一夜的结果不再是可以接受的。

People needed a device that would allow them to enter instructions and get results sent back to them. People wanted efficiency.

人们需要一种设备,允许他们输入指令并将结果发回给他们。人们想要效率。

The Teletype Repurposed

电传打字机的新用途

The teletype was the perfect candidate as an input/output device. It was, after all, a device designed to allow messages to be typed, encoded, sent, received, decoded, and printed.

电传打字机是输入 / 输出设备的完美候选者。毕竟,它是一种旨在允许输入、编码、发送、接收、解码和打印消息的设备。

What did the teletype care if the device at the other end of the connection wasn’t another teletype? As long as it spoke the same encoding language and could receive messages and send messages back, the teletype was happy.

如果连接另一端的设备不是另一个电传打字机,电传打字机在乎什么?只要它说相同的编码语言,并且可以接收消息并发送消息,电传打字机就很高兴。

And of course, it used a more-or-less standard keyboard.

当然,它或多或少使用了标准键盘。

Hardware Emulated Teletypes

硬件模拟电传打字机

Teletypes became the default means of interacting with the large mini and mainframe computers of that era.

电传打字机成为那个时代与大型迷你和大型计算机交互的默认方式。

They were eventually replaced by devices that emulated those electro-mechanical machines using electronics. These had Cathode Ray Tubes (CRTs) instead of paper rolls. They didn’t shake when delivering responses from the computer. They permitted hitherto impossible functionality, such as moving the cursor around the screen, clearing the screen, bolding text, and so on.

它们最终被使用电子设备模拟那些机电机器的设备所取代。这些具有阴极射线管 (CRT) 而不是纸卷。当从计算机发送响应时,它们不会摇晃。它们允许迄今为止不可能的功能,例如在屏幕上移动光标、清除屏幕、加粗文本等。

The DEC VT05 was an early example of a virtual teletype, and an ancestor of the famous DEC VT100. Millions of DEC VT100s were sold.

DEC VT05 是虚拟电传打字机的早期示例,也是著名的 DEC VT100 的祖先。售出数百万台 DEC VT100。

Software Emulated Teletypes

软件模拟电传打字机

In the desktop environment of Linux and other Unix-like operating systems such as macOS, the terminal window and applications such as x-term and Konsole are examples of virtual teletypes. But these are emulated entirely in software. They are called pseudo-teletypes. This was shortened to PTS.

在 Linux 和其他类 Unix 操作系统(如 macOS)的桌面环境中,终端窗口和 x-term 和 Konsole 等应用程序是虚拟电传打字机的示例。但这些完全是在软件中模拟的。它们被称为伪电传打字机。这被缩短为 PTS。

And that’s where tty comes in.

这就是 tty 的用武之地。

What can tty Tell us?

tty 能告诉我们什么?

In Linux, there is a pseudo-teletype multiplexor which handles the connections from all of the terminal window pseudo-teletypes (PTS). The multiplexor is the master, and the PTS are the slaves. The multiplexor is addressed by the kernel through the device file located at /dev/ptmx.

在 Linux 中,有一个伪电传打字多路复用器,它处理来自所有终端窗口伪电传打字机 (PTS) 的连接。多路复用器是主站,PTS 是从站。多路复用器由内核通过位于 /dev/ptmx 的设备文件进行寻址。

The tty command will print the name of the device file that your pseudo-teletype slave is using to interface to the master. And that, effectively, is the number of your terminal window.

tty 命令将打印伪电传打字从站用于连接主站的设备文件的名称。实际上,这就是您的终端窗口的编号。

Let’s see what tty reports for our terminal window:

让我们看看 tty 为我们的终端窗口报告了什么:

tty

Linux 上 TTY 的起源

The response shows we are connected to the device file at /dev/pts/0.

响应显示我们已连接到位于 /dev/pts/0 的设备文件。

Our terminal window, which is a software emulation of a teletype (TTY), is interfaced to the pseudo-teletype multiplexor as a pseudo-teletype (PTS). And it happens to be number zero.

我们的终端窗口是电传打字机 (TTY) 的软件仿真,作为伪电传打字机 (PTS) 连接到伪电传打字机。它恰好是零号。

The Silent Option

静音选项

The -s (silent) option causes tty to generate no output.

-s(静默)选项会导致 tty 不生成输出。

tty -s

Linux 上 TTY 的起源

It does is produce an exit value, however:

但是,它确实会产生一个退出值:

  • 0: if standard input is coming from a TTY device, emulated or physical.

    0:如果标准输入来自 TTY 设备,则为仿真设备或物理设备。

  • 1: if standard input is not coming from a TTY device.

    1:如果标准输入不是来自 TTY 设备。

  • 2: Syntax error, incorrect command line parameters were used.

    2:语法错误,使用了不正确的命令行参数。

  • 3: A write error has occurred.

    3:发生写入错误。

    This is likely to be most useful in Bash scripting. But, even on the command line, we can demonstrate how to have a command executed only if you are running in a terminal window (a TTY or a PTS session).

    这在 Bash 脚本中可能是最有用的。但是,即使在命令行上,我们也可以演示如何仅在终端窗口(TTY 或 PTS 会话)中运行时执行命令。

    tty -s && echo "In a tty"
    

    Linux 上 TTY 的起源

    Because we are running in a TTY session, our exit code is 0, and the second command is executed.

    因为我们在 TTY 会话中运行,所以我们的退出代码为 0,并执行第二个命令。

    Linux 上 TTY 的起源

    The who Command who 命令

    Other commands can reveal your TTY number. The who command will list information for all logged in users, including yourself.

    其他命令可以显示您的 TTY 号码。who 命令将列出所有登录用户(包括您自己)的信息。

    Alec and Mary are remotely connected to the Linux computer. They are connected to PTS one and two.

    Alec 和 Mary 远程连接到 Linux 计算机。它们连接到 PTS 1 和 2。

    User dave is shown as connected to “:0”.

    用户 dave 显示为连接到 “:0”。

    This represents the screen and keyboard physically connected to the computer. Even though the screen and keyboard are hardware devices, they are still connected to the multiplexor through a device file. tty reveals that it is /dev/pts/2.

    这表示物理连接到计算机的屏幕和键盘。即使屏幕和键盘是硬件设备,它们仍然通过设备文件连接到多路复用器。tty 显示它是 /dev/pts/2。

    who
    
    tty
    

    Linux 上 TTY 的起源

    Accessing a TTY

    访问 TTY

    You can access a full-screen TTY session by holding down the Ctrl+Alt keys, and pressing one of the function keys.

    您可以通过按住 Ctrl+Alt 键并按其中一个功能键来访问全屏 TTY 会话。

    Ctrl+Alt+F3 will bring up the login prompt of tty3.

    Ctrl+Alt+F3 将显示 tty3 的登录提示。

    Linux 上 TTY 的起源

    If you log in and issue the tty command, you’ll see you are connected to /dev/tty3.

    如果您登录并发出 tty 命令,您将看到您已连接到 /dev/tty3。

    This isn’t a pseudo-teletype (emulated in software); it is a virtual teletype (emulated in hardware). It is using the screen and keyboard connected to your computer, to emulate a virtual teletype like the DEC VT100 used to do.

    这不是伪电传打字机(在软件中模拟); 它是一种虚拟电传打字机(在硬件中模拟)。它使用连接到计算机的屏幕和键盘来模拟虚拟电传打字机,就像 DEC VT100 过去所做的那样。

    You can use function keys Ctrl+Alt with function keys F3 to F6 and have four TTY sessions open if you choose. For example, you could be logged into tty3 and press Ctrl+Alt+F6 to go to tty6.

    您可以将功能键 Ctrl+Alt 与功能键 F3 到 F6 一起使用,并根据需要打开四个 TTY 会话。例如,您可以登录到 tty3 并按 Ctrl+Alt+F6 转到 tty6。

    Linux 上 TTY 的起源

    To get back to your graphical desktop environment, press Ctrl+Alt+F2.

    若要返回到图形桌面环境,请按 Ctrl+Alt+F2。

    Pressing Ctrl+Alt+F1 will return you to the login prompt of your graphical desktop session.

    按 Ctrl+Alt+F1 将返回图形桌面会话的登录提示。

    At one time, Ctrl+Alt+F1 through to Ctrl+Alt+F6 would open up the full-screen TTY consoles, and Ctrl+Alt+F7 would return you to your graphical desktop environment. If you are running an older Linux distribution, this might be how your system behaves.

    有一次,Ctrl+Alt+F1 到 Ctrl+Alt+F6 将打开全屏 TTY 控制台,而 Ctrl+Alt+F7 将返回到图形桌面环境。如果您运行的是较旧的 Linux 发行版,则这可能是您的系统的行为方式。

    This was tested on current releases of Manjaro, Ubuntu, and Fedora and they all behaved like this:

    这在 Manjaro、Ubuntu 和 Fedora 的当前版本上进行了测试,它们的行为都是这样的:

    1. Ctrl+Alt+F1: Returns you to the graphical desktop environment log in screen.

      Ctrl+Alt+F1:返回到图形桌面环境登录屏幕。

    2. Ctrl+Alt+F2: Returns you to the graphical desktop environment.

      Ctrl+Alt+F2:返回到图形桌面环境。

    3. Ctrl+Alt+F3: Opens TTY 3.

      Ctrl+Alt+F3:打开 TTY 3。

    4. Ctrl+Alt+F4: Opens TTY 4.

      Ctrl+Alt+F4:打开 TTY 4。

    5. Ctrl+Alt+F5: Opens TTY 5.

      Ctrl+Alt+F5:打开 TTY 5。

    6. Ctrl+Alt+F6: Opens TTY 6.

      Ctrl+Alt+F6:打开 TTY 6。

    Having access to these full-screen consoles allows people using command-line only installations of Linux—and many Linux servers are configured this way— to have multiple consoles available.

    通过访问这些全屏控制台,人们可以使用仅命令行安装的 Linux— 许多 Linux 服务器都是这样配置的 — 以拥有多个可用的控制台。

    Ever been working on a Linux machine with a graphical desktop environment and had something cause your session to freeze? Now you can hop over to one of the TTY console sessions so that you can try to rectify the situation.

    曾经在具有图形桌面环境的 Linux 机器上工作并且有什么东西导致您的会话冻结吗?现在,您可以跳转到其中一个 TTY 控制台会话,以便您可以尝试纠正这种情况。

    You can use top and ps to try to identify the failed application, then use kill to terminate it, or just use shutdown to try to close down as gracefully as the state of the computer will allow.

    您可以使用 top 和 ps 尝试识别失败的应用程序,然后使用 kill 终止它,或者只使用 shutdown 尝试在计算机状态允许的范围内尽可能优雅地关闭。

    Three Little Letters With a Lot of History 三个充满历史气息的小字母

    The tty command gets its name from a device from the late 1800s, appeared in Unix in 1971, and is part of Linux and Unix-like operating systems to this day.

    tty 命令得名于 1800 年代后期的设备,于 1971 年出现在 Unix 中,至今仍是 Linux 和类 Unix 操作系统的一部分。

    The little chap has quite a tale behind him.

    这个小家伙背后有一个故事。


    via:

    • What is a TTY on Linux? (and How to Use the tty Command By Dave McKay Published Jul 15, 2019
VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]