I received some mails about how to exactly configure rsnapshot/rsync to use another user than root to do remote backups as described in my article.
Im my article I wrote:
But using root for this work is not secure because the ssh-key can not be protected by a passphrase as it is used automatically in the background. Therefore create a backup-user on the remote host for this task and allow him to use sudo rsync without a password in /etc/sudoers. Really paranoid can then also allow only this command in .ssh/autorized_keys. See the rsnapshot mailling list for more information on how to do this.
The danger of using root for the backups is that when somebody hacks your backup-server he will gain access to all SSH private keys. With these he will be able to login to your servers as the private keys are configured to use no password. And if the user that for that key on the remode side is root, you’re lost. As the information on the rsnapshot mailing list is maybe not enough, I will describe in detail how to configure rsnapshot without using root. A hacker with access to the SSH private keys will only be able to backup your files.
First create a backup user on the production server (that means the server you want to backup).
remote# adduser
Enter the desired information. Make sure that this user in not in the group wheel as this should be a non-priviledged user, that is the hole reason behind this article.
As described in this article on securityfocus.com, create a ssh-key for the user on the local machine and copy the public key to the remote server.
local# ssh-keygen -t rsa
local# scp id_rsa.pub ssh-remote-server:id_rsa_backup.pub
local# ssh ssh-remote-server
remote# cat id_rsa_backup.pub >> authorized_keys
The last command will tell sshd to allow connections without a password to this account if they have the private key to this public key. So far, nothing new. Now instead of using root we ca now login with the user backup without a password to the remote host. So lets tell rsnapshot to use our new backup user:
Change
backup root@www.example.com:/etc/ www.example.com/etc/
to
backup backup@www.example.com:/etc/ www.example.com/etc/
Are we done now? Unfortunately no as the user backup has not the rights to do the backup. He is not root and therefore does not have access to many files like special files in /etc or the files of other users.
We need to allow the backup user to have access to all files when using rsync .
Therefore we need to install sudo on the remote server and allow the backup user to call sudo rsync without using a password.
On FreeBSD
remote# cd /usr/ports/security/sudo
remote# make install clean
remote# echo “backup remote = NOPASSWD: /usr/local/bin/rsync_real” >> /usr/local/etc/sudoers
Of course you should use visudo to edit sudoers (the config file of sudo).
This line will allow the local user backup to use the command /usr/local/bin/rsync_real as root without a password.
But wait, why rsync_real and not rsync? rsnapshot will call rsync but we have to call sudo rsync in order to gain root privileges for this command. So we have to create a wrapper script that will call sudo rsync for rsnapshot as rsnapshot is not aware of sudo.
remote# cd /usr/local/bin/
remote# mv rsync rsync_real
Now create the wrapper script:
remote# cat /usr/local/bin/rsync
#!/bin/sh
/usr/local/bin/sudo /usr/local/bin/rsync_real ”$@”
Now rsnapshot should be able to use the remote user backup to backup using rsync over ssh. But we can do more. As I wrote:
Really paranoid can then also allow only this command in .ssh/autorizedkeys
Edit /home/backup/.ssh/authorized_keys to allow the backup user that is logged in through the ssh private key only a certain command and to connect form only certain hosts.
remote# cat .ssh/authorized_keys
from=”backup.example.com”, command=”/home/backup/validate-rsync.sh” ssh-rsa AADDB39z….....
Why not allowing /usr/local/bin/rsync? Because sshd will only allow the command as written in the authorized_keys file, the problem is that we want to call rsync with different arguments and this is not possible with the command=”” directive in authorized_keys. Therefore we have to create another wrapper and call the real (better the other wrapper) rsync. Luckily sshd will set the original command issued over SSH as an variable () that we can use in the vaildate-rsync.sh script.
I got the following validate-rsync.sh from here (notice my change in the rsync-case).
remote# chown backup validate-rsync.sh
remote# chmod u+x validate-rsync.sh
remote# cat validate-rsync.sh
#!/bin/sh
case ”$SSH_ORIGINAL_COMMAND” in
&)
echo “Rejected”
;;
;*)
echo “Rejected”
;;
rsync)
$SSH_ORIGINAL_COMMAND
;;
*)
echo “Rejected”
;;
esac
Puh! Hard work for the paranoid. Let’s walk through the hole process again.
The backup machine will connect to the remote server through rsync over ssh using the user backup (backup@remote in rsnapshot.conf). The connection will use the private key of the user on the backup machine to identify itself to the remote sshd (.ssh/authorized_keys). The SSH server will only allow this connection form certain hosts (from-directive) and limit the user to the validate-rsync.sh command (command-directive).
Now the validate-rsync.sh script will call rsync with the arguments that the backup user originally used (rsnapshots commands). But we have to use sudo in order to gain access to all files so rsync is another wrapper around sudo rsync_real.
In the end rsync_real (the real rsync :-) is called with super-user rights and will actually do the backup.
More complicated then just using root? Yes
More secure? Definitely!
Truly paranoid can further edit the validate-rsync.sh script to validate the exact rsync calls (check rsnapshot.conf). Now a hacker will only be able to backup your files to your server!
UPDATE:
There is an interesting thread on the rsnapshot mailing list with some improvements on my HOWTO.
Thanks to Jason for pointing it out.
