How to copy a file between two remote SSH servers



SSH has a number of tips up its sleeve, together with the flexibility to repeat information between two distant servers.

For those who administer sufficient Linux servers you might be most likely aware of transferring information between machines, with the assistance of the SSH command scp. The method is straightforward:

  1. You log into the server containing the file to be copied.
  2. You copy the file in query with the command scp FILE [email protected]_IP:/DIRECTORY

The place FILE is the file to be copied, USER is a distant username, SERVER_IP is the IP deal with of the distant server, and DIRECTORY is the vacation spot listing that may home the distant file. The method is as second nature as another Linux command, however what occurs if you’re spending a great deal of time transferring information from one machine to a different? This might flip right into a state of affairs the place you continually need to:

  • Log into one machine.
  • Switch information to a different.
  • Sign off of the unique machine.
  • Log into a unique machine.
  • Switch information to yet one more machine.

SEE: Info safety coverage template obtain (Tech Professional Analysis)

What should you may make that course of considerably extra environment friendly, by sending information from MACHINE1, to MACHINE2, from MACHINE3? In different phrases, you possibly can log into one Linux machine after which begin transferring information between distant machines, with out logging out and in of various servers. What if there was a device to deal with such a job?


Stated device is, consider it or not, SSH itself.

Let me present you ways that is achieved.

What we’re doing

We’ll copy the file /dwelling/jack/techrepublic, from IP deal with into the /dwelling/jack/techrepublic folder on IP deal with, engaged on a machine at IP deal with

Earlier than we do that, there’s a one-time job to undertake: Copying SSH keys to and from these machines (I’m assuming you have already got your SSH keys generated on all machines.). To do that, execute the next instructions:

Now that each one the mandatory SSH keys have been copied, log into every machine to check that the SSH key authentication is working. When you confirm that course of, you might be able to ship the techrepublic file on .53 to .51 from .162. Confused? Don’t be concerned, the command itself will clear issues up.

This is the command to repeat the techrepublic file (once more, on .53 to .51). Be sure to’re logged into .153 and the techrepublic file exists on .53. With that in place, concern the command:

scp -Three [email protected]:/dwelling/jack/techrepublic [email protected]:/dwelling/jack/techrepublic

Since we arrange SSH key authentication, you should not be prompted for a password. The file will copy from .53 to .51, and also you’re completed. You’ve got efficiently copied a file between two distant servers, utilizing a safe SSH tunnel.

If you don’t add the -Three possibility, the copy course of will fail with an authentication error (Determine A).

Determine A: Our copy failed with out the -Three possibility.

Complicated however environment friendly

Though this course of may appear complicated at first, when you get the hold of it, you may discover it much more environment friendly than utilizing the usual technique. This additionally has the good thing about forcing you into making use of SSH key authentication—an added safety layer you need to use anyway.

Additionally see

Picture: iStock


Source link