Linux TTY基本概念之ttys*、tty*、ttyS*、console理解

Linux TTY基本概念之ttys、tty、ttyS*、console理解

echo "abc" > /dev/console 输出到系统控制台(console=ttyS0,表示系统控制台是串口)
echo "abc" > /dev/tty 和下面的相等
echo "abc" > tty 输出到本控制台 (tty命令就是输出本控制台)

在Linux中, tty可分为如下几类:

  - 串行端口终端(serial port terminal): 指使用计算机串行端口连接的终端设备, /dev/ttySn
  
  - 伪终端(pseudo terminal): 通常是通过ssh登陆的终端, /dev/pts/*
  - 控制终端(controlling terminal): 代表当前tty设备 /dev/tty
  - 控制台终端(console):  指计算机的输出设备, 通常是printk信息输出的设备, /dev/ttyn、/dev/console 

查看终端类型:

[root@localhost ~]# ps
    PID TTY          TIME CMD
   4441 pts/0    00:00:00 bash
 233118 pts/0    00:00:00 ps
[root@localhost ~]# ps  T
    PID TTY      STAT   TIME COMMAND
   4441 pts/0    Ss     0:00 -bash
 233119 pts/0    R+     0:00 ps T
[root@localhost ~]# ps  aT
    PID TTY      STAT   TIME COMMAND
   1585 tty1     Ssl+   0:00 /usr/libexec/gdm-wayland-session --register-session dbus-run-session -- gnome-session --autostart /usr/share/gdm/greeter/autostart
   1605 tty1     S+     0:00 dbus-run-session -- gnome-session --autostart /usr/share/gdm/greeter/autostart
   1606 tty1     S+     0:00 dbus-daemon --nofork --print-address 4 --session
   1607 tty1     Sl+    0:00 /usr/libexec/gnome-session-binary --autostart /usr/share/gdm/greeter/autostart
   2042 tty1     Sl+    1:22 /usr/bin/gnome-shell
   3002 tty1     Sl+    0:00 /usr/libexec/at-spi-bus-launcher
   3012 tty1     S+     0:00 /usr/bin/dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf --nofork --print-address 3
   3108 tty1     S+     0:00 /usr/bin/Xwayland :1024 -rootless -noreset -accessx -core -auth /run/user/42/.mutter-Xwaylandauth.6OXQH2 -listenfd 4 -listenfd 5 -displayfd 6 -initfd 7
   3158 tty1     Sl+    0:00 /usr/libexec/xdg-permission-store
   3222 tty1     Sl+    0:00 /usr/bin/gjs /usr/share/gnome-shell/org.gnome.Shell.Notifications
   3224 tty1     Sl+    0:00 /usr/libexec/at-spi2-registryd --use-gnome-session
   3227 tty1     Sl+    0:00 /usr/libexec/gsd-sharing
   3238 tty1     Sl+    0:00 /usr/libexec/gsd-wacom
   3240 tty1     Sl+    0:00 /usr/libexec/gsd-color
   3242 tty1     Sl+    0:00 /usr/libexec/gsd-keyboard
   3243 tty1     Sl+    0:00 /usr/libexec/gsd-print-notifications
   3244 tty1     Sl+    0:00 /usr/libexec/gsd-rfkill
   3246 tty1     Sl+    0:08 /usr/libexec/gsd-smartcard
   3249 tty1     Sl+    0:00 /usr/libexec/gsd-datetime
   3250 tty1     Sl+    0:00 /usr/libexec/gsd-media-keys
   3251 tty1     Sl+    0:00 /usr/libexec/gsd-screensaver-proxy
   3257 tty1     Sl+    0:00 /usr/libexec/gsd-sound
   3259 tty1     Sl+    0:00 /usr/libexec/gsd-a11y-settings
   3263 tty1     Sl+    0:03 /usr/libexec/gsd-housekeeping
   3264 tty1     Sl+    0:00 /usr/libexec/gsd-power
   3374 tty1     Sl+    0:00 /usr/libexec/gsd-printer
   3418 tty1     Sl     0:00 ibus-daemon --panel disable -r --xim
   3426 tty1     Sl     0:00 /usr/libexec/ibus-dconf
   3428 tty1     Sl     0:00 /usr/libexec/ibus-x11 --kill-daemon
   3433 tty1     Sl+    0:00 /usr/libexec/ibus-portal
   3463 tty1     Sl+    0:00 /usr/bin/gjs /usr/share/gnome-shell/org.gnome.ScreenSaver
   3495 tty1     Sl     0:00 /usr/libexec/ibus-engine-simple
   4441 pts/0    Ss     0:00 -bash
   9096 pts/1    Ss+    0:00 -bash
 233121 pts/0    R+     0:00 ps aT

通过上面的终端类型,发送信息,对用的终端就能接到信息:

发送信息:
echo "abc" > /dev/pts/0
echo "abc" > /dev/pts/1
echo "abc" > /dev/pts/
echo "abc" > /dev/pts/1
echo "who are you" > /dev/pts/1

接到信息:
[root@localhost ~]# tty
/dev/pts/1
[root@localhost ~]# abc
[root@localhost ~]# abc

可以通过 toe -a 命令查看系统支持的终端类型,不要奇怪,这是一个挺长的列表。

[root@localhost ~]# toe -a
Eterm-256color  Eterm with xterm 256-colors
Eterm-88color   Eterm with 88 colors
Eterm           Eterm with xterm-style color support (X Window System)
alacritty       alacritty terminal emulator
ansi            ansi/pc-term compatible with color
aterm           AfterStep terminal
bterm           bogl virtual terminal
cygwin          ANSI emulation for Cygwin
cons25          freebsd console (25-line ansi mode)
dumb            80-column dumb tty
eterm           gnu emacs term.el terminal emulation
eterm-color     Emacs term.el terminal emulator term-protocol-version 0.96
gnome           GNOME Terminal
gnome-256color  GNOME Terminal with xterm 256-colors
hurd            The GNU Hurd console server
kitty           KovId's TTY
konsole         KDE console window
konsole-256color        KDE console window with xterm 256-colors
kon             Kanji ON Linux console
linux           linux console
mach            Mach Console
mach-bold       Mach Console with bold instead of underline
mach-color      Mach Console with ANSI color
mach-gnu        GNU Mach
mach-gnu-color  Mach Console with ANSI color
mlterm          multi lingual terminal emulator
mrxvt           multitabbed rxvt
nsterm          AppKit Terminal.app
pcansi          ibm-pc terminal programs claiming to be ansi
putty           PuTTY terminal emulator
putty-256color  PuTTY 0.58 with xterm 256-colors
rxvt            rxvt terminal emulator (X Window System)
rxvt-16color    rxvt with 16 colors like aixterm
rxvt-256color   rxvt 2.7.9 with xterm 256-colors
rxvt-88color    rxvt 2.7.9 with xterm 88-colors
rxvt-basic      rxvt terminal base (X Window System)
rxvt-color      rxvt terminal emulator (X Window System)
rxvt-cygwin     rxvt terminal emulator (X Window System) on cygwin

终端与伪终端的区别

至此我们可以得出这样的结论:现在所说的终端已经不是硬件终端了,而是软件仿真终端(终端模拟软件)。
关于终端和伪终端,可以简单的理解如下:

  • 真正的硬件终端基本上已经看不到了,现在所说的终端、伪终端都是软件仿真终端(即终端模拟软件)
  • 一些连接了键盘和显示器的系统中,我们可以接触到运行在内核态的软件仿真终端(tty1-tty6)
  • 通过 SSH 等方式建立的连接中使用的都是伪终端
  • 伪终端是运行在用户态的软件仿真终端

总结

通过本文我们可以了解到,真正的硬件终端基本上已经看不到了。在一些连接了键盘和显示器的系统中(当然也包括一些 vsphere 等虚拟环境),我们可以接触到运行在内核态的软件仿真终端。而我们使用最多的则是伪终端。

posted @ 2024-01-22 10:05  寻梦99  阅读(521)  评论(0)    收藏  举报