How to copy a file from one server to another from a third with SSH


Find out how to work some SSH magic, by transferring a file from one machine to another from a third.

Online internet secure payment and network safe communication and banking concept. Person pay in web via computer. Locks and padlocks on diagram.

Getty Images/iStockphoto

SSH is an incredibly flexible tool that most every Linux administrator depends upon. SSH has a lot of tricks up its sleeve, one of which allows you to copy files with the scp command. But even that trick has a trick of its own: The ability to copy a file from Server A to Server B from Client C. This comes in handy when you have a lot of work to do and don’t want to have to constantly be SSH’ing into a plethora of servers. 

What you’ll need

To make this happen, you’ll need three Linux machines, all of which accept SSH connections. You’ll also need SSH keys for authentication. 

SSH Keys

The first thing that must be done is copying SSH keys to the servers. Here’s our server layout:

  • Server A – 192.168.1.15
  • Server B – 192.168.1.160
  • Client C – 192.168.1.7

What we need to do is copy the SSH Keys back and forth to each machine. This is done with the following command:

ssh-copy-id USER@IP

Where USER is the remote user and IP is the IP address of the machine in question. Make sure to do this from A to B, from A to C, from B to A, from B to C, from C to A, and from C to B.

You should now be able to SSH from Client C to Server A and then from Server A to Server B using SSH key authentication.

How to configure SSH

We now need to map our servers in the ~/.ssh/config file. It used to be that you could simply issue the command:

scp USER@192.168.1.15:/home/USER/test.txt USER@192.168.1.160:/home/USER/test.txt

This is no longer the case. Now you must rely on the ~/.ssh/config file. Open that file with the command:

nano ~/.ssh/config

In that file, you must create new configurations for SERVERA and SERVERB. Those configuration blocks will look like:

Host SERVERA
    HostName 192.168.1.16
    ControlMaster auto
    ControlPath...



www.techrepublic.com