I have a script that executes a Rake remote task which executes once for each of two roles. Both roles are users on the same Vagrant instance. So essentially the task is opening two SSH connections to the same VM in parallel and executing some commands on each connection. I often find that when this task executes, it produces the following error:

ssh_exchange_identification: read: Connection reset by peer

However, this only happens on our Jenkins slave node, which is a Mac Mini. I've come up with a simple example that reproduces the connection reset behavior:

ssh root@vagrant-target "echo hello" & ssh root@vagrant-target "echo bye"

I've also found that executing the above command does not reproduce the error on my local MacBook Pro instance, which has the same Vagrant setup, even if I do it repeatedly.

Both machines have the same OS version, as well as the same version of SSH. I also ran the above ssh commands with -vvv but didn't see anything obvious. The only thing that comes to mind is that I see some sshd processes on the Jenkins machine, presumably used by Jenkins:

jenkins        34394   0.1  0.0  2471700    840   ??  S     6:27PM   0:00.05 /usr/sbin/sshd -i
jenkins        35220   0.0  0.0  2470872   1284   ??  S     6:27PM   0:01.38 /usr/sbin/sshd -i
root           35218   0.0  0.0  2469596   2908   ??  S     6:27PM   0:00.04 /usr/sbin/sshd -i

Does anyone have any ideas as to why this behavior would appear on one machine but not the other?

  • hey, are you log in with ssh key? or with user name and password? – Suku Jan 10 '13 at 3:33
  • I am using an ssh key, not entering a password. – yixu34 Jan 10 '13 at 3:43

Have you tried setting up a control master connection? this will allow you to multiplex several ssh sessions over a single connection.

To setup the control master use

ssh -o "ControlMaster=yes" -o "ControlPath=~/.ssh/%r@%h:%p" root@server

-o "ControlMaster=yes" tells ssh this connection will be the master process. The control path is a socket set up to allow other ssh sessions over this connection.

To make your ssh use the controlpath you just need to use in a seperate terminal.

ssh -o  "ControlPath=~/.ssh/%r@%h:%p" root@server "cmds" & \
ssh -o  "ControlPath=~/.ssh/%r@%h:%p" root@server "cmds"

If this doesn't sort the issue can you post the output of ssh with the -vvv flags?

| improve this answer | |
  • Awesome, this seems to work! Though I did have to make a minor modification as I am running this in a script and therefore cannot open another terminal window. I open the master connection like this: ssh -nM -o ControlPath="~/.ssh/%r@%h:%p" root@vagrant-target "while :; do sleep 1; done" > /dev/null & and then I ssh in using ControlPath as you've described. Thanks! – yixu34 Jan 15 '13 at 1:02
  • 1
    rather than the while sleep loop use -N which is do not execute a remote command and -n redirects stdin from /dev/null. – peteches Jan 15 '13 at 8:52

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.