Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the owner for a rsync

Tags:

unix

rsync

I understand preserving the permissions for rsync. However in my case my local computer does not have the user the files need to under for the webserver. So when I rsync I need the owner and group to be apache on the webserver, but be my username on my local computer. Any suggestions?

I wanted to clarify to explain exactly what I need done.

My personal computer: named 'home' with the user account 'michael' My web server: named 'server' with the user account 'remote' and user account 'apache'

Current situation: My website is on 'home' with the owner 'michael' and on 'server' with the owner 'apache'. 'home' needs to be using the user 'michael' and 'server' needs to be using the user 'apache'

Task: rsync my website on 'home' to 'server' but have all the files owner by 'apache' and the group 'apache'

Problem: rsync will preseve the permissions, owner, and group; however, I need all the files to be owner by apache. I know the not preserving the owner will put the owner of the user on 'server' but since that user is 'remote' then it uses that instead of 'apache'. I can not rsync with the user 'apache' (which would be nice), but a security risk I'm not willing to open up.

My only idea on how to solve: after each rsync manually chown -R and chgrp -R, but it's a huge system and this takes a long time, especially since this is going to production.

Does anyone know how to do this?

Current command I use to rsync:

rsync --progress -rltpDzC --force --delete -e "ssh -p22" ./ [email protected]:/website 
like image 259
Michael Ozeryansky Avatar asked Jun 21 '12 04:06

Michael Ozeryansky


People also ask

Does rsync preserve ownership?

'rsync -a' option preserves the permissions, ownership, timestamp of files and folders that are to be transferred using rsync. This will synchronize the two folders or files and will also maintain the same timestamp as that of the source.

How do I run rsync as another user?

if you can sudo -u www-data then you can run rsync with that user. if your destination is on a different server, then you also need to set --rsync-path to the same (sudo -u www-data rsync).

How do I change the directory in rsync?

Method 1: Updating newer files locally with rsyncThe “-u” or the “–update” option forces rsync to skip that files in your destination directory that are still new, and the “-n” or the “–dry run” option is for testing the update process. Now, remove the “-n” option and quickly update the rsync command execution.

Does rsync need to be on both servers?

The rsync utility must be installed on both the client and server machine before getting started. Rsync has two modes: local and remote. If both machines are on the same network, the local mode is used. This uses minimal transfer security and can rapidly perform transfers and synchronization over LAN.


2 Answers

If you have access to rsync v.3.1.0 or later, use the --chown option:

rsync -og --chown=apache:apache [src] [dst] 

More info in an answer from a similar question here: ServerFault: Rsync command issues, owner and group permissions doesn´t change

like image 107
David Thompson Avatar answered Sep 20 '22 05:09

David Thompson


There are hacks you could put together on the receiving machine to get the ownership right -- run 'chmod -R apache /website' out of cron would be an effective but pretty kludgey option -- but instead, I'd recommend securely allowing rsync-over-ssh-as-apache.

You'd create a dedicated ssh keypair for this:

ssh-keygen -f ~/.ssh/apache-rsync 

and then take ~/.ssh/apache-rsync.pub over to the webserver, where you'd put it into ~apache/.ssh/authorized_keys and carefully specify the allowed command, something like so, all on one line:

command="rsync --server -vlogDtprCz --delete . /website",from="IP.ADDR.OF.SENDER",no-port-forwarding,no-X11-forwarding,no-pty ssh-rsa AAABKEYPUBTEXTsVX9NjIK59wJ+fjDgTQtGwhATsfidQbO6u77dbAjTUmWCZjKAQ/fEFWZGSlqcO2yXXXXXXXXXXVd9DSS1tjE6vAQaRdnMXBggtn4M9rnePD2qlR5QOAUUwhyFPhm6U4VFhRoa3wLvoqCVtCV0cuirB6I45On96OPijOwvAuz3KIE3+W9offomzHsljUMXXXXXXXXXXMoYLywMG/GPrZ8supIDYk57waTQWymUyRohoQqFGMzuDNbq+U0JSRlvLFoVUZ5Piz+gKJwwiFwwAW2iNag/c4Mrb/BVDQAyEQ== [email protected] 

and then your rsync command on your "home" machine would be something like

rsync -av --delete -e 'ssh -i ~/.ssh/apache-rsync apache@server' ./ /website 

There are other ways to skin this cat, but this is the clearest and involves the fewest workarounds, to my mind. It prevents getting a shell as apache, which is the biggest security concern, natch. If you're really deadset against allowing ssh as apache, there are other ways ... but this is how I've done it.

References here: http://ramblings.narrabilis.com/using-rsync-with-ssh, http://www.sakana.fr/blog/2008/05/07/securing-automated-rsync-over-ssh/

like image 43
Adam Avatar answered Sep 18 '22 05:09

Adam