Andreas Vöst
26 days
Claus-Theodor Riegg
4 years
Andreas Vöst
2 months
Andreas Vöst
2 months
Kim Klotz
3 months
Claus-Theodor Riegg
8 years
Andreas Vöst
5 months
Andreas Vöst
7 months
Claus-Theodor Riegg
8 years
Marc Dierig
9 months
Claus-Theodor Riegg
7 years

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.