26

当我尝试使用 ssh 在远程服务器上执行命令时,ssh 命令在exec request accepted调试消息之后挂起,并最终超时。

失败的命令:(ssh -v -v <username>@<server> uptime也尝试过echo hello等)

debug1: Authentication succeeded (publickey).
Authenticated to <server> (<ip>:22).
debug1: channel 0: new [client-session]
debug2: channel 0: send open
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug2: callback start
debug2: client_session2_setup: id 0
debug2: fd 4 setting TCP_NODELAY
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug2: channel 0: request env confirm 0
debug1: Sending command: uptime
debug2: channel 0: request exec confirm 1
debug2: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
debug2: channel 0: rcvd adjust 2097152
debug2: channel_input_status_confirm: type 99 id 0
debug2: exec request accepted on channel 0

它无限期地挂在那里。

但是,当我在没有命令的情况下 ssh 进入远程服务器时,我得到了一个交互式 shell,一切都很好。

成功命令:ssh -v -v <username>@<server>

输出:

debug1: Authentication succeeded (publickey).
Authenticated to <server> (<ip>:22).
debug1: channel 0: new [client-session]
debug2: channel 0: send open
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug2: callback start
debug2: client_session2_setup: id 0
debug2: fd 4 setting TCP_NODELAY
debug2: channel 0: request pty-req confirm 1
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug2: channel 0: request env confirm 0
debug2: channel 0: request shell confirm 1
debug2: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
debug2: channel_input_status_confirm: type 99 id 0
debug2: PTY allocation request accepted on channel 0
debug2: channel 0: rcvd adjust 2097152
debug2: channel_input_status_confirm: type 99 id 0
debug2: shell request accepted on channel 0
Welcome!
<prompt>%
...

有谁知道为什么交互式会话会成功但命令执行不会?

几个月来一直困扰着我,因为我不能再使用 unison 来同步我的文件(它曾经可以工作)。非常感谢任何帮助。

4

7 回答 7

32

问题确实是我的登录脚本,尽管与需要终端无关(我怀疑并使用-tand-T选项进行了测试)。问题是我.bashrc正在运行exec(在这种情况下为- 因为我们的zsh系统不允许)。chshzsh

违规行:

test -f /usr/bin/zsh && exec /usr/bin/zsh

通过首先检查交互式 shell 并退出来解决:

[ -z "$PS1" ] && return
test -f /usr/bin/zsh && exec /usr/bin/zsh

所以,本质上,因为 shell 正在执行到zshssh正在等待这个完成 - 这从未发生过。

我有点困惑,为什么我.bashrc会被调用——我认为这只是用于交互式 shell,但各种初始化脚本的确切目的和顺序是我认为我永远也学不会的东西。

exec我希望这对其他在启动脚本中有某种内容的人有用。

顺便说一句 - 其他两个答案都在正确的轨道上,所以我完全不确定我应该“回答”还是只是评论他们的答案。如果在stackoverflow上回答我自己的问题在道德上是错误的,请告诉我,我会忏悔。谢谢其他回答者。

于 2011-05-09T10:30:16.773 回答
4

您的问题很可能在于您的 shell 启动或 shell 注销脚本。在不知道里面有什么的情况下,很难猜出实际的问题。

于 2011-05-08T19:01:01.730 回答
4

我们通过添加 -n(从 /dev/null 重定向 std in)和 -t(强制伪 tty 分配)来解决此问题

例子:

ssh -t -n user@host command
于 2018-11-20T17:19:53.590 回答
3

我最近遇到了具有相同症状的问题,但确定该问题不是我的登录脚本中的问题。相反,我的本地.ssh/config文件是RequestTTY force为我试图复制到的主机配置的。

于 2014-11-14T19:15:47.483 回答
2

检查您的 shell 启动文件中的命令(我会~/.cshrc根据您的提示进行假设;在非交互式会话中,~/.login这无关紧要)由于某种原因需要终端。

于 2011-05-08T19:00:58.677 回答
2

在解决了其他新问题后,我在 fedora server 22 上遇到了这个问题。

ssh -t ziimp /bin/true 没问题,但 ssh ziimp /bin/true 不行,我所有的 git+ssh 和 scp 都被锁定了。

我找到的解决方案在authorized_keys文件中。我必须从受信任的密钥中删除 command="/usr/bin/bash" 前缀...

于 2015-07-17T18:33:59.880 回答
0

我最终找到了对我有用的“$-”变量:

if [[ $- =~ i ]] ; then
    [ -x /bin/tcsh ] && exec /bin/tcsh
    # Bash startup stuff goes here...
fi

来自: https ://www.gnu.org/software/bash/manual/html_node/Is-this-Shell-Interactive_003f.html

于 2019-11-20T18:58:04.353 回答