495

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.

11 Answers 11

856

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.

9
  • 10
    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)
    – jsbueno
    Aug 24, 2013 at 0:16
  • 1
    Works with colon symbol user@host:/path Feb 3, 2014 at 11:47
  • 35
    the key part of the command is -e 'ssh -p 2222' so you can use this with different rsync params Nov 21, 2014 at 18:19
  • 5
    Note that if you need to spec an ssh key you can do that like -e 'ssh -i mykey -p 2222'
    – dranxo
    Aug 25, 2015 at 21:20
  • 11
    why --remove-sent-files? If it's dangerous to the source data, better mention it...
    – Til
    Jun 24, 2017 at 3:25
182

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
9
  • 31
    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.
    – John Hunt
    Apr 28, 2014 at 8:59
  • 8
    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.
    – Joao Costa
    May 12, 2014 at 9:39
  • 1
    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 Sep 30, 2015 at 6:25
  • 2
    @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. Aug 5, 2016 at 6:32
  • 1
    I'd prefer this solution as I'm lazy enough to keep typing the same thing over and over. Mar 16, 2017 at 3:14
35

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
4
  • 3
    and what exactly does this add to the existing answer?
    – Chris Maes
    Dec 3, 2015 at 16:02
  • 1
    look at the format and and the example. I have simplified things here.
    – Techie
    Dec 3, 2015 at 16:31
  • 4
    OK you removed two optional options, but in essence your answer is exactly the same as the accepted one...
    – Chris Maes
    Dec 3, 2015 at 17:19
  • 6
    It's more simplified and formatted. Easier to understand for new learners. By the way thanks for down voting without a reason
    – Techie
    Dec 7, 2015 at 3:33
20

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/

2
  • Worked for me too! I don't know why the most-voted answer does not work...
    – ch271828n
    Feb 12, 2018 at 12:06
  • Worked for me too! I needed the keyword "--rsh"
    – Taewoo Lee
    Jan 20, 2020 at 8:12
11

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.

3
  • other answers did not mention port 873
    – Kevin
    Mar 11, 2018 at 5:44
  • the question was how to get it to run as ssh over port 2222; whether or not rsync requires it is a different story. Apr 23, 2019 at 20:55
  • @DanSteingart I updated the answer, does it help more now ?
    – Kevin
    Apr 24, 2019 at 19:14
3

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/

1
  • This doesn’t work because keyboard-interactive messes with the data stream, corrupting the protocol. Dec 14, 2016 at 17:15
3

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/
2
  • 4
    and what exactly does this add to the existing answer?
    – Chris Maes
    Dec 3, 2015 at 16:01
  • Shows correct quoting for using port number in a variable, rather than hard-coding it. Nice.
    – Kevin
    Jul 28, 2020 at 23:29
3

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);)

1

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

1

Running Linux Ubuntu I use the following command

rsync -rvz --rsh='ssh -p2222' --progress --remove-sent-files ./dir user@host:/path

This passes rsh environmental variable as ssh connection on port 2222. All other methods here seem to not work for my flavor of linux so figured I'd provide others my method.

You can also specify any remote shell you like, either by using the -e command line option, or by setting the RSYNC_RSH environment variable.

Documentation for Rsync: https://download.samba.org/pub/rsync/rsync.1

-3

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.

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.