I have two remote nodes that I am trying to send files from one to another.

scp remote1:~/testSCP [email protected]:~/

I have ~/ssh/config set up in my local machine so, it is using port 2222 by default.

But the remote1's default ssh port is set to 22 in the ssh config instead of port 2222. So, to make any external connection via ssh, it uses port 22 by default.

I tried the following which did not work:

scp -P 2222 remote1:~/testSCP [email protected]:~/

Also tried the following, which also did not work:

scp remote1:~/testSCP -P 2222 [email protected]:~/

For both I got the following error:

ssh: connect to host port 22: Connection refused
lost connection

Which is true since is using port 2222 and not port 22.

How can I specify remote1 to use port 2222 when trying to send files to (remote2) from remote1?


After trying

 scp -3 remote1:~/testSCP [email protected]:~/

I get weird behaviour. Even though my password is correct, it is giving me the following output:

[email protected]'s password: [email protected]'s password: 
Permission denied, please try again.
[email protected]'s password: 
Permission denied, please try again.
[email protected]'s password:

I have not enabled key-less authentication yet.

New Update

After trying it in several ways, I was able to do it in my scripts by logging in to the remote1 via ssh from my localhost and then scp from remote1 to remote2. However, this does not answer my question. I intended upon doing it directly from my local machine then transfer files between two instances, which I think is not supported if two instances' ssh daemons are using a different port than the default for ssh connection.

  • stackoverflow.com/questions/10341032/… might help you
    – Panki
    Aug 16, 2019 at 8:18
  • I tried those as I mentioned in my question. Did not seem to work for me
    – Rakib Fiha
    Aug 16, 2019 at 8:28
  • localhost listening to port 22 but in ssh config its specefied for both remote1 to remote2 to port 2222. Yes, I meant ~/.ssh/config In remote 1 and remote the default port in ~/.ssh/config is unchanged so its default port 22. I can edit them, but I would like to do it using a one-liner, instead of editing every ssh config every single time.
    – Rakib Fiha
    Aug 16, 2019 at 9:06
  • Ok. I'd suggest to edit your question, adding that you can, but aren't willing to, edit your ~/.ssh/config files. Then, you may ssh into one to the remote hosts and issue a simple scp from there, or do it in one line with something as ssh -t -p <remote1 port> user1@remote1 'scp -P <remote2 port> user2@remote2:/source/path /dest/path'. Also, you won't have to edit config files any single time: only once for each couple of hosts (e.g. setting the port for connecting to remote2 on remote1), but of course you may be uncomfortable with this, depending on the number of hosts you have.
    – fra-san
    Aug 16, 2019 at 9:26
  • Yes, that does answer your question. "No" is an answer and is the answer, no matter if you like it or not.
    – user313992
    Aug 16, 2019 at 11:50

2 Answers 2


You cannot do that with a simple remote-to-remote scp [1].

Instead of it, ssh to the 1st remote host and run scp with a port argument from there:

ssh -p 2222 ruser1@rhost1 scp -P 2222 /rpath/1 ruser2@rhost2:/rpath/2

If you want to do exactly what scp is doing, you can also add the -n -x -oClearAllForwardings=yes options to ssh, though that's usually not needed.

[1]: newer versions of scp support a uri spec (including a port) instead of host:path, but only when using the -3 option ("pass through the local host").

So you could probably use

scp -3 -P 2222 ruser1@rhost1:/rpath/1 scp://ruser2@rhost2:2222//rpath/2

(notice that the / after the host[:port] is not part of the path -- scp://user@host/file will refer to ./file in the user's home directory).

But copying through the localhost is both slower, and in my experience it will hide errors. For instance, this won't print any error message, despite not being able to create any /foo/bar/baz file:

scp -3 localhost:.bashrc localhost:/foo/bar/baz

I did not get to any depth into this -- just avoided it ;-)

If someone is not convinced by all this, they can look at the source code:

toremote(char *targ, int argc, char **argv)
                } else if (host) {      /* standard remote to remote */
                        if (tport != -1 && tport != SSH_DEFAULT_PORT) {
                                /* This would require the remote support URIs */
                                fatal("target port not supported with two "
                                    "remote hosts without the -3 option");

Notice that the tport variable is only set by parsing a scp:// uri, and it simply doesn't exist in versions older than 7.6p1 (Oct 2017).

  • Could you explain what -x -oClearAllForwardings=yes this option is doing in your answer please? I was also thinking if it can be done using rsync instead.
    – Rakib Fiha
    Aug 16, 2019 at 10:16
  • -x disables X11 forwarding and -oClear.. disables all tcp forwardings (in case any of them were forced via ~/.ssh/config or /etc/ssh/ssh_config)
    – user313992
    Aug 16, 2019 at 10:19
  • 1
    @RakibFiha rsync says: "The source and destination cannot both be remote". Maybe there are tricks around it, but that should be a separate question.
    – user313992
    Aug 16, 2019 at 10:52
  • Now, I got to realise it after your detailed explanation. (y)
    – Rakib Fiha
    Aug 16, 2019 at 12:07

A way that does work is setting up an alias in ~/.ssh/config


Host destination
User remote2
Port 2222

Now, you can just use destination as your 'hostname' instead, saving you from having to specify the hostname/user/port in the actual command.

You must log in to answer this question.

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