Rsync over SSH with Key file and sudo user

Normally, I used to create config files related to rsync (rsyncd.conf) when rsyncing from 1 server to another.

Finally, after lot of tinkering and testing, I have setup rsync over SSH with key file (password less access) and sudo user (because root user is DANGEROUS).

Ok, we need to follow a set of steps to achieve this, covered below:

  • Open port 873 both source and destination.
  • Run rsync –daemon on source.
    We need to enable rsync command to run without password. For this, we have to edit sudoers file, on destination, as root user (need to be careful here)
  • visudo
  • Add the following line at the end of the file (ec2-user is the sudo user):
    1
    ec2-user ALL= NOPASSWD:/usr/bin/rsync
  • Then use the –rsync-path option to specify the sudo in the rsync command.
  • Run the following command to test the working of the rsync:
    1
    rsync -n -av -e 'ssh -i key.pem' --rsync-path="sudo rsync" ec2-user@192.168.0.123:/opt/data/ /opt/data/
  • If the above command works, remove the -n parameter to start the actual sync.

Feel free to comment in case of any issue.

linux ubuntu ec2 centos root daemon rsync sudo