SSH命令执行挂起,虽然交互式shell功能很好

Rob*_*uil 23 linux ssh remote-execution

当我尝试使用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
Run Code Online (Sandbox Code Playgroud)

它无限期地悬挂在那里.

然而,当我没有命令进入我的远程服务器时,我得到一个交互式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>%
...
Run Code Online (Sandbox Code Playgroud)

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

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

Rob*_*uil 24

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

违规行:

test -f /usr/bin/zsh && exec /usr/bin/zsh
Run Code Online (Sandbox Code Playgroud)

通过首先检查交互式shell并退出(如果是这样)来解决:

[ -z "$PS1" ] && return
test -f /usr/bin/zsh && exec /usr/bin/zsh
Run Code Online (Sandbox Code Playgroud)

所以,基本上,因为shell正在进行zsh,ssh正在等待这个完成 - 这从未发生过.

我有点困惑为什么我的.bashrc被调用 - 我认为这只适用于交互式shell,但各种init脚本的确切目的和顺序是我认为我不会学习的东西.

我希望这对其他exec在启动脚本中有某种功能的人有用.

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


A.B*_*ger 6

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

例子:

ssh -t -n user@host command
Run Code Online (Sandbox Code Playgroud)