ChatGPT解决这个技术问题 Extra ChatGPT

Is it possible to specify a different ssh port when using rsync?

I have been attempting the following command:

rsync -rvz --progress --remove-sent-files ./dir user@host:2222/path

SSH is running on port 2222, but rsync still tries to use port 22 and then complains about not finding the path, cause of course it does not exist.

I would like to know if it is possible to rsync to a remote host on a non-standard ssh port.


P
Piotr Dąbrowski

Your command line should look like this:

rsync -rvz -e 'ssh -p 2222' --progress ./dir user@host:/path

this works fine - I use it all the time without needing any new firewall rules - just note the SSH command itself is enclosed in quotes.


I wonder why it is so - it is such an obvious need, still quite hidden, since this is effectively creating an alias to the ssh binary. (It worked flawlessly, though)
Works with colon symbol user@host:/path
the key part of the command is -e 'ssh -p 2222' so you can use this with different rsync params
Note that if you need to spec an ssh key you can do that like -e 'ssh -i mykey -p 2222'
why --remove-sent-files? If it's dangerous to the source data, better mention it...
J
Joao Costa

Another option, in the host you run rsync from, set the port in the ssh config file, ie:

cat ~/.ssh/config
Host host
    Port 2222

Then rsync over ssh will talk to port 2222:

rsync -rvz --progress --remove-sent-files ./dir user@host:/path

Although this isn't the most obvious answer it is still a very good answer. It's worth using SSH config for any host you connect to more than once or twice as it'll save you a lot of thinking and typing.
Indeed, although to be fair, the downside is it becomes invisible, ie, after a while one might forget it's in the ssh config file and not understand how it works, or one of your colleagues might copy/paste the command and not understand why it doesn't work in their account. Still, personally i prefer not having to type the port number all the time.
This is horrible. its completely incompatible with port NATing. unless you want to have multiple dns names for the same ip address, which is a maintenance issue
@melfect Are you kidding? I wouldn't call it horrible as much as I'd call it the greatest thing ever for someone who needs to rsync to a weird port many times but each time is spread out just enough to where you forget the syntax.
I'd prefer this solution as I'm lazy enough to keep typing the same thing over and over.
T
Techie

when you need to send files through a specific SSH port:

rsync -azP -e "ssh -p PORT_NUMBER" source destination

example

rsync -azP -e "ssh -p 2121" /path/to/files/source user@remoteip:/path/to/files/destination

and what exactly does this add to the existing answer?
look at the format and and the example. I have simplified things here.
OK you removed two optional options, but in essence your answer is exactly the same as the accepted one...
It's more simplified and formatted. Easier to understand for new learners. By the way thanks for down voting without a reason
S
Siwei

use the "rsh option" . e.g.:

rsync -avz --rsh='ssh -p3382' root@remote_server_name:/opt/backups

refer to: http://www.linuxquestions.org/questions/linux-software-2/rsync-ssh-on-different-port-448112/


Worked for me too! I don't know why the most-voted answer does not work...
Worked for me too! I needed the keyword "--rsh"
K
Kevin

The correct syntax is to tell Rsync to use a custom SSH command (adding -p 2222), which creates a secure tunnel to remote side using SSH, then connects via localhost:873

rsync -rvz --progress --remove-sent-files -e "ssh -p 2222" ./dir user@host/path

Rsync runs as a daemon on TCP port 873, which is not secure.

From Rsync man:

Push: rsync [OPTION...] SRC... [USER@]HOST:DEST

Which misleads people to try this:

rsync -rvz --progress --remove-sent-files ./dir user@host:2222/path

However, that is instructing it to connect to Rsync daemon on port 2222, which is not there.


other answers did not mention port 873
the question was how to get it to run as ssh over port 2222; whether or not rsync requires it is a different story.
@DanSteingart I updated the answer, does it help more now ?
a
activedecay

I found this solution on Mike Hike Hostetler's site that worked perfectly for me.

# rsync -avz -e "ssh -p $portNumber" user@remoteip:/path/to/files/ /local/path/

and what exactly does this add to the existing answer?
Shows correct quoting for using port number in a variable, rather than hard-coding it. Nice.
L
Luke

When calling rsync within java (and perhaps other languages), I found that setting

-e ssh -p 22

resulting in rsync complaining it could not execute the binary:

ssh -p 22

because that path ssh -p 22 did not exist (the -p and 22 are no longer arguments for some reason and now make up part of the path to the binary rsync should call).

To workaround this problem I was able to use this environment variable:

export "RSYNC_RSH=ssh -p 2222"

(Programmatically set within java using env.put("RSYNC_RSH", "ssh -p " + port);)


T
Tomas

A bit offtopic but might help someone. If you need to pass password and port I suggest using sshpass package. Command line command would look like this: sshpass -p "password" rsync -avzh -e 'ssh -p PORT312' root@192.xx.xxx.xxx:/dir_on_host/


This doesn’t work because keyboard-interactive messes with the data stream, corrupting the protocol.
C
CalfCrusher

My 2cents, in a single system user you can set the port also on /etc/ssh/ssh_config then rsync will use the port set here


K
Ketema

I was not able to get rsync to connect via ssh on a different port, but I was able to redirect the ssh connection to the computer I wanted via iptables. This is not the solution I was looking for, but it solved my problem.