Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have access to 3 machines, A, B, and C. The only possible (ssh) connections are:

A -> B
B <-> C

I need to get files from A to C, so I could scp the files from A to B, and then scp them from B to C. However, B doesn't have much disk space, so this is not an option. Is there a way to scp files from A to C via B? Note, I don't have root access on any of the machines, so don't think I can set up any persistent tunnels, but correct me if I'm wrong!

share|improve this question
I know this doesn't answer the question, but for those who don't know about rsync, or don't know how to use it to hop through a host, this could be a useful tip: use the '-e' option with rsync like this: A$ rsync <options> -e 'ssh B ssh' source C:destination – Eddified Apr 1 '14 at 21:44
up vote 34 down vote accepted


Updated to include -W from other answers:

A$ scp -oProxyCommand="ssh -W %h:%p B" thefile C:destination

The "-W" is a rather recent feature. If B has a very old SSH client installed, use alternatives:

A$ scp -oProxyCommand="ssh B socat stdio tcp:%h:%p" thefile C:destination
A$ scp -oProxyCommand="ssh B nc %h %p" thefile C:destination


A$ tar cf - thefile anotherfile | ssh B "ssh C \"cd destination && tar xvf -\""


A$ ssh B "ssh C \"cd destination && cat > thefile\"" < thefile

"Tunnel" through B

A$ ssh -fNML 4567:C:22 B
A$ scp -P 4567 thefile localhost:destination

When you're done, don't forget to kill the previously started ssh process (which has dropped to background due to -fN).

Reverse "tunnel" through B to A

Doesn't always work though:

A$ ssh -fN -R 4567:localhost:22 B
(now you can reach A from B, by using localhost:4567)
B$ scp -P 4567 localhost:thefile C:destination
share|improve this answer
Thanks! I used the second option without any issues :) – astrofrog May 10 '11 at 16:43
Thanks so much for these examples @grawity. One question - is it possible to invert `tar c thefile anotherfile | ssh B "ssh C \"cd destination && tar xv\""´ in order to copy download from C to A (being on A) – dmeu Oct 8 '15 at 9:04
@dmeu: Yeah, it's possible. – grawity Oct 8 '15 at 13:14

Versions of scp from early 2011 and later may have a "-3" option:

 -3      Copies between two remote hosts are transferred through the local
         host.  Without this option the data is copied directly between
         the two remote hosts.  Note that this option disables the
         progress meter.

If you have this you can just run:

B$ scp -3 A:file C:file
share|improve this answer
thanks. this is new to me and helped solve my problem – Sharuzzaman Ahmat Raslan Apr 15 at 15:19
ssh -L 4321:hostC:22 youruser@hostB

in another shell:

scp -P 4321 localfile youruser@

This is using port forwarding. The only limitation here is host B needs to be configured to allow port forwarding. Otherwise this should work fine.

In the way of explanation, -L and -R allow you to forward ports. In -L, the first port given is the port ssh will begin listening on the originating machine (host A), and it'll forward anything it receives on that port over your SSH connection to host B, then route to host C on port 22.


I messed up the syntax slightly. It sets up a forward on your LOCAL machine.

share|improve this answer
@astrofrog - if one of our answers satisfies your needs, you should probably accept one of them. – Brian Vandenberg May 2 '11 at 22:28

Nearly all have been already said but here is my last penny: I use ProxyCommand variant without nc nor soc. Based on OpenSSH Proxies and Jumphost Cookbook I crafted a following configuration:

  1. So we have following players:

    • HOME_HOST: it is from where we copy a file to the target host
    • HOP_HOST: we copy through this host (logged as HOP_USER)
    • TARGET_HOST: it is our destination (authenticated as TARGET_USER)
  2. First I added my local public key from my home host .ssh/ to .ssh/authorized_keys at both hop and target hosts. Yes, the same public key from the home host to both of them. Usually you would expect it is the HOP public key you have to add to the TARGET one.

  3. Then I tweaked .ssh/config a little by adding following entry:

       User TARGET_USER
       ProxyCommand ssh -W %h:%p HOP_USER@HOP_HOST
  4. After that the copy operation is as simple as: scp FILE TARGET_HOST:. It displays double banners from both the hop and target nodes but it works.

Of course you may use above to ssh directly to the target: ssh TARGET_HOST. It works with scp and ssh.

share|improve this answer

You must log in to answer this question.

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