When trying to solve this same problem in the ansible synchronize module, I came across this question and would like to draw some attention to the solution https://askubuntu.com/a/1263657/874618 by Simon Schmid. Can't upvote or comment using my account (not enough reputation points), so I'll post this as another answer:
Simon's solution not only is the safest alternative in that it requires no changes to sudoers and passes the password using the environment (so that it will not pop up in history files and logs), it also works without changes to the target machine, which is very convenient if you want to rsync to lots of fresh machines and do not want to reconfigure them just for this command.
After playing around with it a bit, I've managed to inline the shell commands used for wrapping ssh on the host machine as well, so it is possible to just use a single rsync line:
PASSWORD=<SUDOPASS> rsync -avzue '/bin/sh -c "{ echo $PASSWORD; cat - ; } | ssh $0 $* &"' --rsync-path "sudo -S rsync" SRC DST
Happy syncing ;-)
-e="ssh -i $PRIVATE_KEY_PATH" --rsync-path="sudo rsync"– A T May 15 '17 at 06:01/etc/sudoerscomes at the end of the file (or after any group rules which may affect the same user) solved the problem for me. – CPBL Jan 28 '19 at 01:15<username> ALL=NOPASSWD:<path to rsync> <args>but I don't know what the args would be when called remotely like this. Any ideas? – Pete Cornell Sep 12 '19 at 01:00Defaults requirettySSHD configuration on the remote side. – TrinitronX Jun 08 '20 at 21:31alias rsync-sudo='rsync --rsync-path="sudo rsync"'– kenorb Aug 03 '21 at 09:02sudoers. I set<username> ALL=(ALL)NOPASSWD: /usr/bin/rsyncin/etc/sudoers.d/usernameand expected that is would do the job. Turned out in my/etc/sudoersthere was a%some-usergroup ALL=(ALL) ALLline (username is a member of that some-usergroup) after@includedir /etc/sudoers.d. Moving the%some-usergroup <..>line before@includedirfixed my remotesudo rsyncproblems. – DmitrySandalov Sep 15 '22 at 07:10