The following command is working as expected.

ssh [email protected] "ssh [email protected] 'cat test.txt'" > /home/shantanu/test.txt

What I need to do is to copy the entire directory instead of a single file.

Is it possible to use rync with SSH tunneling?

4 Answers 4


You could pipe tar.


ssh [email protected] "ssh [email protected] 'tar cf - test_directory'" | tar xf -

You can use scp to copy single or multiple files and also use rsync with an ssh transport:

scp -r localdirectoryname username@hostname:/remotepath


rsync -av localdirectory username@hostname:/remotepath 

Both programs also work the other way round, with the remote part as the origin and the local as the destination.

See man scp and man rsync.


If you indeed need an intermediate server, you could use ssh port forwarding:

In one shell, use this command to establish a port forwarding:

ssh -NL 10022: [email protected]

This connects port 10022 on your local machine with port 22 on, but won't open a shell, instead just blocks until you terminate it.

Afterwards, on another shell/xterm, you can use either

scp -r -P 10022 localpath soak@localhost:/remotepath 

but have to be aware that soak@localhost actually points to [email protected] when entering your credentials.

Edit 2, now featuring rsync

As you specifically asked about rsync, here is how to use rsync instead of scp. It requires the same port forwarding enabled as the scp variant:

rsync -av --rsh="ssh -p 10022" localpath soak@localhost:/remotepath 

and again remember that you are actually connecting to [email protected].

  • Does it connect to the intermediate server before copying data to remote host?
    – shantanuo
    Commented Sep 28, 2011 at 7:27
  • Maybe you should describe your requirements a bit better, especially why you need an intermediate server. Anyway, see my edit.
    – Sven
    Commented Sep 28, 2011 at 7:48

Another way is use SSH Port Forwarding.

First, run the following command on your computer:

$ ssh -N -f -L 2302: [email protected]

This allocated a socket to listen to port 2302 on localhost. Whenever a connection connect to this port, it will be forwarded to

So you can copy a folder on to your machine with:

$ scp -r -P 2302 soak@localhost:/path/to/folder /path/to/destination/dir

Also available is ssh's ProxyCommand.

From Undeadly:

Host internal.hostname.tld internal
  User          merdely
  HostName      internal.hostname.tld
  ProxyCommand  ssh [email protected] nc %h %p 2> /dev/null

You must log in to answer this question.

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