Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

How to Sync Files with rsync Using a Non-Standard SSH Port

How to Sync Files with rsync Using a Non-Standard SSH Port

Synchronizing files and directories between computers is a common task for system administrators, developers, and anyone managing data across multiple machines. rsync is a powerful and efficient tool for this purpose, allowing you to copy and update files while minimizing data transfer by only syncing changes. When your remote server uses SSH on a non-standard port (not the default port 22), you need to specify this port explicitly to connect successfully. This guide walks you through how to use rsync with a custom SSH port to sync your files securely and efficiently.

What You’ll Need

  • A local machine with rsync installed (most Unix-like systems have it pre-installed).
  • SSH access to the remote server using a non-standard port (e.g., 2222, 2200, etc.).
  • The remote server’s hostname or IP address.
  • Valid SSH credentials (password or preferably SSH key authentication).
  • Basic familiarity with command-line operations.

Step-by-Step

  1. Confirm SSH connection on the custom port: Before using rsync, ensure you can SSH into the remote server using the non-standard port. Run:
    ssh -p <port> user@remote_host

    Replace <port> with the actual port number, user with your username, and remote_host with the IP or hostname.

  2. Basic rsync command structure: The general syntax to sync files/directories is:
    rsync [options] -e "ssh -p <port>" source destination

    The -e flag lets you specify the remote shell program and options—in this case, SSH with the custom port.

  3. Example: Sync a local directory to a remote directory:
    rsync -avz -e "ssh -p 2222" /path/to/local/dir/ user@remote_host:/path/to/remote/dir/

    Explanation:

    • -a: archive mode (preserves permissions, symbolic links, timestamps, etc.)
    • -v: verbose output
    • -z: compress data during transfer
    • -e "ssh -p 2222": specifies SSH with port 2222

    The trailing slash on the source directory means “copy contents of this directory” rather than the directory itself.

  4. Example: Sync from remote to local:
    rsync -avz -e "ssh -p 2200" user@remote_host:/path/to/remote/dir/ /path/to/local/dir/

    This copies files from the remote directory to your local machine.

  5. Use SSH key authentication for smoother syncing: If you have SSH keys set up, rsync will connect without prompting for a password. Otherwise, you will be prompted to enter it.
  6. Exclude files or directories if needed: To skip certain files, use the --exclude flag. For example:
    rsync -avz -e "ssh -p 2222" --exclude='*.tmp' /local/dir/ user@remote_host:/remote/dir/

    This excludes all files ending in .tmp.

  7. Dry run before actual sync: To preview what will be copied or deleted without making changes, add --dry-run:
    rsync -avz --dry-run -e "ssh -p 2222" /local/dir/ user@remote_host:/remote/dir/

    This helps avoid unexpected file overwrites or deletions.

  8. Automate with a script or cron job: For regular syncing, you can create a shell script with your rsync command and set up a cron job to run it on schedule.

Common Mistakes

  • Forgetting the -e "ssh -p <port>" part: Without specifying the port, rsync attempts to connect on default port 22 and fails if the server uses a different port.
  • Incorrect trailing slash usage: The presence or absence of a trailing slash on the source directory changes the behavior. For example, /dir/ copies contents, while /dir copies the directory itself.
  • Wrong user or hostname: Typos in the SSH username or remote host will cause connection failures.
  • Permissions issues: Not having read access on the source or write access on the destination will cause errors.
  • Not using quotes around the SSH command: When specifying -e "ssh -p 2222", omitting quotes can cause shell parsing errors.
  • Assuming passwordless login without keys: If SSH keys are not set up, you will be prompted for a password every time.

