Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm using the method described here to copy a directory from one Unix server to another (both Ubuntu 14.04):

rsync -aruv localfolder/ user@server:destination/

I need to keep the same owners and permissions, but the IDs of the users on the new machine may be different. I think that's why the method described doesn't work properly for me: all the files get owner my-user:my-user (supposed I'm using my-user@server) to copy the files to the server.

So, basically, what I need to do is copy the folder (did that), and then, for every file, check what is the ID of the user with the same name on the new machine, and update the owner on the new machine with the new ID.
Is this possible? Or is there a simpler way to do this?

This is what I did in the end, but it's not a neat solution and Michael Kjörling's answer should be preferred.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

This behavior you are looking for is controlled by rsync's -o (--owner) switch, possibly together with --no-numerics-ids. The man page describes --owner thus (my emphasis):

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

The preservation of ownership will associate matching names by default, but may fall back to using the ID number in some circumstances (see also the --numeric-ids option for a full discussion).

There is a similar option -g (--group) which works on groups.

However, both should be implied by -a which means that you need something more. This "something more" is detailed by this little tidbit in the option description, which IMO is rather easy to miss:

only if the receiving rsync is being run as the super-user

So the receiving rsync must run as root. When you think about it, that makes sense, as only root is able to change the ownership of a file to an arbitrary user (which is basically what you are asking for something to do).

Hence, make sure that the remote rsync daemon is run as root, and file ownership (based on user and group names) should be preserved when possible when you use the -a (or -og) switch.

share|improve this answer

This is what I did in the end, but it's not a neat solution.

Fortunately I knew there would be only files owned by users ftp,www-data,my-user,root and groups ftp,www-data,root.

So, on the old machine I ran (in the base of the folder I copied)

find . -user ftp -print > temp-user.ftp.txt
find . -user www-data -print > temp-user.www-data.txt
find . -user my-user -print >
find . -user root -print > temp-user.root.txt
find . -group ftp -print > temp-group.ftp.txt
find . -group www-data -print > temp-group.www-data.txt
find . -group root -print > temp-group.root.txt

Now, for example, temp-user.ftp.txt contains a list of all files owned by the ftp user. And temp-group.ftp.txt is a list of all files with group ftp. For example, temp-user.ftp.txt might look like this:


Use some editor to make it look like this (for example, replace \n by "\nchown *** " and clean up a bit manually at the beginning and end of the file):

chown ftp "./some-file.txt"
chown ftp "./a-directory"
chown ftp "./a-directory/lower-level-file.mp3"

Here, of course, change chown ftp to match the file - temp-group.www-data.txt would get chgrp www-data, etc.

And these files I ran on the new server, with root.

So this worked for me, but again,

  • this is not a nice solution,
  • it requires quite some manual work,
  • and requires that you know what users and groups own files in the directory.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.