44

I´m trying to set owner and group via rsync and it doesn't seem to be working.

This is the command:

sudo rsync -rlptDvz --owner=cmsseren --group=cmsseren /home/serena/public_html/ -e ssh root@ip:/home/cmsseren/public_html2/

The files sync correctly but doesn´t seem to change the owner and group.

Aaron Copley
  • 12,345
  • 5
  • 46
  • 67
Arnoldo Bazaldua
  • 539
  • 1
  • 4
  • 5

5 Answers5

96

Version 3.1.0 of rsync introduced the --usermap and --groupmap mentioned by Thomas, but also the convenience option --chown, which works well for your scenario.

--chown=USER:GROUP
    This option forces all files to be owned by USER with group GROUP.
    This  is  a  simpler  interface  than  using  --usermap  and  --groupmap directly,
    but  it  is implemented using those options internally, so you cannot mix them.
    If either the USER or GROUP is empty, no mapping for the omitted user/group will
    occur.  If GROUP is empty, the trailing colon may be omitted, but if USER is
    empty, a leading colon must  be supplied.

    If you specify "--chown=foo:bar, this is exactly the same as specifying
    "--usermap=*:foo --groupmap=*:bar", only easier.

Also, the -o and -g options are required. Excluding them will fail to update their respective attribute, but produce no error.

rsync -og --chown=cmsseren:cmsseren [src] [dest]

This is mentioned indirectly in the manpage, which states that the --chown option "is implemented using --usermap and --groupmap internally", and:

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).

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.

