The thing is, you can specify a port to SCP, and you can transfer stuff from a remote host to another.

If both hosts use different ports on SSH (i.e. 2203 and 2541), how can I specify these ports to the SCP command?

I know I can do

scp -P <port> host1:/file host2:/file

But that port will apply to both hosts.

So... how can I specify two different ports for the two different hosts?

  • 5
  • 3
    The simplest way would be to add an entry for each host in ~/.ssh/config specifying each host's specific port. – DopeGhoti May 12 '17 at 17:52
  • @kyngo - what OS are you on, what's your version of scp? – tink Feb 16 '19 at 4:23
  • 1
    @tink Linux, Ubuntu 18.04 LTS now (back then it was 16.04). The latest version was installed at the moment, can't remember. – Kyngo Feb 18 '19 at 11:21

After tink's comment: I think this may not apply to Linux, but to BSD systems:

The source and target can be specified as a URI in the form scp://[user@]host[:port][/path]

so you can run:

scp scp://user1@host1:port1/path/to/file1 scp://user2@host2:port2/path/to/file2

And to copy between two remote hosts through the local host add "-3":

scp -3 scp://user1@host1:port1/path/to/file1 scp://user2@host2:port2/path/to/file2
  • scp as shipped with with Ubuntu 16.04 ` 1:7.2p2-4ubuntu2.7` doesn't understand this syntax. What OS, which version of ssh/scp are you using? – tink Feb 16 '19 at 4:27
  • I tested with Suse and it does not work as well. This may be limited to BSD systems only. – molabib Apr 3 '19 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.