Linux Commands

How to Use Rsync with Non-Standard Port

Rsync is a valuable utility for transferring files between servers. Rsync is used to copy files locally, from a remote rsync daemon to another host/on another remote shell. We widely use rsync for backing up data. Over SSH, rsync communicates with the remote host using the default port 22.

Most Linux system users have changed the default SSH port to some non-standard ports for security‌. In this condition, you need to specify an SSH port on a non-standard port to connect the rsync command. This article will see how to copy data using rsync with non-standard ports. Let’s start by changing the SSH port to a non-standard port.

How to Change SSH Port to Non-standard Port

We need to change the SSH port of our remote server to tighten the security. As we already know, rsync uses the default SSH port 22 to sync files from remote to localhost and vice versa.

To change the SSH port to a non-standard port, open and edit the SSH configuration /etc/ssh/sshd_config file:

vi /etc/ssh/sshd_config

Please find the following lines and change the port number according to you by uncommenting it. Any number you choose can be difficult for others to choose.

We need to allow the new port through our router and firewall in RPM-based systems such as Scientific Linux 7, CentOS, and RHEL.

firewall-cmd --add-port 1431/tcp

firewall-cmd --add-port 1431/tcp --permanent

To allow the port, SELinux permissions have to be updated.

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p TCP --dport 2345 -j ACCEPT
semanage port -a -t ssh_port_t -p tcp the 2345.

Restart the SSH service to take effect.

service sshd restart [On SysVinit]

Or

systemctl restart sshd [On System]

How to Use Rsync with Non-Standard Port

You can specify SSH to use rsync communication between the remote and local hosts. Using SSH over a non-standard port, your remote host performs (2232) using native rsync syntax.

rsync [options] src [dest]

The rsync command lists only files at the source by default, with no destination provided.

rsync /usr/

Now, with the help of the command below, we can copy locally.

rsync -av file1 dir1/

‘file1’ is copied into ‘dir1’ with the above command. Also, here the -v option is used for output only.

Rsync SSH to a Specific Port

We can connect to a machine running SSH on a specific port using rsync with the help of the following command.

rsync --rsh='ssh -p2345' <sourcefile> user@host:/path/to/destination/directory

Change and uncomment the port number. Here, we are changing port number 22 to 2345.

Using the -p option, specify the port on which SSH runs, as specified above. Using rsync+ssh, it copies the file.

The rsync command pushes a local file with a custom SSH port 22. Here, the local SSH port does not matter.

Conclusion

To work with another host, rsync has to use a non-standard port from a security perspective to copy files and back-up data over the default SSH port. In this article, we have seen how rsync is used with non-standard ports. Please feel free to contact us with any questions, problems, or doubts about the above information.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.