514

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.

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
Ketema
  • 6,108
  • 3
  • 21
  • 24

11 Answers11

883

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.

klapa
  • 8,839
  • 2
  • 14
  • 2
187

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
Joao Costa
  • 2,563
  • 1
  • 21
  • 15
  • 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 '14 at 08: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 '14 at 09: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 – Northstrider Sep 30 '15 at 06: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. – Freedom_Ben Aug 05 '16 at 06:32
  • 1
    I'd prefer this solution as I'm lazy enough to keep typing the same thing over and over. – Christian Noel Mar 16 '17 at 03:14
  • 1
    While this makes it easier for you to connect, it also makes it easier for hackers, and your auth.log will once again be swamped with automated attempts. So it sort of voids the point of changing ports. – forthrin Sep 17 '18 at 05:44
  • What if one host listens on two different ports? Right now I'm using /etc/hosts with custom names to be able to have two different host entries in the ssh config, but this doesn't work with dynamic dns. – XMB5 Aug 14 '19 at 03:45
  • arguable, but imo this is vastly better than other answers as you may set this via cloud-config, locally in /etc or per user in /home/you/.ssh. The fact that you can forget it is a feature because it means someone else (or system) can maintain settings. Mostly likely this is *why* the rsync project uses SSH's settings and does not have a simple port flag – That Realty Programmer Guy Oct 19 '22 at 11:22
  • @Northstrider this is not incompatible with port forwarding in any way shape or form. If you have only 1 DNS name per IP you are deeply underutilizing DNS. Most folks don't realize CNAME is not alias, A is an alias. By DNS design, you can have unlimited A but only 1 canonical name to 1 A for a reason. – That Realty Programmer Guy Oct 19 '22 at 11:24
38

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
Techie
  • 44,706
  • 42
  • 157
  • 243
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/

Siwei
  • 19,858
  • 7
  • 75
  • 95
12

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.

Kevin
  • 2,761
  • 1
  • 27
  • 31
4

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 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/
activedecay
  • 10,129
  • 5
  • 47
  • 71
Rusty1
  • 334
  • 2
  • 6
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/

Tomas
  • 1,131
  • 2
  • 12
  • 25
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);)

Luke
  • 884
  • 8
  • 21
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

CalfCrusher
  • 350
  • 1
  • 4
  • 15
-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.

Ketema
  • 6,108
  • 3
  • 21
  • 24