The Computer Oracle

Run rsync with root permission on remote machine

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Ominous Technology Looping

--

Chapters
00:00 Run Rsync With Root Permission On Remote Machine
00:33 Accepted Answer Score 13
00:48 Answer 2 Score 11
01:11 Answer 3 Score 39
01:38 Answer 4 Score 15
02:35 Thank you

--

Full question
https://superuser.com/questions/270911/r...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#ubuntu #rsync #sudo

#avk47



ANSWER 1

Score 39


Try this solution. In your sudoers file (/etc/sudoers) setup your user like this:

username ALL= NOPASSWD:/usr/bin/rsync

the NOPASSWD:/usr/bin/rsync tells sudo that when your user runs /usr/bin/rsync or just rsync that no password is needed.

Then your original --rsync-path="sudo rsync" should work.




ANSWER 2

Score 15


I'm amazed by the complexity of the existing answers! It's far easier to configure the remote host so that you can connect as root from specific clients without using a password. And although it may sound scary, it is quite secure.

  1. On the remote host make sure that /etc/ssh/sshd_config has this line PermitRootLogin without-password (in many distributions it's there by default). This allows root to get an ssh shell using any authentication method except the insecure password prompt.
  2. (If you don't already know how) follow any of the many tutorials on how to obtain passwordless login via ssh
  3. Use rsync as you would normally do and without any password prompts.

Just don't forget that after step 2, and for as long as you don't remove the line that was added in /root/.ssh/authorized_keys, your client can connect as root to the remote host.




ACCEPTED ANSWER

Score 13


This is the solution I came up with:

rsync -R -avz -e ssh --rsync-path="echo mypassword | sudo -S  mkdir -p /remote/lovely/folder && sudo rsync" /home/ubuntu/my/lovely/folder ubuntu@x.x.x.x:/remote/lovely/folder --delete

Bit of a mission!




ANSWER 4

Score 11


The solution on this blog worked really well for me: http://www.pplux.com/2009/02/07/rsync-root-and-sudo/.

Basically:

stty -echo; ssh myUser@REMOTE_SERVER "sudo -v"; stty echo  
rsync -avze ssh --rsync-path='sudo rsync' myUser@REMOTE_SERVER:/REMOTE_PATH/ LOCAL_PATH 

The first line allows for interactive password entry, without showing the password on the screen. Works great for me on Ubuntu 9.04.