Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syncing local and remote directories using rsync+ssh+public key as a user different to the ssh key owner

The goal is to sync local and remote folders over ssh.

My current user is user1, and I have a password-less access setup over ssh to a server server1. I want to sync local folder with a folder on server1 by means of rsync utility. Normally I would run:

rsync -rtvz /path/to/local/folder server1:/path/to/remote/folder

ssh access works as expected, rsync is able to connect over ssh, but it returns "Permission denied" error because on server1 the folder /path/to/remote/folder is owned by user2:user2. File permissions of the folder do not allow it to be altered by anyone else. user1 is a sudoer on server1 so sudo su - user2 works during ssh session. How to forse rsync to switch the user when it ssh'ed to the server?

Adding user1 to the group user2 is not an option because all user/group management on the server is done automatically and replicated from a central repo every X mins, that I have not access to.

Same for changing permissions/ownership of the destination folder: it is updated automatically on a regular basis with a reset of all permissions.

Possible solution coming to my mind is a script that syncs the local folder with a temporary intermediate remote folder owned by user1 on the server, and then syncs two remotes folders as user2.

Googling for a shorter and prettier solution did not yield any success.

like image 447
schatten Avatar asked Jan 16 '13 04:01

schatten


People also ask

How do I use rsync with SSH keys?

Using rsync With SSH and Your Key If you want details on what SSH is doing, add "-v" to the ssh options. To run rsync quietly, remove the "-v" option from both rsync and SSH option list. To reverse the synchronization so the remote file is updated on your local computer, reverse the source and destinations.

How do I keep two directories in sync with rsync?

In order to keep two directories truly in sync, it's necessary to delete files from the destination directory if they are removed from the source. By default, rsync does not delete anything from the destination directory. You can change this behavior with the --delete option.

How does rsync work with SSH?

Rsync stands for "Remote Sync." The rsync command lets you transfer and synchronize data between different machines and directories. Using the Secure Shell (SSH) protocol, you can copy your files securely to another location. The rsync tool has many benefits when compared to other methods for copying files.

Can rsync sync both ways?

rsync works in one direction, so we need to run it twice to sync directories in both directions.


2 Answers

I have not tried it by myself, but how about using rsync's '--rsync-path' option?

rsync -rtvz --rsync-path='sudo -u user2 rsync' /path/to/local/folder server1:/path/to/remote/folder
like image 189
ymnk Avatar answered Oct 27 '22 10:10

ymnk


To fix the permissions problem you need to run rsync over an an SSH session that logs in remotely as user2:

rsync avz -e 'ssh -i privatekeyfile' /path/to/local/folder/ user2@server1:/path/to/local/folder

The following answer explains how to setup the SSH keys.

  • Ant, download fileset from remote machine
like image 28
Mark O'Connor Avatar answered Oct 27 '22 09:10

Mark O'Connor