Search code examples
command-linersyncchown

Why does this rsync command change the user correctly, but not the group?


I have a directory on my local machine called foo that contains a file called document. The user and group for foo and document are A:B.

I want to move these files to a remote directory remote-dir using rsync. remote-dir contains other directories, but not /foo, so rsync will be introducing /foo and /foo/document to the remote directory for the first time. The files inside remote-dir should all have a user:group of J:L. Here's what that looks like:

BEFORE

/foo/         (A:B)        /remote-dir/      (J:K)
 `- document  (A:B)         `- other/        (J:L)
                            `- directories/  (J:L)

I used the following rsync command to carry out the transfer*:

rsync -rv --super --owner --chown=J:L ./foo/ root@remote:/remote-dir/foo

EXPECTED

/foo/         (A:B)        /remote-dir/      (J:K)
 `- document  (A:B)         `- other/        (J:L)
                            `- directories/  (J:L)
                            `- foo/          (J:L)
                               `- document   (J:L)

I expect /foo and /foo/document to appear in remote-dir with a user:group of J:L.

ACTUAL

/foo/         (A:B)        /remote-dir/      (J:K)
 `- document  (A:B)         `- other/        (J:L)
                            `- directories/  (J:L)
                            `- foo/          (J:root)
                               `- document   (J:root)

In actuality, /foo and /foo/document appear in remote-dir with a user-group of J:root, root being the group of the root user I specified in the rsync command (root@remote:[...]).

According to the man page, --chown=J:L is equivalent to --usermap=*:J --groupmap=*:L, and as we can see, the usermap is working correctly, but the groupmap is not.

Am I doing anything wrong? Any incorrect assumptions? What should be on my sanity checklist?

*rsync options explained:

  • -r for recursive
  • --super because the "receiver attempts super-user activities" (chown)
  • --owner because chown uses --usermap and --groupmap internally, and these options require --owner to function (edit: actually, this is incorrect. --usermap requires --owner, but --groupmap requires a separate option --group.)

Solution

  • The rsync man page has the answer.

    I was aware of this first part:

    For the --usermap option to have any effect, the -o (--owner) option must be used (or implied), and the receiver will need to be running as a super-user (see also the --fake-super option).

    But I didn't realize I also needed -g (--groupmaps):

    For the --groupmap option to have any effect, the -g (--groups) option must be used (or implied), and the receiver will need to have permissions to set that group.