How to reuse an ssh connection

ssh

Solution

If you open the first connection with `-M`:

ssh -M $REMOTEHOST

subsequent connections to `$REMOTEHOST` will "piggyback" on the connection established by the master `ssh`. Most noticeably, further authentication is not required. See `man ssh_config` under "ControlMaster" for more details. Use `-S` to specify the path to the shared socket; I'm not sure what the default is, because I configure connection sharing using the configuration file instead.

In my `.ssh/config` file, I have the following lines:

host *
  ControlMaster auto
  ControlPath ~/.ssh/ssh_mux_%h_%p_%r

This way, I don't have to remember to use `-M` or `-S`; `ssh` figures out if a sharable connection already exists for the host/port/username combination and uses that if possible.

This option is available in OpenSSH since 2004.

Problem

I'm creating a small script to update some remote servers (2+) I am making multiple connects to each server; is there a way I can reuse the SSH connections so I don't have to open too many at once?

Original source