I have read several articles how to automatically backup files with Rsync and public key authentication. All of them are very similar. I just finished setting up everything and everything works fine but... I just found an article which says it's not secure. I did the following:
- On backup server I generated public and private keys.
- I copied public key to the remote (original) servers directory:
/var/sites/.ssh
(fileauthorized_keys
). The directory is owned by"user12"
- I added the following to the authorized_keys file:
from="BACKUP.SERVERS.IP.ADDRESS",command="/root/validate_rsync"
I created a file /root/validate_rsync with the following content:
#!/bin/sh echo $SSH_ORIGINAL_COMMAND >> /var/log/synchronize-log.log case "$SSH_ORIGINAL_COMMAND" in *\&*) echo "Rejected" ;; *\;*) echo "Rejected" ;; *\(*) echo "Rejected" ;; *\{*) echo "Rejected" ;; *\<*) echo "Rejected" ;; *\>*) echo "Rejected" ;; *\`*) echo "Rejected" ;; *\|*) echo "Rejected" ;; rsync\ --server*) $SSH_ORIGINAL_COMMAND ;; *) echo "Rejected" ;; esac
I run the rsync command:
rsync -avzp --del -e "ssh -p 2211" user12@ORIGINAL.SERVERS.IP:/var/sites/photos/ /var/sites/sync/photos
I got error message: permission problems with file /root/validate_rsync
. I moved the file /root/validate_rsync
to /var/sites/validate_rsync
and chowned it to user12:user12
Now synchronization works. But I found an article which says it's insecure:
1- the validate_rsync command itself should not be owned nor writeable by the userid that executes the rsync command. Otherwise, rsync can be used to overwrite the validation script with another script that doesn't validate, or even execute arbitrary commands.
2- similarly, the authorized-keys file should not be owned or writeable by the rsync user, otherwise rsync can be used to overwrite that file, with one that removes the requirement to run validate-rsync, or with one that runs some other command instead.
What can I do? If validate_rsync
is owned by root, the synchronization does not start because user12
can't access root
's files. If authorized-keys
file will be owned by another user I will not be able to login with username user12
.
My questions:
Where should I put validate_rsync and authorized-keys files, in which directory? What permissions and ownerships should they have?
Is there some way how to tell to the
validate_rsync
file to allow to synchronize only 2 folders:/var/sites/photos/
,/var/sites/photos2/