Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have two remote hosts.
Both run an ssh server.

The ssh server listens on port 22 in host1 and on port 6969 in host2. Now, using my local machine, I need to copy something from host1 to host2 without logging into either host1 or host2 via ssh. Something like,

scp user@ user@

How can I do this, please note that the two hosts use different ports for ssh.

Thanks guys.

share|improve this question
Are you asking if you can transfer from a remote host to a remote host, or are you asking how to do it without having to supply a password? –  glenn jackman Dec 10 '13 at 14:22
While the -P flag exists to specify the port to use, in case of remote-to-remote transfer, ssh as no defined behaviour on how to specify per-host port... –  Kwaio Dec 10 '13 at 15:14

2 Answers 2

In the past, the way in which scp worked, when called (naively) to copy files between remote systems, was very inconvenient: if you wrote for instance

   scp user1@remote1:/home/user1/file1.txt user2@remote2:/home/user2/file1.txt

scp would first open an ssh session on remote1, and then it wouldrun scp from there to remote2. For this to work, you would have to set up the autohorization credentials for remote2 on remote1.

The modern way to do it, instead, (modern because it has been implemented only a few years ago, and perhaps not everybody has a -3-capable scp) requires two steps. First the use of the -3 option, as follows:

  scp -3 user1@remote1:/home/user1/file1.txt user2@remote:/home/user2/file1.txt

The -3 option instructs scp to route traffic through the pc on which the command is issued, even though it is a 3rd party to the transfer. This way, authorization credentials must reside only on the issuing pc, the third party.

The second necessary step is to use ~/.ssh/config to set up all options for the connection to both remote1 and remote2, as follows:

    Host remote1.example.org
    Port 2222
    IdentityFile /path/to/host1-id_rsa

    Host remote2.example.org
    Port 6969
    IdentityFile /path/to/host2-id_rsa    

This way it becomes possible to pass all necessary options to the command, without worrying about the format on the CLI, which remains instead tidy and simple.

share|improve this answer
For future reference: If you copy a file between two hosts that share an identity file (like an EC2 instance), then you don't need the config file. One -i argument is sufficient to connect to both hosts. –  Artur Czajka Aug 8 '14 at 8:13
Also worth noting, for Google Compute Engine, there is support for adding to your ~/.ssh/config file: cloud.google.com/compute/docs/gcloud-compute but I don't think that AWS has the same support –  Lucas Dec 28 '14 at 8:36
You sir, are a genius! –  look Mar 5 at 19:14

Last time I tried this, scp wasn't able to do that. Your command line looks okay. This workaround will work:

ssh -p port_on_machine1 user@machine1 "cat /path/to/file/one"|ssh -p port_on_machine2 user@machine2 "cat >/path/to/file/two"
share|improve this answer
my scp man page says "Copies between two remote hosts are also permitted." –  glenn jackman Dec 10 '13 at 14:23
Thanks, it is good to hear. To scp you can give a -P flag (it was written by some BSD people, this because its argument handling is so tragic :-( ), but it seems you can't specify different ports on the remote hosts. I am sorry, but I think, only this workaround lefts (or there are a lot of trickier solutions, using ssh but avoiding scp - for example, sftpfs, but they are not the simplest). I extended my workaround with the port settings. –  peterh Dec 10 '13 at 15:11

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.