Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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 want to copy a file from server-a to server-d but I can access server-d only from server-b and then to server-c. server-a can only access server-b, server-b can only access server-c, server-c can only access server-d..

Currently I do it like this:

server-a> scp /tmp/file.txt user@server-b
server-a> ssh user@server-b
server-b> scp /tmp/file.txt user@server-c
server-b> ssh user@server-c
server-c> scp /tmp/file.txt user@server-d

This could be through 5,6,7,.. servers

Can I do this is one command?

share|improve this question

You need the ProxyCommand, see the man page. Here's an example:

Host serverB
HostName serverA.com
User someuser
ProxyCommand ssh -q serverB -W %h:%p  # -W is supported by a recent OpenSSH
# or for older versions or other implementations
# ProxyCommand ssh -q serverB nc %h %p

This allows you to type ssh serverB and you connect to serverA which then connects to serverB. This can easily be extended to work with 𝑛 host. It will work with scp the same way it does for ssh.

share|improve this answer
I only can use the ssh command not the ~/.ssh/config file. Does this command should work ssh -o ProxyCommand="ssh -o ProxyCommand=\"ssh user@server-d\" user@server-c" user@server-b ? – nir Sep 16 '13 at 13:12
Should work. You can also use alternate config file through the -F option. – peterph Sep 16 '13 at 19:58

your best bet is probably to build up tunnels, and then cat the file through oll these tunnels:

cat file.txt | ssh -A -t user@server-b \
   "cat | ssh -A user@server-c         \
      \"cat | ssh -A user@server-d     \
         \\\"cat > /tmp/file.txt\\\"   \
      \"                               \
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.