7

When running the same ssh command with -T and -t, any stderr output arrives on stderr vs. stdout, respectively.

No pty allocated: ssh -T user@host "echo >&2 foo" 1>/tmp/out 2>/tmp/err

Output is written to /tmp/err.

With pty allocation: ssh -t user@host "echo >&2 foo" 1>/tmp/out 2>/tmp/err

Output is now written to /tmp/out.

I somewhat understand that with pty a full pseudo screen is simulated and that the output is in raw mode. The output sent to the screen then are sent via stdout back to ssh and ssh's tty is also set to raw mode. Can somebody please explain it further?

gabor
  • 400
  • 3
  • 12

1 Answers1

15

A tty does not have separate output and error channels. There is only one output channel; whatever you write to it simply goes to the CRT, serial port, terminal window, modem, printer, or whatever is connected to the tty.

When allocating a tty for running a command, ssh could in theory attach the command's stdin and stdout to the tty, while in contrast attaching the command's stderr to a separate stderr channel (a pipe) that is completely separate from the tty. However, this does not agree with the convention that a command running on a tty should have all 3 of its stdio channels connected to that same tty, and some commands might be confused or behave differently. So ssh chooses to follow convention.

When not using a tty, ssh is free to attach the command's stdin, stdout, and stderr to 3 separate unidirectional pipes.

Celada
  • 21,627
  • 4
  • 64
  • 78
  • That's what I was thinking, thanks for the explanation. So stdout and stderr are sent to the tty. I read somewhere that when this happens and the calling ssh has a tty (pty), then it is put into raw mode. I no longer remember all the details about cooked vs. raw mode. Anybody can elucidate further? – gabor May 30 '13 at 21:43
  • 2
    Yes, the local tty (on the client) is put into raw mode, because the remote pty (the one that ssh allocates for the comment) is in cooked mode and you only need/want one of the two to do cooked processing... or if an app running on the remote system purposefully puts the (remote) tty into raw mode, it's because it's a curses/full screen/whatever app that *expects* raw mode all the way down to the user. So one way or the other the local tty ought to be raw. – Celada May 30 '13 at 21:48