Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

marked as duplicate by Gilles, Renan, phunehehe, jasonwryan, rahmu Oct 11 '12 at 13:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

1 Answer 1

up vote 18 down vote accepted

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)

share|improve this answer
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 '13 at 15:21
Just to add that you can use ControlPersist to timeout the connection so that there is no need to exit. –  Question Overflow Jul 19 '14 at 7:20

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