尽管交互式 shell 功能正常,但 SSH 命令执行挂起
当我尝试使用 ssh 在远程服务器上执行命令时,ssh 命令在 exec requestaccepted
调试消息后挂起,并最终超时。
失败的命令:ssh -v -v <用户名>@<服务器>正常运行时间
(也尝试了echo hello
等)
debug1: Authentication succeeded (publickey).
Authenticated to <server> (<ip>:22).
debug1: channel 0: new [client-session]
debug2: channel 0: send open
debug1: Requesting [email protected]
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
输出:
debug1: Authentication succeeded (publickey).
Authenticated to <server> (<ip>:22).
debug1: channel 0: new [client-session]
debug2: channel 0: send open
debug1: Requesting [email protected]
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>%
...
有谁知道为什么交互式会话会成功但命令执行不成功?
几个月来一直困扰着我,因为我无法再使用一致来同步我的文件(它曾经有效)。非常感谢任何帮助。
When I attempt to execute a command on a remote server with ssh, the ssh command hangs after the exec request accepted
debug message, and eventually times out.
The failing command: ssh -v -v <username>@<server> uptime
(also tried echo hello
etc.)
debug1: Authentication succeeded (publickey).
Authenticated to <server> (<ip>:22).
debug1: channel 0: new [client-session]
debug2: channel 0: send open
debug1: Requesting [email protected]
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
And there it hangs, indefinitely.
When I ssh without a command into my remote server, however, I get an interactive shell and all is good.
Successful Command: ssh -v -v <username>@<server>
Output:
debug1: Authentication succeeded (publickey).
Authenticated to <server> (<ip>:22).
debug1: channel 0: new [client-session]
debug2: channel 0: send open
debug1: Requesting [email protected]
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>%
...
Has anyone an idea why an interactive session would be successful but a command execution not?
Has been haunting me for months now because I cannot use unison to sync my files any more (it used to work). Any help much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
问题确实是我的登录脚本,尽管与需要终端无关(我怀疑这一点并使用
-t
和-T
选项进行了测试)。问题是我的.bashrc
正在运行exec
(在本例中是zsh
- 因为我们的系统不允许chsh
到zsh
)。有问题的行:
通过首先检查交互式 shell 并退出(如果有)来解决:
因此,本质上,因为 shell 正在执行到
zsh
,ssh
正在等待此完成 -这从未发生过。我有点困惑为什么我的
.bashrc
被调用 - 我认为这仅适用于交互式 shell,但各种初始化脚本的确切目的和顺序是我不认为的永远都会学习。我希望这对在启动脚本中具有某种
exec
的其他人有用。顺便说一句 - 其他两个答案都在正确的轨道上,所以我完全不确定我是否应该“回答”或只是评论他们的答案。如果在 stackoverflow 上回答我自己的问题在道德上是错误的,请告诉我,我会忏悔。谢谢其他回答者。
The problem was indeed my login script, although not to do with requiring a terminal (I'd suspected that and tested with the
-t
and-T
options). The problem was that my.bashrc
was running anexec
(in this case tozsh
- because our system doesn't allowchsh
tozsh
).The offending line:
Solved by first checking for interactive shell and exiting if so:
So, essentially, because the shell was execing into
zsh
,ssh
was waiting for this to finish - which never happened.I am a little confused why my
.bashrc
was being called at all - I thought this was only for interactive shells, but the exact purpose and order of the various init scripts is something I don't think I'll ever learn.I hope this can be useful to others who have some kind of
exec
in their startup scripts.BTW - the other two answers were on the right track so I was completely unsure if I should 'answer' or just comment their answers. If answering my own question is morally wrong on stackoverflow, let me know and I'll do penitence. Thank you to the other answerers.
我们通过添加 -n (从 /dev/null 重定向 std )和 -t (强制伪 tty 分配)来修复此问题
示例:
We fixed this by adding adding -n (to redirect std in from /dev/null) and -t (force pseudo-tty allocation)
Example:
您的问题很可能出在您的 shell 启动或 shell 注销脚本中。如果不知道里面有什么,就很难猜测实际的问题。
Your problem most likely lies in your shell startup or shell logout scripts. Without knowing what's in there, it's hard to guess the actual problem.
我最近遇到了具有相同症状的问题,但确定该问题不是我的登录脚本中的问题。相反,我的本地
.ssh/config
文件针对我尝试复制到的主机配置了RequestTTY force
。I recently encountered a problem with the same symptoms, but determined that the issue was not a problem in my login scripts. Rather, my local
.ssh/config
file was configured withRequestTTY force
for the host that I was trying to copy to.检查 shell 启动文件中的命令(我假设提示符下有
~/.cshrc
;在非交互式会话中,~/.login
应该不重要)由于某种原因需要终端。Check for commands in your shell startup files (I would assume
~/.cshrc
from your prompt; in a non-interactive session,~/.login
shouldn't matter) that require a terminal for some reason.在解决其他新问题后,我在 Fedora Server 22 上遇到了这个问题。
ssh -t ziimp /bin/true 没问题,但 ssh ziimp /bin/true 不行,我所有的 git+ssh 和 scp 都被锁定。
我找到的解决方案位于 authorized_keys 文件中。我必须从我的可信密钥中删除 command="/usr/bin/bash" 前缀...
I had this problem on fedora server 22, after the resolution of other new problems.
ssh -t ziimp /bin/true was ok but not ssh ziimp /bin/true and all my git+ssh and scp were locked.
The solution i found was in the authorized_keys file. I had to remove the command="/usr/bin/bash" prefix from my trusted keys...
我最终找到了对我有用的“$-”var:
从以下地方得到的:
https://www.gnu.org/软件/bash/manual/html_node/Is-this-Shell-Interactive_003f.html
I eventually found the "$-" var which works for me:
Got this from:
https://www.gnu.org/software/bash/manual/html_node/Is-this-Shell-Interactive_003f.html