67

I'm trying to connect to machine one with ssh and then connect to another machine two with ssh, but I get this error.

ssh user@computerone.com 'ssh otheruser@computertwo.com'

stdin: is not a tty

Why?

1
  • 1
    I don't think you need those single quotes around the second ssh!
    – coffeMug
    May 9, 2016 at 16:19

7 Answers 7

82

By default, when you run a command on the remote machine using ssh, a TTY is not allocated for the remote session. This lets you transfer binary data, etc. without having to deal with TTY quirks. This is the environment provided for the command executed on computerone.

However, when you run ssh without a remote command, it DOES allocate a TTY, because you are likely to be running a shell session. This is expected by the ssh otheruser@computertwo.com command, but because of the previous explanation, there is no TTY available to that command.

If you want a shell on computertwo, use this instead, which will force TTY allocation during remote execution:

ssh -t user@computerone.com 'ssh otheruser@computertwo.com'

This is typically appropriate when you are eventually running a shell or other interactive process at the end of the ssh chain. If you were going to transfer data, it is neither appropriate nor required to add -t, but then every ssh command would contain a data-producing or -consuming command, like:

ssh user@computerone.com 'ssh otheruser@computertwo.com "cat /boot/vmlinuz"'
1
  • If you were going to transfer data, it is neither appropriate nor required to add -t.Should not transfer files by scp other than ssh?
    – John
    Jun 15 at 1:40
11

There's a better way to use SSH as a relay: use the ProxyCommand option. You'll need to have a key on the client machine that lets you log in into the second computer (public key is the recommended way of using SSH in most circumstances anyway). Put this in your ~/.ssh/config and run ssh computertwo.

Host computerone
HostName computerone.com
User user

Host computertwo
HostName computertwo.com
User otheruser
ProxyCommand ssh computerone exec nc %h %p

nc is netcat. Any of the several versions available will do.

2
  • Aren't we required to add a "proxy"-related line into 'authorized_keys' on computerone? I keep meaning to look into the 'proxy' power of openSSH, but just haven't gotten around to it yet. Jun 26, 2014 at 5:07
  • I was getting prompted by a ~/.ssh/config error on UserName. Turns out nowadays it's just User user instead.
    – Yamaneko
    Feb 25, 2020 at 15:28
8

It's expecting an interactive terminal on a tty device on the intermediate server.

If you try this command, it should work:

ssh user@computer1 -t "ssh otheruser@computer2"

See man ssh for the -t option.

5

I solved this by adding RequestTTY Yes to my ssh config file located at ~/.ssh/config like this...

Host myserver.com
  User my-ssh-username
  RequestTTY Yes
1
  • Yes, this works around the problem. However, it also changes the behavior of the first ssh connection in some aspects, for example when sending an interrupt (Ctrl-C).
    – hagello
    Aug 6, 2021 at 8:47
2

You can use PROXY Jump option in ssh

-J [user@]host[:port]
 Connect to the target host by first making a ssh connection to the jump host and then establishing a TCP forwarding to the ultimate destination from there.  Multiple jump hops may be specified
 separated by comma characters.  This is a shortcut to specify a ProxyJump configuration directive.

So if I need to connect to hostB but I have to go through hostA first to get there. Normally I would

 ssh hostA
 [user@hostA ~]$ ssh hostB

I now do this

ssh -J hostA hostB
[user@hostB ~]$

You could even , separate multiple hosts

ssh -J hostA,hostB hostC
[user@hostC]

Much simpler than trying -t to run ssh command again.

0

You can override an SSH config option "RequestTTY" from the command line.

My working example cd-serv-one.sh starts /bin/bash in SSH session after running multiple commands:

#!/bin/bash

ssh -o "requestTTY=yes" User@ExampleHostName "cd /home/myPathFoo/myPathBar; /bin/bash"

And now I just run ./cd-serv-one.sh for starting a needed SSH session.

0

You should fix the rc files on computerone! One of them (probably .bashrc) contains mesg y or mesg n without first checking whether stdin is a tty.

You should replace mesg n by [ -t 0 ] && mesg y.

References: man 1 test 0 is the file number of stdin.

Background: While ssh <host> does allocate a pseudo tty on , ssh <host> <cmd> (without option -l) does not. Still, some programs (mesg, editors, password prompts) need a terminal (or a pseudo terminal).

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.