Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

rsync with Non-Standard SSH Port: How-To

· 6 min read · Home Tech

In this article
  1. Background: Why Use a Non-Standard SSH Port?
  2. How to Use rsync with a Non-Standard SSH Port
  3. How to Choose Between -e and --port
  4. Common Problems and Solutions
  5. Comparison: SSH Port vs. rsync Daemon Port
  6. Costs and Safety Considerations
  7. Alternatives to rsync
  8. Frequently Asked Questions

Yes, you can sync files using rsync with a non-standard SSH port by using the -e option to specify an SSH command with the port number, or by using the --port option if the remote rsync daemon is listening on a non-standard port. This article explains both methods, how to choose between them, and common pitfalls to avoid.

Background: Why Use a Non-Standard SSH Port?

By default, SSH listens on port 22. Many system administrators change this to a non-standard port (e.g., 2222, 22000) to reduce automated brute-force attacks and log noise. When you use rsync over SSH, rsync invokes the SSH client to establish the connection, so it must know which port to use. If the remote server's SSH daemon is not on port 22, a standard rsync command will fail with Connection refused or a timeout.

There are two distinct scenarios:

  • rsync over SSH: The most common case, where rsync uses SSH as a transport. You need to tell SSH which port to connect to.
  • rsync daemon mode: The remote machine runs the rsync daemon (rsyncd) which listens on its own port (default 873). If that port is changed, you use --port.

This article focuses primarily on the first scenario, as it is what most people mean by "rsync with a non-standard SSH port."

How to Use rsync with a Non-Standard SSH Port

The key is the -e (or --rsh) option, which lets you specify the remote shell program that rsync should use. You can pass SSH options to it, including the port.

The basic syntax is:

rsync -avz -e "ssh -p 2222" /local/path/ user@remote:/remote/path/

Here:

  • -a (archive mode) preserves permissions, timestamps, and other metadata.
  • -v (verbose) shows progress.
  • -z (compress) reduces data transfer.
  • -e "ssh -p 2222" tells rsync to use SSH with port 2222.
  • /local/path/ is the source directory (trailing slash matters: see below).
  • user@remote:/remote/path/ is the destination.

You can also use the long form of the option: --rsh="ssh -p 2222".

If you need to use a different SSH key or other SSH options, include them inside the quotes:

rsync -avz -e "ssh -p 2222 -i /path/to/key" /local/ user@remote:/remote/

How to Choose Between -e and --port

Use -e "ssh -p PORT" when you are syncing over SSH (the default). This is the most common method and works with any SSH server.

Use --port=PORT when you are connecting directly to an rsync daemon (using rsync:// protocol). This is rare for personal use but common in backup servers configured to run rsyncd on a custom port. For example:

rsync -avz --port=8730 /local/ rsync://user@remote/module/

In this case, the remote machine runs the rsync daemon, not SSH. If you are unsure which one to use, you are almost certainly using SSH, so -e is correct.

Common Problems and Solutions

Here are the most frequent issues users encounter:

  • Connection refused: This usually means the SSH port is wrong or the SSH service is not running. Double-check the port number and that the server's firewall allows it.
  • Permission denied: SSH authentication failed. Check your username, SSH key setup, or password.
  • rsync: connection unexpectedly closed: This can happen if the remote server does not have rsync installed, or if the SSH connection is dropped due to a firewall or network issue. Verify rsync is installed on both ends.
  • Trailing slash errors: A trailing slash on the source path means "copy the contents of this directory," while no trailing slash means "copy the directory itself." For example, /home/user/data/ copies the contents, /home/user/data copies the directory and its contents into the destination. This is a common source of unexpected file structures.
  • Using the wrong option: Remember, --port is for rsync daemon, not SSH. If you use --port with an SSH connection, it will be ignored and the default SSH port 22 will be used.

Comparison: SSH Port vs. rsync Daemon Port

It's important to understand the difference between the SSH port and the rsync daemon port. They are two separate services.

  • SSH port (default 22): Used for secure shell sessions, including rsync over SSH. Changing this port affects all SSH connections (SFTP, scp, etc.).
  • rsync daemon port (default 873): Used by the rsync daemon (rsyncd) which serves files without SSH. This is less common and is often used for anonymous read-only mirrors.

When you use -e "ssh -p 2222", you are telling SSH to use port 2222. When you use --port=8730, you are telling rsync to connect directly to the rsync daemon on port 8730, bypassing SSH entirely. Mixing these up is the most common mistake.

Costs and Safety Considerations

Using a non-standard SSH port is a security measure, but it is not a silver bullet. It reduces automated attacks but does not stop targeted ones. Always use strong authentication (SSH keys) and keep your software updated.

There is no monetary cost to changing the SSH port, but there is a usability cost: you must remember to specify the port every time you connect, unless you configure it in your SSH config file. You can add a host entry in ~/.ssh/config to specify the port automatically:

Host myserver
    HostName example.com
    Port 2222
    User myuser

Then you can simply run rsync -avz /local/ myserver:/remote/ and SSH will use port 2222 automatically.

Regarding safety, always test your rsync command with --dry-run (or -n) to see what would be transferred without actually changing anything. This is especially important when deleting files with --delete.

Alternatives to rsync

If rsync does not meet your needs, there are alternatives:

  • scp: Simple secure copy, but does not preserve all metadata and cannot resume interrupted transfers.
  • sftp: Interactive file transfer over SSH, good for manual operations.
  • rdiff-backup: Incremental backup tool that uses rsync algorithms, but stores reverse diffs.
  • Syncthing: Continuous file synchronization between devices, not a one-time sync tool.

However, for one-time or scheduled syncs with efficient delta transfer, rsync remains the standard choice.

Frequently Asked Questions

Can I use rsync with a non-standard SSH port without typing the port every time?

Yes, configure the port in your SSH config file (~/.ssh/config) as shown above. Then rsync will automatically use that port when you reference the host alias.

What if I get "Permission denied (publickey)" when using a non-standard port?

This means SSH authentication failed. The port is correct, but your SSH key or password is not accepted. Check that you are using the correct username, that your public key is in the remote server's ~/.ssh/authorized_keys, and that the private key has correct permissions (600 on Linux/macOS).

Does the non-standard SSH port affect rsync's transfer speed?

No, the port number does not affect transfer speed. The speed depends on network bandwidth, disk I/O, and compression settings. Using a non-standard port only changes the connection endpoint.

Can I use rsync with a non-standard SSH port on Windows?

Yes, if you have rsync installed (e.g., via WSL, Cygwin, or Git Bash) and an SSH client. The same -e "ssh -p PORT" syntax works, but you may need to adjust the path to ssh.exe.

What is the difference between "ssh -p" and "ssh -P"?

The lowercase -p is used to specify the port for SSH. The uppercase -P is used by scp to specify the port, but for SSH it is not valid. In rsync's -e option, you must use -p (lowercase) because you are passing the argument to the ssh command.