Server Fault is a question and answer site for system and network administrators. 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 am trying to copy a file from to the localhost. The only problem is that I need to tunnel the connection through Something like this does not work....

ssh shantanu@ "ssh shantanu@ cp /home/shantanu/test.txt . "

The thread "ssh tunnel via multiple hops" is too confusing.

share|improve this question
That's because the accepted solution is not a very good one. – Ignacio Vazquez-Abrams Jun 8 '11 at 8:41
up vote 1 down vote accepted

Don't use "cp" for copying files between SSH-enabled hosts, use scp. Something like this should suffice for your needs right now:

ssh -f shantanu@ -L 41111: -N
scp -P 41111 shantanu@localhost:/home/shantanu/test.txt .

It should be noted that the first command creates a tunnel (accessible only from your localhost, but still) that you might want to close after transferring the files. Or leave it open, it's not a huge security risk since you still have to auth against the second host to use it.

share|improve this answer

Just pipe the local file through a chain of SSH pipes to its final destination:

ssh shantanu@ "ssh shantanu@ 'cat >test.txt'" </home/shantanu/test.txt

The only tricky part can be the double quoting required to get the shell redirection executed on the target machine.

share|improve this answer

(Not directly related to this but..) you can also create a Reverse SSH Tunnel.

on Remote server:

ssh -fN -R 7000:localhost:22 username@yourMachine-ipaddress

Now do an ssh connection request from your machine to your own machine at port 7000:

ssh username@localhost -p 7000
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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