Possible Duplicate:
Using an already established SSH channel

I have a remote server accessible via a chain of SSH proxies. The resulting channel is wide and fast enough, but setting it up takes a couple seconds.

I want to periodically run rsync against this server, synchronizing small changes to a relatively large file tree.

When I do it plainly by rsync -r source_dir remote_host:target_dir, most of the time is spent establishing the connection. For comparison, establishing an interactive connection to that host takes about the same time.

Is there a way to somehow reuse the SSH connection across several rsync invocations, to avoid the connection delay?

The catch is that I don't want continuous replication of files to the remote host; file tree has to be consistent at the moment of the replication, so I'd like to trigger the rsync event myself.

I'm open to using something else, e.g. git, as long as I avoid the constant reconnection delay and don't have to open a listening port on the remote host.

  • Won't running an ssh tunnel in the background do what you want?
    – terdon
    Oct 10, 2012 at 19:04
  • 2
    @terdon: probably it would. I'd appreciate if you composed an answer showing how to use rsync with that.
    – 9000
    Oct 10, 2012 at 19:19

1 Answer 1


Yes, see the ControlMaster and ControlPath options.

You can start the master connection with:

mkdir ~/.ssh/ctl
ssh -nNf -o ControlMaster=yes -o ControlPath="$HOME/.ssh/ctl/%L-%r@%h:%p" user@host

And then use rsync with:

rsync -e "ssh -o 'ControlPath=$HOME/.ssh/ctl/%L-%r@%h:%p'" ...


ssh -O exit -o ControlPath="$HOME/.ssh/ctl/%L-%r@%h:%p" user@host

to terminate the master connection.

(And by the way, some versions of ubuntu have an annoying feature in that they rebuild /etc/motd upon every login (including non-interactive ssh sessions), which can take a long time if part of that involves checking if updates are available for instance. IIRC, you can disable it by removing pam_motd from your pam configuration)

  • 13
    It should be noted that OpenSSH client also supports the -M command-line option whith the same meaning as -o ControlMaster=yes and the -S <path> command-line option with the same meaning as -o ControlPath=<path>. At least this is the case with OpenSSH 5.1p1.
    – kostix
    Jul 16, 2013 at 15:21
  • 4
    Just to add that you can use ControlPersist to timeout the connection so that there is no need to exit. Jul 19, 2014 at 7:20
  • There's no ControlPersist in older versions of ssh but running sleep on the remote host does something similar: e.g. ssh -f -M -S <path> user@host sleep 60 (Except ControlPersist counts idle seconds while this counts from the initial connection.)
    – antak
    Aug 6, 2015 at 1:06

Not the answer you're looking for? Browse other questions tagged .