Ssh – SCP file between 2 servers that go through few servers

PROXYscpssh

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?

Best Answer

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.

Related Question