So let's say you boot up your Linux install all the way to the desktop. You start up a gnome-terminal/konsole/whatever so you have a tty to enter commands to.

Now let's say I SSH into that same machine. It will bind me to another tty to enter commands to.

Now let's say I want to "switch" my tty from my original SSH one to the gnome-terminal one started earlier.

Basically I'm asking if there is any way to do the same thing screen -x does but without screen?

I know you can easily send output to the other tty simply by echoing something into the /dev file, but I don't know a way to 'view' what's in the tty.

Any ideas?


Maybe these schema can clarify the situation. This is the usual setting:

                               Terminal  (/dev/ttyX or /dev/pts/x)
                    (screen)<--[<output]----x-------(stdout) Process1
        Terminal  (keyboard)---[input >]---o-\----->(stdin)
                                            \ \
(hardware console or                         \ `----(stdout) Process2
 virtual console or terminal                  `---->(stdin)
 emulators like xterm, …)

And there is no way to plug some new Process3 like this:

             (screen)<---o---[<output]--x------(stdout) Process1
  Terminal (keyboard)---/-x--[input >]-o-\---->(stdin)
                       | /              \ \
                       | |               \ `---(stdout) Process2
                       | |                `--->(stdin)
                       | |
                       \ `---------------------(stdout) Process3

What screen (and others) does is allocating some pseudo terminal device (like xterm does) and redirect it to one or more "real" terminals (physical, virtual, or emulated):

             Terminal                   pseudo
             devices              ,--> Terminal (/dev/pts/x)
                |         _______/      device
Terminal <--[<output]--- |       |        |
 1       ---[input >]--> |screen | <--[<output]---x-----(stdout) Process1
                         |Process| ---[input >]--o-\--->(stdin)
Terminal <--[<output]--- |       |                \ \
 2       ---[input >]--> |_______|                 \ `--(stdout) Process2

Using screen -x you can attach one more terminal, xterm, whatever (say Terminal 3) to the screen session.

So no, you can't communicate directly through stdin/stdout with processes attached to a different terminal. You can only do so through the process that is controlling this terminal if it happens to be a pseudo terminal, and if this process was concieved to do so (like screen is).

  • That's a great explanation! I think it's not really an answer here, but would suit this question well. One observation: “there is no way” is only true if you forbid ptrace; thanks to ptrace, programs like neercs, retty and so on can do it sometimes. Aug 2 '11 at 22:43
  • @Gilles: well they do something else: they change the value of the file descriptors (stdin,stdout on the right of the schema) directly by hijacking the process. Pure evil! Aug 2 '11 at 22:47
  • 1
    Hmm, you're right, they change a different arrow. Evil, but convenient! Aug 2 '11 at 22:49

Reconnecting the processes on the other terminal to your current terminal is not possible without dirty tricks. It is possible by forcing the process to perform certain system calls (with ptrace); this causes some programs to crash. There are several tools that do this, such as neercs, retty, cryopid, reptyr, …; see How can I disown it a running process and associate it to a new screen shell? and linked questions.

Obtaining the output already displayed on the other terminal is a different problem. There is no fully general solution: in principle, once the output has been rendered, the terminal might store it only as an image. In practice, all X terminal emulators keep their output buffer in text form so that you can copy-paste it. The way to grab that output depends on the terminal emulator; the basic idea is to simulate selecting the whole scrollback buffer. For a Linux console such as /dev/tty1, the scrollback buffer is easily available as /dev/vcs1 (and /dev/vcsa1 with text attributes).

The simple answer is that if you want to reconnect to a terminal from a different place, use screen or tmux.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.