Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 ubuntu@x.x.x.x:/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
share|improve this question
    
Can you not change the permissions on the remote folder so that your user has write access to it? –  Phil Apr 14 '11 at 19:34
    
Unfortunately that isn't an option. –  Peter Apr 15 '11 at 8:11

4 Answers 4

You need a method to supply the password to sudo. An askpass program is designed to ask for passwords when the normal mechanisms aren't available. Setting up sudo to not require a password to run rsync as your userid is one option.

I normally configure key based login with appropriate restrictions for cases like this. If you configure a restricted key that an only run rsync as root then this kind of thing gets easier to do. Another alternative is to use an rsycnd process to handle the remote requests. The configuration provides a variety of restrictions that can be applied.

EDIT: I included a script to setup keys for key based loings in the Creating Userids on Clients section of my post on Setting up BackupPC on Linux. See also the documenation for ssh_config which details some of the things you can do with resticting key usage as shown in the script.

share|improve this answer
    
Thanks for your help, but I found a solution that works better for me. –  Peter Apr 15 '11 at 15:20
    
any details about keybased login? –  TheVillageIdiot May 9 '11 at 10:29
up vote 5 down vote accepted

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!

share|improve this answer
4  
I hope you recognize your password will be visible in the command line. It should be visible for the length of time the rsync is running. –  BillThor Apr 16 '11 at 2:02
    
I know, but thank you :) –  Peter Apr 16 '11 at 18:25
1  
where is the password visible? Just on the local box in the command line? Or does this approach create remote vulnerabilities too? I'm trying to understand the answer above and the ramifications of using this solution. I asked a question here: superuser.com/questions/398146/… –  MountainX Mar 7 '12 at 21:15
    
@MountainX It'll be visible in list of processes (e.g. ps aux). Test: rsync -R -avz -e ssh --rsync-path="find / > /dev/null && rsync" server.example.com:/ /tmp/example, then open terminal on remote machine and ps aux | grep find. find / is used only as it's the first thing that came to my mind with large execution duration. –  Ivan Vučica May 17 at 19:05
    
@IvanVučica thanks for the good explanation –  MountainX May 17 at 23:42

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.

share|improve this answer
    
This requires the tty_tickets option to be unset: !tty_tickets. –  blueyed Dec 19 '11 at 13:12
    
How about ssh -t [other options] instead of playing with stty -echo? –  Ivan Vučica May 17 at 19:10
    
I my case I have already unset: tty_tickets anyway... so I may try this approach. –  MountainX May 17 at 23:43

Try this solution. In your sudoers file 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.