How to Sync Directories Using Rsync with Custom SSH Port

Rsync with SSH Custom Port

SSH default port is 22, every body knew it. And it will cause many brute force SSH attacks.

For security reasons, Linux administrator should change to custom port to securing SSH access to Unix server from brute force attacks.

How to change default SSH Port

Before change port number, first open firewall port to new SSH Port, example change to port 12345, to prevent locked your self because SSH port closed by firewall.

On RHEL, CentOS, and Scientific Linux 7, allow the new port through your firewall.

# firewall-cmd --add-port 12345/tcp --permanent

On RHEL/CentOS/Scientific Linux 6 and before, you should allow new SSH port with command below.

# iptables -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 12345 -j ACCEPT
# service iptables restart

If you have Selinux enabled, you should run this command

# semanage port -a -t ssh_port_t -p tcp 12345

Then Edit /etc/ssh/sshd_config and remark port 22 and add another port, for example 12345.

#Port 22
Port 12345

Then restart SSHD service using this command:

RHEL / Centos 7 / 8
# systemctl restart sshd
RHEL / Centos 5/6
# service sshd restart

To connect SSH on custom port, use the following command

# ssh -P 12345 root@192.168.0.1

How to use Rsync on Custom Port

To use rsync on custom port from local directory to remote directory

# rsync -arvz -e 'ssh -p 12345' --progress /path/to/local ssh_user@host:/parent/path/remote

Example you want to sync /home/website_dir to server 192.168.0.1

# rsync -arvz -e 'ssh -p 12345' --progress /home/website_dir root@192.168.0.1:/home

To use rsync on custom port from remote directory to local directory

# rsync -arvz -e 'ssh -p 12345' --progress ssh_user@host:/parent/path/remote /path/to/local

Example you want to sync /home/website_dir on 192.168.0.1 to local directory

# rsync -arvz -e 'ssh -p 12345' --progress root@192.168.0.1:/home /home/website_dir

Hope this helps.

ServerDiary

ServerDiary

Leave a Reply

Your email address will not be published. Required fields are marked *