Linux 黑话解释：TTY 是什么？
TTY 终端
![](/data/attachment/album/202112/18/155143cz6aplfi8i6elmll.jpg)


谈到 Linux 和 UNIX 时，你一定听说过 “TTY” 这个术语，但是，这玩意是什么？


作为一个桌面用户，它对你有用吗？你需要它吗？你能用它做什么？


在本文中，让我向你介绍这些，帮助你熟悉 Linux 中的 TTY。


注意：这个问题没有明确的答案，但它与过去的输入/输出设备的交互方式有关。因此，你必须了解一些历史，才能清楚地了解情况。


### “TTY” 背后的历史


一切始于 19 世纪 30 年代的<ruby> 电传打印机 <rt>  Teleprinter </rt></ruby>。


电传打印机可以让你通过电线发送或接受消息，它取代了摩尔斯电码通信，那是一种需要两个操作员才能有效地相互通信的方式。


一台电传打印机只需要一个操作员就可以轻松地传递消息。虽然它没有现代布局的键盘，但它的系统后来由 Donald Murray 在 1901 年进行了改良，包括了一个类似打字机的键盘。


Murray 电码减少了操作员发送消息的工作量。这才使得电传打印机在 1908 年有了发展成为商业<ruby> 电传打字机 <rt>  Teletypewriter </rt></ruby>的可能。TTY 即是电传打字机的缩写。


![二战期间在伦敦实际使用的电传打字机 | 图片来源于维基百科](/data/attachment/album/202112/18/155151h64ww5haa64q4sn6.jpg)


电传打字机和普通<ruby> 打字机 <rt>  typewriter </rt></ruby>的区别在于，电传打字机连接到通信设备，直接发送输入的消息。


