[Python-Dev] SSH connection sharing

Oleg Broytmann phd at phd.pp.ru
Fri May 16 14:40:52 CEST 2008

Hello! I would like to share a useful tip on how to speedup many short ssh
(scp, rsync, svn over ssh) sessions (on Unix). Sorry if this information is
widely known; I learned the trick a week ago and I am pretty happy with it.

OpenSSH4 on Unix allows connection sharing - utilizing one authenticated
TCP connection for a number of SSH sessions. The advantage is that
subsequent connections don't need to open a new TCP connections and don't
need to pass the authentication phase, thus they initialize a session much
faster. This is very useful for running a lot of short ssh sessions (scp,
sftp, rsync, svn over ssh).

Detailed docs are in man ssh_config; see options ControlMaster and
ControlPath. There is a lot of information_ on the web.

.. _information: http://www.google.com/search?hl=en&ie=utf-8&oe=utf-8&q=ssh%20ControlPath

Add the following to your ~/.ssh/config::

   ControlMaster auto
   ControlPath ~/.ssh/.%r@%h:%p

*ControlMaster auto* means the first client becomes the master, it opens
TCP connection, authenticates and opens a unix socket named in ControlPath.
Subsequent clients use the unix socket to connect to the server via the
already established connection. In case the user tries to close the first
(master) client it will wait until all sessions are finished; in case the
user kills the master, it kills all its shared sessions.

I use all 3 recommended macros, because I use ssh/scp/sftp/rsync/svn to
connect to a number of servers.

Drawbacks: the subsequent connections inherit some attributes of the
master; agent forwarding, e.g. If ssh (or the OS, or even the entire
computer) crashes one needs to remove unix sockets manually; to remedy this
I changed ControlPath to ~/.ssh/controls/%r\@%h:%p and added to my

   @reboot rm -f .ssh/controls/*

     Oleg Broytmann            http://phd.pp.ru/            phd at phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.

More information about the Python-Dev mailing list