SSH Command Execution Hangs, although interactive shell functions fine
Asked Answered
K

7

31

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.

Kiushu answered 8/5, 2011 at 18:55 Comment(3)
I don't know the answer to your question, but I have an idea. Maybe there is a misconfiguration in your SSH client or on the SSH server. Try a different client to the same server, and then try the same client to a different server, and let's see which one works.Rehearing
Some similar problems have been previously posted on stackoverflow: google.com/search?q=ssh+command+execution+hangsGlobate
It is not an SSH configuration issue - it fails from different clients. The server configuration is locked for me, but verified by other users. The other problems posted here are not exactly the same, I've looked through them.Kiushu
K
33

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.

Kiushu answered 9/5, 2011 at 10:30 Comment(2)
.bashrc = called whenever the shell is invoked and attached to a terminal. .bash_profile is only invoked on login. The distinction is vague, but easily checked by echo 'echo I am logged in' > ~/.bash_profile and executing a command using ssh. That way you can see if direct command execution over ssh does or does not make the shell a login shell.Polished
I was running into the exact same problem using oh-my-zsh, I had to insert [ -z "$PS1" ] && return right before source $ZSH/oh-my-zsh.sh to avoid SSH hanging when using another host as a proxyNap
L
7

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
Lita answered 20/11, 2018 at 17:19 Comment(0)
P
4

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.

Polished answered 8/5, 2011 at 19:1 Comment(0)
O
3

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.

Overflow answered 14/11, 2014 at 19:15 Comment(0)
N
2

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.

Nanananak answered 8/5, 2011 at 19:0 Comment(0)
I
2

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

Iaria answered 17/7, 2015 at 18:33 Comment(0)
P
0

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

Protectorate answered 20/11, 2019 at 18:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.