[电传打字机使人类在没有计算机的情况下通过电线进行更快的通信成为可能](https://en.wikipedia.org/wiki/Teletype_Corporation#/media/File:What-is-teletype.jpg)。


从这时起，“TTY” 一词就存在了。


### （相对）现代的概念


现在，你一定想知道，它是如何进入现代计算机和 Linux 的？


最初是当电传打字机进入了市场，几年后半导体晶体管发展起来，然后演变成微处理器，为计算机的出现做好了准备。


最初的计算机没有键盘的概念，<ruby> 打孔卡 <rt>  Punch card </rt></ruby>就是输入的方法。


![一种插入计算机而不是通过键盘（TTY）输入的打孔卡计算机程序 | 图片来源于维基百科](/data/attachment/album/202112/18/155151d57wc8zxfyxfmoyf.jpg)


随着计算机的发展，批量输入的打孔卡最终被电传打字机取代，成为一种方便的输入/输出设备。


![1956 年的 LGP-30 计算机，附带 TTY](/data/attachment/album/202112/18/155151q33w0uldhuzwuiwt.jpg)


随着技术的进步，电传打字机被电子技术“虚拟化”了。因此，你不需要一个物理的、机械的 TTY，而是一个虚拟的电子 TTY。


早期的计算机甚至没有视频屏幕。字符被打印在纸上而不是显示在屏幕上。因此，你会看到“<ruby> 打印 <rt>  print </rt></ruby>”这个术语而不是“<ruby> 显示 <rt>  display </rt></ruby>”。随着技术的进步，视频显示后来被添加到终端中。


换句话说，你可能听说过把它们称为“视频终端”。或者，你可以称它们为“物理”终端。


后来，它们演变成具有更强的能力和功能的软件仿真的终端。


这就是所谓的“<ruby> 终端仿真器 <rt>  terminal emulator </rt></ruby>”，如 GNOME 终端或 Konsole，或者其他 [你在 Linux 上找到的各种终端仿真器](https://itsfoss.com/linux-terminal-emulators/)。


### 所以，Linux 中的 TTY 到底是什么？


在 Linux 或 UNIX 中，TTY 变为了一个抽象设备。有时它指的是一个物理输入设备，例如串口，有时它指的是一个允许用户和系统交互的虚拟 TTY（[参考此处](https://unix.stackexchange.com/questions/4126/what-is-the-exact-difference-between-a-terminal-a-shell-a-tty-and-a-con)）。


TTY 是 Linux 或 UNIX 的一个子系统，它通过 TTY 驱动程序在内核级别实现进程管理、行编辑和会话管理。


在编程的场景下，你还需要深入研究。但是考虑到本文的范围，这可能是一个容易理解的定义。


如果你好奇的话，你可以查看一个有点旧的资源（[TTY 揭秘](https://www.linusakesson.net/programming/tty/index.php)），它尽可能的澄清了 Linux 和 UNIX 系统中的 TTY 的各种技术细节。


事实上，每当你在系统中启动一个终端仿真器或使用任何类型的 shell 时，它都会与虚拟 TTY（也被称为<ruby> 伪 TTY <rt>  pseudo-TTY </rt></ruby>，即 PTY）进行交互。


你可以在终端仿真器中输入 `tty` 来找到相关联的 PTY。


### 如何在 Linux 中访问 TTY？


![](/data/attachment/album/202112/18/155152yzs5e7srkr4sc6xr.png)


在 Linux 中很容易访问 TTY。事实上，当我不知道它是什么时，我不小心打开了它，于是对要做什么、如何摆脱它感到恐慌。


在大多数 *发行版* 中，你可以使用以下键盘快捷键来得到 TTY 屏幕：


* `CTRL + ALT + F1` – 锁屏
* `CTRL + ALT + F2` – 桌面环境
* `CTRL + ALT + F3` – TTY3
* `CTRL + ALT + F4` – TTY4
* `CTRL + ALT + F5` – TTY5
* `CTRL + ALT + F6` – TTY6


你最多可以访问六个 TTY。但是，前两个快捷方式指向发行版的锁定屏幕和桌面环境。


![](/data/attachment/album/202112/18/155152r4728hl30lg1zfab.png)


而其他快捷方式将会让你进入一个命令行界面。


### 什么时候应该使用 TTY？


TTY 不仅是一个技术宝藏，即使像我这样不是开发人员的用户，它也很有用。


在图形桌面环境冻结的情况下，它应该可以派上用场。在某些情况下，从 TTY 重建桌面环境能帮助解决程序问题。


或者，你也可以选择在 TTY 中执行任务，例如更新 Linux 系统等。在这些情况下，你不希望显示问题中断你的进程。


最坏的情况是，如果图形用户界面失去响应，你可以进入 TTY 并重新启动计算机。


有些用户还喜欢在 TTY 的帮助下传输大文件（我不是其中之一）。


### Linux 中的 TTY 命令


![](/data/attachment/album/202112/18/155152wrr4z922wwog8wc0.png)


当你在终端模拟器中输入 `tty` 时，它将打印连接到标准输入的终端文件名，就像手册页描述的一样。


换句话说，要知道你连接的 TTY 编号，只需输入 `tty`。并且，如果有多个用户远程连接到 Linux 机器，你可以使用 `who` 命令来检查其他用户连接到的是哪个 TTY。




---


via: <https://itsfoss.com/what-is-tty-in-linux/>


作者：[Ankush Das](https://itsfoss.com/author/ankush/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[MjSeven](https://github.com/MjSeven) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

You must have heard about the term “TTY” when it comes to Linux and UNIX. But, what is it?

Is it useful to you as a desktop user? Do you need it? And, what can you do with it?

In this article, let me mention everything essential to get you familiar with the term TTY in Linux.

Do note that there’s no definitive answer to this, but it relates to how input/output devices interacted in the past. So, you will have to know a bit of history to get a clear picture.

## History Behind the Term ‘TTY’

It all starts with a Teleprinter in the 1830s.

Teleprinters let you send/receive text messages over the wire. It was a replacement to Morse code communication, where two operators were needed to effectively communicate with one another.

And, a Teleprinter just needed a single operator to easily convey a message. While it did not have a modern-layout keyboard, its system was later evolved by Donald Murray in 1901 to include a typewriter-like keyboard.

The Murray code reduced the effort for operators to send a message. And, this made it possible for a Teleprinter to evolve as a commercial Teletypewriter in 1908. TTY is shorthand for Teletypewriter.

![wacsoperateteletype](https://itsfoss.com/content/images/wordpress/2021/12/WACsOperateTeletype.jpg)

[Wikimedia](https://commons.wikimedia.org/wiki/File:WACsOperateTeletype.jpg?ref=itsfoss.com)

The difference between the Teletypewriter and a regular typewriter was that a Teletypewriter was attached to a communication device to send the typed message.

[Teletypewriter made it possible for humans to communicate faster](https://en.wikipedia.org/wiki/Teletype_Corporation?ref=itsfoss.com#/media/File:What-is-teletype.jpg) over a wire without any computers until now.

And, this is where “**TTY**” came into existence.

## The (relatively) Modern Concept

Now, you must be wondering, how did it land into modern computing and in Linux?

Well, for starters, when Teletypewriter hit the market, some years later semiconductor transistors were developed which then evolved into microprocessors making a computer possible.

Initial computers didn’t have the concept of a keyboard. Punch cards were the input method.

![punch card program](https://itsfoss.com/content/images/wordpress/2021/12/punch-card-program.jpg)

[Image credit](https://www.computerhistory.org/revolution/punched-cards/2/intro/12?ref=itsfoss.com)

While computers were evolving, batch input cards were eventually replaced by Teletypewriters as a convenient input/output device.

![lgp 30 early computer 1956](https://itsfoss.com/content/images/wordpress/2021/12/LGP-30-early-computer-1956.jpg)

With technological advancements, the Teletypewriters were ‘virtualized’ using electronics. So, you wouldn’t need a physical, mechanical TTY, but a virtual, electronic TTY.

Earlier computers didn’t even have video screens. Things were printed on paper instead of displaying on a screen (which didn’t exist). And hence you see the use of the term ‘print’, not ‘display’. Videos were added to terminals later as technology advanced.

In other words, you might have heard of them as video terminals. Or, you could call them “physical” terminals.

And, then these evolved into software emulated terminals which came with enhanced abilities and features.

This is what you call a “terminal emulator”. For instance, GNOME Terminal, or Konsole, are some of the [best terminal emulators that you will find for Linux](https://itsfoss.com/linux-terminal-emulators/).

## So, What is TTY in Linux?

When it comes to Linux, TTY is an abstract device in UNIX and Linux. Sometimes it refers to a physical input device such as a serial port, and sometimes it refers to a virtual TTY where it allows users to interact with the system ([reference](https://unix.stackexchange.com/questions/4126/what-is-the-exact-difference-between-a-terminal-a-shell-a-tty-and-a-con?ref=itsfoss.com)).

TTY is a subsystem in Linux and Unix that makes process management, line editing, and session management possible at the kernel level through TTY drivers.

In terms of programming, you need to dive in deep. But, considering the scope of this article, this could be an easy definition to digest.

If you are curious, you can explore an old resource ([TTY Demystified](https://www.linusakesson.net/programming/tty/index.php?ref=itsfoss.com)) that tries to clear up TTY in Linux and Unix systems with all the technical details you need.

In fact, whenever you launch a terminal emulator or use any kind of shell in your system, it interacts with virtual TTYs that are known as pseudo-TTYs or PTY.

You can just type in TTY in your terminal emulator to find the associated PTY.

## How to Access TTY in Linux?

![ubuntu tty](https://itsfoss.com/content/images/wordpress/2021/11/ubuntu-tty.png)

It is easy to access TTY in Linux. In fact, when I did not have a clue what it was, I accidentally accessed it and panicked about what to do (how to get out from it).

You can get the TTY screen by using the following keyboard shortcuts on * most distributions*:

**CTRL + ALT + F1** – Lockscreen**CTRL + ALT + F2** – Desktop Environment**CTRL + ALT + F3** – TTY3**CTRL + ALT + F4** – TTY4**CTRL + ALT + F5** – TT5**CTRL + ALT + F6** – TTY6

You can access up to six TTYs in total. However, the first two shortcuts point to the distribution’s lock screen and the desktop environment.

![ubuntu tty4](https://itsfoss.com/content/images/wordpress/2021/11/ubuntu-tty4.png)

So, you get a command-line interface with the rest of the shortcuts.

## When Would You Use TTY in Linux?

TTY is not just a technical treasure. It is useful even for users like me who aren’t developers.

It should come in handy in case the graphical desktop environment freezes. In some cases, reinstalling the desktop environment from the TTY helps resolve the program.

Or, you can also choose to carry out tasks in TTY like updating the Linux system and similar, where you do not want visual issues to interrupt your process.

Worst-case scenario, you can go to the TTY and reboot the computer if your graphical user interface is unresponsive.

Some users also prefer to perform large file transfers with the help of TTY (I am not one of them).

## TTY as a Command in Linux

![ubuntu terminal tty command](https://itsfoss.com/content/images/wordpress/2021/11/ubuntu-terminal-tty-command.png)

When you type in TTY in your terminal emulator, it will print the file name of the terminal connected to the standard input, as described by the man page.

In other words, to know the TTY number you are connected to, just type in TTY. And, if there are multiple users connected to the Linux machine remotely, you can use the **who** command to check what other users are connected to.