36

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.

9000
  • 1,639

1 Answers1

51

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'" ...

Then

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 '13 at 15:21
  • 4
    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 07: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 06 '15 at 01:06