【问题标题】:SSH Command Execution Hangs, although interactive shell functions fineSSH 命令执行挂起,尽管交互式 shell 功能正常
【发布时间】:2019-11-21 02:58:04
【问题描述】:

当我尝试使用 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 &lt;username&gt;@&lt;server&gt;

输出:

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 来同步我的文件(它曾经可以工作)。非常感谢任何帮助。

【问题讨论】:

  • 我不知道你的问题的答案,但我有一个想法。可能是您的 SSH 客户端或 SSH 服务器上的配置错误。在同一个服务器上尝试不同的客户端,然后在不同的服务器上尝试相同的客户端,让我们看看哪个有效。
  • 一些类似的问题之前在stackoverflow上贴过:google.com/search?q=ssh+command+execution+hangs
  • 这不是 SSH 配置问题 - 它从不同的客户端失败。服务器配置已为我锁定,但已由其他用户验证。这里发布的其他问题并不完全相同,我已经查看了它们。

标签: linux ssh remote-execution


【解决方案1】:

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

违规行:

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

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

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

所以,基本上,因为 shell 正在执行到 zsh,ssh 正在等待它完成 - 这从未发生过。

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

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

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

【讨论】:

  • .bashrc = 每当调用 shell 并将其连接到终端时调用。 .bash_profile 仅在登录时调用。区别很模糊,但很容易通过 echo 'echo I am logged in' > ~/.bash_profile 和使用 ssh 执行命令来检查。这样您就可以查看通过 ssh 直接执行命令是否使 shell 成为登录 shell。
  • 我在使用 oh-my-zsh 时遇到了完全相同的问题,我必须在 source $ZSH/oh-my-zsh.sh 之前插入 [ -z "$PS1" ] &amp;&amp; return 以避免在使用另一台主机作为代理时 SSH 挂起
【解决方案2】:

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

例子:

ssh -t -n user@host command

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        检查您的 shell 启动文件中是否存在出于某种原因需要终端的命令(我会在提示符下假设 ~/.cshrc;在非交互式会话中,~/.login 应该无关紧要)。

        【讨论】:

          【解决方案6】:

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

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

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

          【讨论】:

            【解决方案7】:

            我最终找到了适合我的“$-”变量:

            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-03-11
              • 2021-07-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-12-26
              • 1970-01-01
              相关资源
              最近更新 更多