Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does rsync preserve ownership when uid/gid differs?

Upon deploying a new server and migrating the entire contents of /home using rsync I noticed that the group and user ownership were in fact preserved, despite the fact that the ids differ between the two servers.

Specifically, the command I ran was:

rsync -avz oldserver:/home/ /home

Though I have recreated all of the same users and groups on the new server, most of them have different ids than the old, but somehow this command has magically kept the correct ownership (based on name) and assigned new gid and uid where applicable. I am able to verify this with ls -n /home.

How is this possible? Does rsync do some kind of name lookup?

like image 962
Eaten by a Grue Avatar asked Aug 28 '18 23:08

Eaten by a Grue


1 Answers

Yes, by default rsync matches owners and groups by name. Details are in the docs for --numeric-ids.

--numeric-ids

With this option rsync will transfer numeric group and user IDs rather than using user and group names and mapping them at both ends.

By default rsync will use the username and groupname to determine what ownership to give files. The special uid 0 and the special group 0 are never mapped via user/group names even if the --numeric-ids option is not specified.

If a user or group has no name on the source system or it has no match on the destination system, then the numeric ID from the source system is used instead.

rsync presumably uses getpwnam and getgrnam to look up the uid and gid associated with the user and group names.

like image 152
Schwern Avatar answered Jan 04 '23 00:01

Schwern