SSH Command Execution Hangs, although interactive shell functions fine

前端 未结 7 1608
庸人自扰
庸人自扰 2020-12-29 23:03

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.<

相关标签:
7条回答
  • 2020-12-29 23:09

    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 an exec (in this case to zsh - because our system doesn't allow chsh to zsh).

    The offending line:

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

    Solved by first checking for interactive shell and exiting if so:

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

    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.

    0 讨论(0)
  • 2020-12-29 23:10

    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.

    0 讨论(0)
  • 2020-12-29 23:11

    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.

    0 讨论(0)
  • 2020-12-29 23:12

    I eventually found the "$-" var which works for me:

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

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

    0 讨论(0)
  • 2020-12-29 23:19

    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 with RequestTTY force for the host that I was trying to copy to.

    0 讨论(0)
  • 2020-12-29 23:29

    We fixed this by adding adding -n (to redirect std in from /dev/null) and -t (force pseudo-tty allocation)

    Example:

    ssh -t -n user@host command
    
    0 讨论(0)
提交回复
热议问题