Andreas Vöst
18 days
Kim Klotz
1 month
Andreas Vöst
2 months
Andreas Vöst
5 months
Marc Dierig
7 months
Stefan Langenmaier
9 months

Use sudo with rsync

Updated . Posted . Visible to the public. Repeats.

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
Last edit
Stefan Langenmaier
License
Source code in this card is licensed under the MIT License.