Title here
Summary here
早期的终端是一种独立的硬件设备,用于连接到主计算机或服务器。它们通常包括:
例如:早期的 Teletype (tty) 终端、DEC VT100 终端。
现代终端更多是以软件形式存在的,即终端模拟器(Terminal Emulator), 它们在现代操作系统中(如Linux、macOS、Windows)作为应用程序运行,模拟早期硬件终端的功能。
终端模拟器 | 描述 |
---|---|
iTerm2 | macOS 系统上的高级终端模拟器,提供了丰富的功能,如分屏、标签页、搜索等。 |
GNOME Terminal | 常见于 Linux 系统的默认终端模拟器,功能强大且易于使用。 |
PuTTY | Windows 系统上广泛使用的 SSH 客户端,适用于远程连接和管理。 |
Windows Terminal | Windows 系统上的现代终端模拟器,支持多个命令行环境,如 PowerShell、CMD、WSL。 |
虽然 Teletype 设备现在已经不再使用了,但它的概念和功能依然保留在 Linux 中。
可以看到它的文件类型是 c
(字符设备),代表当前进程所连接的终端设备。
ls -l /dev/tty
crw-rw-rw- 1 root tty 5, 0 Aug 7 22:33 /dev/tty
它的权限是 666
,主设备号是 5
,次设备号是 0
,这就是我们常常看到的标准输入/输出。
echo "Hello, World" > /dev/tty
read -p "Enter your name: " name < /dev/tty
echo "Hello, $name"
PTS 是伪终端的简写(Pseudo-Terminal),/dev/pts
是一个目录,用于存放伪终端设备文件。
ls -l /dev/pts/
crw------- 1 root tty 136, 0 Aug 8 09:52 0
c--------- 1 root root 5, 2 Aug 7 21:30 ptmx
列出 /dev/pts
目录的文件,一般情况下会看到两类文件。
ssh
会话对应一个数字。在命令行输入 tty 命令可以看到当前连接的伪终端设备文件。
tty
/dev/pts/0
man tty
tty - print the file name of the terminal connected to standard input
最常用的方法是 w
命令。
w
11:18:19 up 15 days, 20:08, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 120.231.138.130 10:59 3.00s 0.02s 0.00s w
root pts/1 120.231.138.130 11:11 6:41 0.01s 0.01s -bash
还可以使用 who
命令。
who
root pts/0 2024-08-04 10:59 (120.231.138.130)
root pts/1 2024-08-04 11:11 (120.231.138.130)
当然你要直接用 ls
也行。
ls -l /dev/pts/
crw------- 1 root tty 136, 0 Aug 8 2024 0
crw------- 1 root tty 136, 1 Aug 8 09:53 1
c--------- 1 root root 5, 2 Aug 7 21:30 ptmx
echo "hello, world" > /dev/pts/1
ps -t /dev/pts/1
kill -9 PID