17

How can I use scp command to copy files between two remote servers from my local PC ?

remote server 1 : IP=67.12.21.133 & port=6774

remote server 2 : IP=67.129.242.40 & port=6774

scp -rp -P 6774 [email protected]:/home/denny/testapp1.txt [email protected]:

It gives an error after giving password of 67.12.21.133 ,

ssh: connect to host 67.129.242.40 port 22: Connection refused

lost connection

1
  • I just tested that also, but only works for the first host. The second is locked to port 22.
    – gajdipajti
    Jun 21, 2012 at 7:43

5 Answers 5

23

You can use ~/.ssh/config to specify the ports to use for the hosts (and for setting many other nice things; check the man page man ssh_config):

# ~/.ssh/config

Host 67.12.21.133
  Port 6774

Host 67.129.242.40
  Port 6774

When doing this, you have to use the option -3 to scp, which copies the files through your local machine. Otherwise, scp issues the scp command via ssh on the first host, so it actually runs

 ssh -p 6774 [email protected] scp -rp /home/denny/testapp1.txt [email protected]:

and then the ~/.ssh/config of the first remote host (67.12.21.133) is used instead of your local one.

When you have setup your ~/.ssh/config correctly, this should work:

scp -rp3 [email protected]:/home/denny/testapp1.txt [email protected]:

Of course, you can also copy the contents of the ~/.ssh/config file onto your first remote host, and then you can use scp without the -3 option, which will probably speeden up the transfer.

Or you can use the trick that scp uses and use such a command line:

ssh -p 6774 [email protected] scp -rp -P 6774 /home/denny/testapp1.txt [email protected]:

(Note the different case of the port parameter for ssh and scp: ssh -p 6774 vs. scp -P 6774)

PS: I got this information from the OpenSSH bugzilla where I entered this as a bug: https://bugzilla.mindrot.org/show_bug.cgi?id=2020

1
  • '-3' doesn't appear to be a valid option - I just get "scp: illegal option -- 3". Mar 1, 2017 at 11:49
8

I found no easy solution but you could try to use sshfs the following way:

mkdir /tmp/h1
mkdir /tmp/h2

sshfs -p 6774 user1@host1:/public /tmp/h1
sshfs -p 2211 user2@host2:/data/src /tmp/h2

cp /tmp/h1/files.* /tmp/h2

fusermount -u /tmp/h1
fusermount -u /tmp/h2

Another way could be to use ssh (actually not successfull tested):

ssh user1@host1 "cat /public/file.bin" | ssh user2@host2 "cat >/data/file.bin"

Currently I don't found the right way to enter the two passwords. It asks some times for the two passwords but doesn't accept anyone. Maybe if you exchange the ssh keys between the hosts it works. Because you than don't need the passwords.

I hope this helps? Thomas

0
6

According to this page on the Linux Academy blog, you simply need to use the -P 6774 option right before the second remote path as well:

scp -rp -P 6774 [email protected]:/home/denny/testapp1.txt -P 6774 [email protected]:

Each use of the -P flag applies only to the next location on the command line, not the entire command. Any location that doesn't have a -P between it and the previous path (or the command name [scp]) defaults to the standard port (22).

6
  • It doesn't work for me. It's a pity, because this seems the best solution. The link is broken. Jan 26, 2016 at 12:50
  • 1
    Just updated the link (though I forgot to log in first...). Linux Academy decided to restructure their site. Hope that helps, some! Jan 27, 2016 at 22:20
  • 2
    If only this worked. But I get -P: No such file or directory.
    – Tom
    Sep 26, 2017 at 11:56
  • @Tom - What system are you running this on? You might have a different implementation of scp than the one referenced here. Sep 26, 2017 at 16:29
  • This is Ubuntu 17.04.
    – Tom
    Sep 26, 2017 at 16:41
3

A method working with the current version of OpenSSH is specifying the ports in the source and target URLs:

scp -3 scp://host1:port1//path/to/file  scp://host2:port2//path/to/file
2
  • 1
    This method also works if host1 and host2 can't see each other, but the machine from where the scp is being executed can see both (in my case with Cygwin, I did not need to specify the "-3" parameter)
    – AwkMan
    Jun 30, 2022 at 15:40
  • 1
    This is the best answer! And it is also possible to mix the old host syntax with this new scp:// syntax in a single command. The -3 option is necessary only if hosts can't reach each other directly.
    – k3a
    Mar 17, 2023 at 16:25
-3

If you need to transfer data from one server to another, use the SCP command. To do so

Just press Ctrl+Alt+T on your keyboard to open Terminal. When it opens, run the command below.

scp -pr [email protected]:/path/to/files /destination/path

Note: The parameter -P can be used if you want to mention the port number.

4
  • 2
    you haven't read the question. He has a problem with the ports. you can set the first port, but nut the second with the -P parameter
    – gajdipajti
    Jun 21, 2012 at 7:25
  • @gajdipajti I didn't specify a port in my answer, and added it as a note. The user also stated that he is using the same port. Look at the Q. 2nd & 3rd lines :)
    – Mitch
    Jun 21, 2012 at 7:32
  • 2
    But it doesn't work that way, you will set the first with -P but the second will remain 22. Just give it a try. I am doing the tests at the moment, but still no working solutions.
    – gajdipajti
    Jun 21, 2012 at 7:41
  • You would only use the -P option if you're using a non standard port.
    – Mitch
    Jun 21, 2012 at 7:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .