Asterisk Blacklist Hobby webbhotell


Access rsnapshout

Filed under: Server — Mathz @ 13:50

Root, sudo, and rsnapshot


The Mission:

To rsync and/or rsnapshot both normal and protected/restricted files from one
server to another over ssh without enabling remote root access to either
server while maintaining original file attributes and permissions. Whew.

Cast of Characters

  • uno: the server which will store the backups, run rsync or
  • zero: the server to be backed up, with root readable files (/etc for


  • root on both uno and zero, hopefully via sudo and not by remote
    root ssh access!


The command examples here are specific to Debian (and therefore Ubuntu), so
adjust to your distribution accordingly (though little if any adjustment should
be necessary).

Server: uno (backup server)

Create new backup user, lets call it rbackup:

$ sudo adduser rbackup 

Login as rbackup, create an ssh key pair:

$ su rbackup $ ssh-keygen -t rsa 

If you are running sshd on a non-standard port (like I am), as user rbackup
create a file named config in ~/.ssh. In this file, enter the host and port:

Host zero Port 12345 

While still user rbackup, copy the public key somewhere publicly

As your regular user (which already has ssh access to zero), send rbackup’s
public key from uno to zero:

$ scp /home/rbackup/ zero:~/ 

Become root, sudo -i. If it doesn’t exists, make root’s ssh directory, set
the correct permissions, create a ssh config file:

# mkdir .ssh # chmod 700 .ssh # cd .ssh # vim config 

In root’s .ssh/config file:

Host zero-rsync Port 12345 Hostname zero User rbackup IdentityFile /home/rbackup/.ssh/id_rsa 

Save the file, set permissions:

# chmod 600 config 

Server: zero (server to be backed up)

Create new backup user, lets call it rbackup:

$ sudo adduser rbackup 

Make uno’s public key available to user rbackup.

Login as rbackup, create a .ssh directory, set permissions, create an
authorized_keys file:

$ su rbackup $ cd $ mkdir .ssh $ chmod 700 .ssh $ cd .ssh $ cat /home/regularuser/ > authorized_keys $ chmod 600 authorized_keys 

Now we want to limit the use of this authorized key by allowing connections
only from uno, and allowing one command only. Edit the key, and add something
like this to the beginning of the key:

from="",command="/home/rbackup/" ssh-rsa AX ...remainder of key...rbackup@uno 

While still user rbackup, create a script named in your
home directory:

$ cd $ vim 

Contents of

#!/bin/sh case "$SSH_ORIGINAL_COMMAND" in *\&*) echo "Connection closed" ;; *\;*) echo "Connection closed" ;; rsync*) $SSH_ORIGINAL_COMMAND ;; *true*) echo $SSH_ORIGINAL_COMMAND ;; *) echo "Connection closed." ;; esac 

As your regular user (which can sudo):

$ sudo visudo 

Add this line to the bottom:

rbackup ALL=NOPASSWD:/usr/bin/rsync 

If you have the AllowUsers directive set for sshd in /etc/ssh/sshd_config,
make sure to add the user rbackup to the list, and restart sshd.

As root or with sudo, create a simple rsync wrapper, named at /usr/local/bin,

#!/bin/sh /usr/bin/sudo /usr/bin/rsync "$@"; 


Test 1

Become user rbackup on uno, attempt ssh to zero:

$ ssh zero 

Expected response:

Connection closed. Connection to zero closed. 

The “Connection closed.” with the period at the end tells us the worked as expected (echoing the last Connection closed).

Test 2

Become root on uno, attempt to ssh to zero-rsync (the alias set in root’s

# ssh zero-rsync 

Expected response:

Connection closed. Connection to zero closed. 

Test 3

Become root on uno, attempt to rsync something on zero that is restricted:

# rsync -ae ssh --rsync-path='' zero-rsync:/etc /home/regularuser/tmp/ 

Expected response: you should have a copy of zero’s /etc directory in
regular users tmp directory (or wherever). Important things to note in the above
command – the –rsync-path switch and using zero-rsync as the host instead of
just zero.

Using rsnapshot

If the above tests all work, setting up rsnapshot is easy. Check
any other guide for general setup info, the relevant stuff for us to use in our
rsnapshot.conf is:

rsync_long_args --delete --numeric-ids --relative --delete-excluded backup rbackup@zero-rsync:/etc zero/ 

Remember the rsnapshot.conf file needs tabs. The rsync_long_args setting is
rsnapshot’s default rsync arguments, with our
added. The backup command has our backup user (rbackup) and the host alias
set in root@uno’s .ssh/config.

Extra sshd_config parameters

AuthorizedKeysFile .ssh/authorized_keys2


This was pieced together from various notes, blogs, and articles.
Some script source and extra-helpful information was found at:


Ubutnu as PDC

Filed under: Server — Mathz @ 18:25



Filed under: Hobby — Mathz @ 06:58

[nggallery id=1]

Powered by WordPress