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?


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.

  • 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\\\"   \
      \"                               \

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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