The ssh commands only execute ones from local system.

for eg :

ssh myuser@mydomain.com df -h

This worked, and then after this, the ssh protocol got hanged. It stays hang for around 10 mins and then again gets normal again. The error message that I get is this.

ssh: connect to host mydomain.com port 22: Operation timed out

  • 1
    Are you connecting from a mac to an ubuntu server by chance? I've found that I have to kill the ssh-agent process sometimes if my connection times out. Also there are a few config options in /etc/ssh/sshd_config that may be causing the hangup. LoginGraceTime, ClientAliveInterval, MaxStartups, or TCPKeepAlive. TCPKeepAlive sounds likely, as if it is set to yes, sessions can hang on the server even after your client has disconnected. I would have to look at the config to be able to diagnose it further. – blanket_cat Apr 18 '17 at 11:48
  • 1
    You are very much right brother. I am trying to connect from mac to ubuntu server. All the commands and everything works fine if I manually ssh into server first and the execute the commands. My PM2 tool for deployment is not working. Any particular option in config file that you can suggest me to look for. – NarayaN Apr 18 '17 at 11:51
  • 2
    Yeah, Mac starts ssh-agent automatically when you start a connection, but when the connection drops, it doesn't kill the process, which isn't available to another instance of the same user for security reasons. I don't know what PM2 is. Just kill the ssh-agent process when you're done. Or even use ssh user@host && kill $(pgrep ssh-agent) – blanket_cat Apr 18 '17 at 11:58
  • 2
    Worked beautifully like a charm. I owe you a beer mate. Could you please write this comment as answer so that I can mark it as accepted answer. Thanks alot – NarayaN Apr 18 '17 at 12:06
  • 1
    Totally. add into the question that you're logging in from OS X to indicate that for anyone who needs this answer in the future. :) – blanket_cat Apr 18 '17 at 12:16

The problem is that ssh-agent running on your local machine is hanging, and preventing you from attempting to make another connection. I believe ssh-agent should exit when your ssh connection is terminated and the process ends, but for some reason the OS X implementation hangs. This instance of ssh-agent isn't available to other instances of your user, for security reasons.

The solution, which I believe would apply if this was happening in Ubuntu as well, is simply to kill the ssh-agent process when you're done with your ssh session, before starting another one.

If ssh-agent hanging turns out to be problem, you can automatically end it following your session with:

~$ ssh username@hostname && kill $(pgrep ssh-agent)


That command is if the client you are connecting from has only one user using ssh, if you are making connections from a machine with multiple, concurrent ssh connections from different users. You'll want to use:

~$ ssh username@hostname && kill $(ps -U your_local_username | grep ssh-agent | grep -oP "^\d+")
| improve this answer | |
  • 1
    Worked like a charm. – NarayaN Apr 18 '17 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.