Tips

  • Use -P for progress and partial transfers: The -P flag shows progress and allows resuming interrupted transfers.
    rsync -avzP -e "ssh -p 2222" source destination
  • Combine multiple SSH options: You can add other SSH flags inside the -e option, e.g., disabling strict host key checking for temporary connections:
    rsync -avz -e "ssh -p 2222 -o StrictHostKeyChecking=no" source destination
  • Use an SSH config file for convenience: Instead of specifying the port each time, add an entry in ~/.ssh/config:
    Host myserver
      HostName remote_host
      Port 2222
      User user
    

    Then you can simply run:

    rsync -avz -e ssh /local/dir/ myserver:/remote/dir/
  • Verify sync with checksums: Use --checksum to compare file contents instead of just timestamps and sizes, but note this can be slower.
  • Log output for review: Redirect rsync output to a log file for auditing or troubleshooting:
    rsync -avz -e "ssh -p 2222" /local/dir/ user@remote:/remote/dir/ > rsync.log 2>&1

Troubleshooting

If your rsync command using a non-standard SSH port does not work as expected, here are some common issues and how to address them:

  • Connection refused or timeout: Verify that the SSH port is correct and that the remote server firewall allows incoming connections on that port. Test with ssh -p <port> user@host. If that fails, the port may be blocked or SSH server not listening there.
  • Permission denied: Check your SSH credentials and permissions on both source and destination directories. Confirm you can SSH manually and have proper read/write access.
  • Command not found: If rsync is missing on either local or remote machine, install it via your package manager (e.g., sudo apt install rsync or yum install rsync).
  • Syntax errors: Ensure you correctly quoted the -e argument and used proper flags. Run rsync --help for usage details.
  • File permission or ownership issues after sync: Use -a to preserve permissions. If ownership changes are needed, ensure you run as a user with appropriate privileges or use --chown (if supported).
  • Unexpected files not syncing or being deleted: Use --dry-run first to preview changes. Check your source and destination paths carefully.
  • SSH key issues: If using keys, confirm the private key permissions are correct (chmod 600 ~/.ssh/id_rsa) and the public key is authorized on the remote server.

Frequently Asked Questions

1. Can I use rsync over SSH with a non-standard port without specifying -e every time?

Yes. You can configure the SSH client by adding an entry in ~/.ssh/config specifying the custom port for the host. Then, rsync will use that configuration automatically.

2. Does rsync encrypt my data during transfer?

When used with SSH, yes. SSH encrypts all data between your local and remote machines, ensuring secure transfer.

3. How do I sync deletions so that files removed from source are also deleted on the destination?

Use the --delete flag with rsync. For example:

rsync -avz --delete -e "ssh -p 2222" source/ user@host:/dest/

Be cautious and test with --dry-run first to avoid accidental data loss.

4. Can rsync resume interrupted transfers?

Yes, by using the -P flag, which combines --partial and --progress. This allows interrupted transfers to resume from where they left off.

5. Is it possible to sync files between two remote hosts using rsync with a non-standard SSH port?

Yes, but it requires SSH access from your local machine to both remote hosts or using rsync in a remote-to-remote mode. You can specify the SSH port similarly with the -e option or SSH config entries for each host.

6. What happens if the SSH port is blocked by a firewall?

rsync over SSH will fail to connect. You need to open the port on the server firewall or network devices. Alternatively, consider using VPN or tunneling methods.

By following this guide, you should be able to confidently use rsync to synchronize files and directories over SSH even when the server listens on a non-standard port. This is especially useful for enhanced security setups or custom server configurations. Remember to test your commands with --dry-run before actual syncing to avoid data loss, and consider automating routine syncs with scripts and cron jobs for convenience.

Below is an additional practical example to illustrate syncing between two remote hosts via your local machine using custom SSH ports:

rsync -avz -e "ssh -p 2222" -e "ssh -p 2200" user1@host1:/path/to/dir/ user2@host2:/path/to/dir/

Note that this syntax can be complex; often, it’s easier to run rsync on one remote host via SSH, pulling or pushing files as needed, specifying the correct ports using SSH config or -e flags.

Mastering these techniques will help you maintain efficient, secure file synchronization tailored to your network environment.

Related

How to Sync Files with rsync Using a Non-Standard SSH Port
Scroll to top