I have a remote server and wonder what would be the best way for transferring files to the remote server considering that

  • SSH is only allowed by one user
  • Each dir in /var/www has a specific user as owner.

So I'm looking to rsync but need to specify another command which does the sudo part. Otherwise I'm getting a permission denied.

$> rsync somefile -e "ssh" user@server:/var/www/whichever.site

Now the 'user' does not own the rights to /var/www, but I want to use that user to enter the server and then perform the sudo.

Obviously if there's another 'best practice' for this scenario I'd like to know that.

  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 as long as the line in /root/.ssh/authorized_keys of the remote host is there that machine accepts root commands from your PC.


Try this

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

This will keep two folders synced. Remember source is always first.

Another alternative is that you can add a NOPASSWD in the /etc/sudoers on the remote server like such.

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.

  • I went to edit the sudoers file, stripped down the command a bit just to get it to work. Files are copied now, but the owner+group changes to the SSH user, instead of keeping the real owner. rsync -avuzgo -e "ssh" --rsync-path="sudo rsync" * user@x.y.z.w:/var/www/whichever.site – H.Rabiee Oct 24 '17 at 1:19
  • Awesome. Glad it worked for you. If you don't mind marking it as solved and that this answer helped. – remoteitguy Oct 25 '17 at 18:35
  • it sort of works, the owner+group must be preserved, but they're not :( – H.Rabiee Oct 26 '17 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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