SSH 内的 SSH 失败并显示“stdin: is not a tty”

Jho*_*han 67 command-line ssh terminal

我正在尝试使用 ssh 连接到第一台机器,然后使用 ssh 连接到另一台机器 2,但出现此错误。

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

stdin: is not a tty
Run Code Online (Sandbox Code Playgroud)

为什么?

mrb*_*mrb 81

默认情况下,当您使用 ssh 在远程机器上运行命令时,不会为远程会话分配TTY。这使您可以传输二进制数据等,而无需处理 TTY 怪癖。这是为在 上执行的命令提供的环境computerone

但是,当您在没有远程命令的情况下运行 ssh 时,它确实会分配一个 TTY,因为您可能正在运行一个 shell 会话。这是ssh otheruser@computertwo.com命令所期望的,但由于前面的解释,该命令没有可用的 TTY。

如果您想要一个 shell on computertwo,请改用它,这将在远程执行期间强制 TTY 分配:

ssh -t user@computerone.com 'ssh otheruser@computertwo.com'
Run Code Online (Sandbox Code Playgroud)

当您最终在 ssh 链的末端运行 shell 或其他交互式进程时,这通常是合适的。如果您要传输数据,则添加 既不合适也不必需-t,但是每个 ssh 命令都将包含一个数据生成或消耗命令,例如:

ssh user@computerone.com 'ssh otheruser@computertwo.com "cat /boot/vmlinuz"'
Run Code Online (Sandbox Code Playgroud)


Gil*_*il' 11

有一个更好的方法来使用 SSH 作为中继:使用ProxyCommand选项。您需要在客户端计算机上拥有一个密钥,以便您可以登录到第二台计算机(无论如何,在大多数情况下,推荐使用公钥的方式是使用 SSH)。把它放在你的~/.ssh/config运行中ssh computertwo

Host computerone
HostName computerone.com
User user

Host computertwo
HostName computertwo.com
User otheruser
ProxyCommand ssh computerone exec nc %h %p
Run Code Online (Sandbox Code Playgroud)

nc是网。可用的几个版本中的任何一个都可以。


小智 8

它期望在中间服务器上的 tty 设备上有一个交互式终端。

如果你尝试这个命令,它应该可以工作:

ssh user@computer1 -t "ssh otheruser@computer2"
Run Code Online (Sandbox Code Playgroud)

请参阅man ssh-t选项。


小智 6

我通过将 RequestTTY Yes 添加到位于 ~/.ssh/config 的 ssh 配置文件中解决了这个问题...

Host myserver.com
  User my-ssh-username
  RequestTTY Yes
Run Code Online (Sandbox Code Playgroud)