10

I usually use a graphical SSH client to sync a local and remote directory. And I have to set the ownership of all the files from root to www-data.

Can I do the same with Rsync, only that I do it as www-data instead of root, so that I dont have to set the ownership afterwards?

4 Answers 4

9

It's not clear what you want to do, so let me assume that you want the local files to be owned by user A and the remote files to be owned by user B. If you run the following command as user A:

rsync [options] [email protected]:/path/to/remote/dir /path/to/local/dir

this would have the effect you want. But it's not really an rsync issue (so I'm not elaborating on what the above command will do, I'm focusing on your question); even with your graphical SSH client you should be able to do the same if you run it as user A and set it to log on to the remote system as user B.

5

Provided that you are running as root, use the chown option that is build into rsync:

sudo rsync --chown=www-data:group user@remoteIP:/var/www/html/ /var/www/html

(replace 'group' with your desired group, or just use --chown=www-data, also replace user, remoteIP and change the folders to your liking)

If you don't run the command as root the file ownership will not be changed since you need sudo for that. So you would have to run the command as www-data indeed, but that leads to other complications.

5
  • Not working. ERROR: rsync: --chown=www-data:www-data: unknown option
    – phaberest
    Dec 18, 2015 at 10:37
  • What platform are you on? And what version of rsync do you use? rsync --version
    – Sebastian
    Dec 18, 2015 at 13:00
  • 2
    Mac OS X 10.11 rsyncing on Ubuntu 15.04, rsync version 2.6.9. I solved, by the way, using --owner=www-data and --group=www-data.
    – phaberest
    Dec 18, 2015 at 17:17
  • 1
    Ok glad you solved it, for others having the same issue: the chown option was introduced in version 3.1, so either upgrade or use the solution of phaberest
    – Sebastian
    Dec 18, 2015 at 17:31
  • But watch out, there's a caveat. I have the same problem. But it looks like it's simply using the user and group IDs from the source machine, which may differ, like in my case. It owned the files by a non existent user and the wrong group. Seems like there is no "real" solution... Mar 30, 2018 at 21:47
3

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). you also need to work around the tty issue (by using -t in your ssh), although depending on your distribution and ssh server settings, it could be very tricky. personally i think it is easier just to use root and do a chown afterwards, unless the server does not permit remote root login.

0
0

Overview

(srcmachine)  (rsync)   (destmachine)
  srcuser    -- SSH -->   destuser
                             |
                             | sudo su jenkins
                             |
                             v
                          jenkins

Let's say that you want to rsync:

  • From:
    • Machine: srcmachine
    • User: srcuser
    • Directory: /var/lib/jenkins
  • To:
    • Machine: destmachine
    • User: destuser to establish the SSH connection.
    • Directory: /tmp
    • Final files owner: jenkins.

Solution

rsync --rsync-path 'sudo -u jenkins rsync' -avP --delete /var/lib/jenkins destuser@destmachine:/tmp

Read more here:

https://unix.stackexchange.com/a/546296/116861

1
  • Not sure why this is downvoted. It's complicated, but I don't allow ssh connections as root, so the version in superuser.com/a/874614/227006 isn't going to work for me. I haven't tested this, but it looks right
    – Auspex
    Dec 14, 2023 at 15:21

You must log in to answer this question.

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