Run rsync with root permission on remote machine
I want to sync a folder from my machine with a folder on a remote machine. The remote folder can only be manipulated by root
. I have an account on the remote machine which can use sudo
. How can I run rsync such that it has root permissions on the remote machine?
I've tried the following:
rsync -avz -e ssh /home/ubuntu/my/lovely/folder [email protected]:/remote/lovely/folder --delete --rsync-path="sudo rsync"
But (after entering my password) I get the following error:
sudo: no tty present and no askpass program specified
Solution 1:
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.
Solution 2:
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 [email protected]:/remote/lovely/folder --delete
Bit of a mission!
Solution 3:
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.
Solution 4:
I'm amazed by the complexity of the existing answers! It's far easier and convenient to configure your systems (your PC and the remote host) so that you can connect as root to the remote host without using a password. And although it sounds scary it is quite secure.
- 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.
- (If you don't already know how) follow any of the many tutorials on how to obtain passwordless login via ssh
- Use rsync as you would normally do and without any password prompts.
Just don't forget that as long as the line in /root/.ssh/authorized_keys of the remote host is there that machine accepts root commands from your PC.