Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 6 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
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
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:… – 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" /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 '14 at 19:05
I know this is old but, If you want to avoid the password in the command you can use key's for ssh and on the remote machine add your user to the sudoers file with the NOPASSWD: ALL flag. See the highest rank answer at the following link. NOTE: It's not the accepted answer: – Dave Dec 15 '15 at 9:16

The solution on this blog worked really well for me:


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 '14 at 19:10
I my case I have already unset: tty_tickets anyway... so I may try this approach. – MountainX May 17 '14 at 23:43
sudo: no tty present and no askpass program specified – Michael Apr 15 at 3:08

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

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
I used sudo rsync option without setting username ALL... after rsync done. on the remote when I try to cd or ls. I get permission denied. even though I'm root. i have to sudo to ls. but in case of cd it is even not possible. I tried to chown all directories and files. still didn't work. anybody know the reason and how to fix it? – Dreaded semicolon Jun 30 '15 at 18:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .