Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does OpenSSH RequestTTY cause stderr redirected to stdout?

Tags:

unix

openssh

pty

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?

like image 355
gabor Avatar asked May 30 '13 14:05

gabor


1 Answers

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.

like image 112
Celada Avatar answered Sep 23 '22 13:09

Celada