How to specify port for scp for a remote server?
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:
void
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).
Related videos on Youtube
Comments
-
Rakib Fiha almost 2 years
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 port2222
by default.But the remote1's default ssh port is set to
22
in the ssh config instead of port2222
. So, to make any external connection via ssh, it uses port22
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 10.0.1.10 port 22: Connection refused lost connection
Which is true since
10.0.1.10
is using port2222
and not port22
.How can I specify remote1 to use port
2222
when trying to send files to10.0.1.10
(remote2) fromremote1
?Update
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.
-
Panki almost 5 yearsstackoverflow.com/questions/10341032/… might help you
-
Rakib Fiha almost 5 yearsI tried those as I mentioned in my question. Did not seem to work for me
-
Rakib Fiha almost 5 yearslocalhost 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. -
mosvy almost 5 yearsYes, that does answer your question. "No" is an answer and is the answer, no matter if you like it or not.
-
-
Rakib Fiha almost 5 yearsCould you explain what
-x -oClearAllForwardings=yes
this option is doing in your answer please? I was also thinking if it can be done usingrsync
instead. -
mosvy almost 5 years
-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
) -
mosvy almost 5 years@RakibFiha rsync says: "The source and destination cannot both be remote". Maybe there are tricks around it, but that should be a separate question.
-
Rakib Fiha almost 5 yearsNow, I got to realise it after your detailed explanation. (y)