Search code examples
sudorsync

rsync content as sudo user


I have a reseller web server that I am migrating into a new machine. The source server owner provided a user (let's call it user A) which has sudo access to other users, but not to root. My user does not have direct file access to the files I need to pull, but I can sudo to the proper user (user B for our example) and get the right content.

I have an ssh key set up from my user to the target machine, but this obviously does not carry through to the other users. I can pass the password for the target server when sudoed as the individual content users, though for the quantity of data to move and the number of users to sudo to, this is quite tedious.

So, my question is, is there a way I can access content belonging to user B, to which I have sudo access but not direct access, but authenticate my rsync ssh connection and run the full rsync command as user A, my linux user?


Solution

  • You can specify the command to use to run rsync on the remote end with the --rsync-path option, and that command can be sudo with options. For example:

    rsync -a --rsync-path "sudo -u userB rsync" userA@host:/path/to/stuff/ /local/stuff/
    

    You'll need to make sure your sudoers file is set up to provide NOPASSWD access to the rsync command, of course.