David Thompson
  • 1,136
  • 1
  • 7
  • 7
  • 7
    Why are -og required? It seems illogic to me but it IS required on my system too. – Yai0Phah Jul 03 '15 at 08:39
  • 6
    @FrankScience Agreed, it seems illogical. The same question was [asked on the rsync mailing list](https://lists.samba.org/archive/rsync/2013-October/028753.html). *"I would suggest that the docs for `--chown` would do well to mention that `--owner` (`--super`) and `--group` are needed"*. – TachyonVortex Nov 20 '15 at 12:58
  • 1
    thanks for the hint that `-og` may be required! – karlsebal Dec 18 '19 at 12:14
  • It does not work for me even with `-og` – Flimm Jul 15 '20 at 17:43
  • I spent half an hour on this, it is absoultely ludicrous. It is indeed mentioned in the documentation, but given the size and the complexity of the man page it is silly to expect that one will get this requirement from _there_. Luckily we have SO and SE :D – Francesco Marchetti-Stasi Aug 08 '22 at 11:06
22

It sounds like it is working correctly. Use --owner and --group to preserve (not set) the owner and group names... meaning that you do not want them to change after the transfer.

If you don't use these options, the user and group will be changed to the invoking user on the receiving end. If you want to specify some other user, you will need to add a chown command to your script.

-o, --owner
    This option causes rsync to set the owner of the destination file to be 
    the same as  the source file, but only if the receiving rsync is being run 
    as the super-user (see also the --super and --fake-super options). Without 
    this option, the owner of new and/or transferred files are set to the invoking 
    user on the receiving side...

-g, --group
    This option causes rsync to set the group of the destination file to be the same as 
    the source file. If the receiving program is not running as the super-user (or if
    --no-super was specified), only groups that the invoking user on the receiving side
    is a member of will be preserved. Without this option, the group is set to the default
    group of the invoking user on the receiving side...

man rsync

Aaron Copley
  • 12,345
  • 5
  • 46
  • 67
user3150166
  • 415
  • 3
  • 5
  • 1
    So how can I chown user and group remotly? Via ssh? Could you please tell me how? I have root acces for both servers. I don´t want to use ssh keys, but tell me all options you know, I´ll appreciate it. Thanks for quick response – Arnoldo Bazaldua Dec 31 '13 at 21:30
  • The command would look something like this: `ssh root@ip 'chown -R cmsseren:cmsseren /home/serena/public_html2/*'` – user3150166 Dec 31 '13 at 21:45
  • I tried it, but didn´t work, the owners and users are the same. After making the command it says: stdin: is not a tty – Arnoldo Bazaldua Jan 02 '14 at 17:26
  • It works now, firewall issues. Now I just have to automatize the password input for ssh and rsync – Arnoldo Bazaldua Jan 02 '14 at 18:16
  • I´ll post this in otherquestion, thanks for your help – Arnoldo Bazaldua Jan 02 '14 at 18:51
  • `If the receiving program is not running as the super-user...only groups that the invoking user on the receiving side is a member of will be preserved` but I *am* a member of the group the file has the group of, yet it is STILL not changing the group on the target (running this locally,btw) – Michael Mar 03 '16 at 03:48
  • @ArnoldoBazaldua do use SSH with keys. Everywhere. You will be glad you did. – sjas May 19 '16 at 07:30
  • The language you use is not very clear because we can speak of "preserving" the user/group of the source file or of the files on the destination. the flags cause the SOURCE user/group to be preserved... – Steven Lu Dec 28 '16 at 20:49
  • the OP talks about using the --chown rsync option and it not working. The answer was given in another post (you must also specify --owner and ---grooup for --chmod to work. --chmod silently does nothing without those options) – rmeden Jul 21 '22 at 17:23
8

Last version (at least 3.1.1) of rsync allows you to specify the "remote ownership":

--usermap=tom:www-data

Changes tom ownership to www-data (aka PHP/Nginx). If you are using Mac as the client, use brew to upgrade to the last version. And on your server, download archives sources, then "make" it!

Thomas Decaux
  • 1,239
  • 11
  • 13
1

Another solution may be to change the remote user that establishes the rsync connection using --rsync-path. I posted a complete explanation here:

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

Samuel Phan
  • 111
  • 2
0

Issue. Backing up remote files, directories from a cloud VPS via rsync over SSH was not maintaining remote owner:group ownerships on local host.

Solution.

  1. Create users on local host for files (directories, etc.) rsync'd from remote host.
  2. Run local host rsync command as root / sudo.
  3. Will have to manually provide root password, SSH passphrase.
# create local host system user(s) for those on remote host; see:
# https://wiki.archlinux.org/title/Users_and_groups#Example_adding_a_system_user

sudo rsync -aqP  -e "ssh -p 4321  \                   ## SSH port
    -i /home/victoria/.ssh/my-vps/id_rsa"  \          ## SSH credentials
    root@vps1234.my_isp.com:/etc/default/solr.in.sh   ## rsync SRC (remote host)
    /<my home path>/backups/solr.in.sh  \             ## rsync DEST (local host)

Tests.

# -----------------------------------------------------------------------------
# REMOTE HOST (cloud VPS):

[me@vps1234]$ ls -l /etc/default/

  ...
  -rw-r----- 1 root solr 15079 Aug 12 20:35 solr.in.sh    ## ownership: root:solr
  ...

[me@vps1234]$ 

# -----------------------------------------------------------------------------
# LOCAL HOST:

The basic command for testing (with variations, following) is:

```bash
rm -f solr.in.sh;  \
rsync -aqP  \
    --rsync-path="sudo /bin/rsync"  \
    -e "ssh -p 4321  \
    -i /home/victoria/.ssh/my-vps/id_rsa"  \
    root@vps1234.my_isp.com:/etc/default/solr.in.sh
    /<my home path>/backups/solr.in.sh;  \
ls -l solr.in.sh
# -----------------------------------------------------------------------------
# [TEST 1] rsync --rsync-path...

[victoria]$ rm -f solr.in.sh;  \
    rsync -aqP --rsync-path="sudo /bin/rsync"  \
      -e "ssh -p 4321 -i /home/victoria/.ssh/my-vps/id_rsa"  \
      root@vps1234.my_isp.com:/etc/default/solr.in.sh  \
      /<my home path>/backups/solr.in.sh;  \
    ls -l solr.in.sh 

  -rw-r----- 1 victoria victoria 15079 Aug 12 20:35 solr.in.sh
            ## victoria victoria [incorrect]

[victoria]$
# -----------------------------------------------------------------------------
# [TEST 2] sudo rsync --rsync-path ; user "solr" not on local host so remote files
# in group "solr" renamed "git" on local host; tried rsync -og options: no effect

[victoria]$ rm -f solr.in.sh;  \
    sudo rsync -aqP --rsync-path="/usr/bin/rsync"  \
      -e "ssh -p 4321 -i /home/victoria/.ssh/my-vps/id_rsa"  \
      root@vps1234.my_isp.com:/etc/default/solr.in.sh  \
    /<my home path>/backups/solr.in.sh;  \
    ls -l solr.in.sh 

  [sudo] password for victoria: 
  Enter passphrase for key '/home/victoria/.ssh/my-vps/id_rsa': 

  -rw-r----- 1 root git 15079 Aug 12 20:35 solr.in.sh
            ## root:git [incorrect]

[victoria]$
# -----------------------------------------------------------------------------
# [CONFIGURE USERS]
# local host:

[victoria]$ cat /etc/passwd | grep solr     ## no such user
[victoria]$

# Create local host system user, for user on remote host:

[victoria@victoria backups]$ sudo useradd -r -s /usr/bin/nologin solr
  [sudo] password for victoria: 

[victoria]$ cat /etc/passwd | grep solr

  solr:x:980:980::/home/solr:/usr/bin/nologin

[victoria]$ ls -l /home/     ## check: useradd did not create /home/solr/

  total 52
  drwx------   2 root     root     16384 Mar  6  2015 lost+found
  drwxrwxr-x 178 victoria victoria 36864 Aug 13 10:09 victoria

[victoria]$
# -----------------------------------------------------------------------------
# [TEST 3: works] sudo --rsync-path...

[victoria]$ rm -f solr.in.sh;  \
    sudo rsync -aqP --rsync-path="/bin/rsync"  \
      -e "ssh -p 4321 -i /home/victoria/.ssh/my-vps/id_rsa"  \
      root@vps1234.my_isp.com:/etc/default/solr.in.sh  \
      /<my home path>/backups/solr.in.sh;  \
    ls -l solr.in.sh 

  [sudo] password for victoria: 
  Enter passphrase for key '/home/victoria/.ssh/my-vps/id_rsa': 

  -rw-r----- 1 root solr 15079 Aug 12 20:35 solr.in.sh
            ## root:solr [correct]

[victoria]$
# -----------------------------------------------------------------------------
# [TEST 4: works] No need for --rsync-path option:

[victoria@victoria backups]$ rm -f solr.in.sh;  \
    sudo rsync -aqP -e  \
      "ssh -p 4321 -i /home/victoria/.ssh/my-vps/id_rsa"  \
      root@vps1234.my_isp.com:/etc/default/solr.in.sh  \
      /<my home path>/backups/solr.in.sh;  \
    ls -l solr.in.sh

  [sudo] password for victoria:
  Enter passphrase for key '/home/victoria/.ssh/my-vps/id_rsa':

  -rw-r----- 1 root solr 15079 Aug 12 20:35 solr.in.sh
            ## root:solr [correct]

[victoria@victoria backups]$

# -----------------------------------------------------------------------------