Use sudo with rsync

If you want to use rsync to transfer files that don't belong to your user to another system you can combine it with sudo. This doesn't require a direct root login on the source or target system.

Command

ssh -A -t $SOURCE_SERVER \
  'sudo -E rsync -avPhn --rsync-path "sudo rsync" \
    /var/lib/foo/ \
    $USER@$TARGET_SERVER:/var/lib/bar/'

Explanation

  • ssh -A: Forward SSH agent
  • -t: Interactive shell for accepting the SSH fingerprint
  • sudo -E rsync: Preserve the environment on the source system to use the forwarded SSH agent
  • --rsync-path "sudo rsync": Call rsync as root on the target system to allow writing the files
  • rsync -n: Dry-run by default. Remove to actually sync
Andreas Vöst