4

I am a moderately new linux user. I changed my PC, and started using CentOS 7 from CentOS 6.

So I attached my previous hard disk to my new pc to take backup of my files. Now, copying the files (and preserving the permissions and all), the files shows owner as 500 (I guess this is my previous UID).

Is there any way I can change them to my new user name? I want to exclude the files which shows some other owners like 501.

Edit:

Example:

ls -l
total 3
-rw-rw-r--.  1 500 500        210 Jan 10  2012 about.xml
drwxr-xr-x.  2 500 500       4096 May 15  2013 apache
drwxrwxr-x.  2 500 500       4096 Dec  9  2012 etc

Now, I can do chown -R xyz:xyz . to make them look like:

ls -l
total 3
-rw-rw-r--.  1 xyz xyz        210 Jan 10  2012 about.xml
drwxr-xr-x.  2 xyz xyz       4096 May 15  2013 apache
drwxrwxr-x.  2 xyz xyz       4096 Dec  9  2012 etc 

But I just want to know if there are some kind of commands which can map user 500 to user "xyz".

Thank you.

  • Are you simply asking how to change file permissions? What is your current UID? Why would you preserve ownership of files if you're just going to change it again? Why not just run a simple copy which will also update the file ownership? Please edit your question and clarify. – terdon Sep 23 '14 at 8:28
  • Running a simple copy (being root) would make the ownership of the files as root, right? I want to copy the files with owner 500 and change the ownership to "xyz". And the UID of "xyz" is 1000. – waiting_for_peace Sep 23 '14 at 8:58
  • That depends on a lot of details like the filesystem you are using, the way your disk is mounted etc. But yes, in general, copying will change the ownership to whoever is running the copy command. Why would you run it as root? Anyway, please edit and include some example files so we can understand what you need. – terdon Sep 23 '14 at 9:00
6

If I understand you correctly, you want to change the owner of all files inside some directory (or the root) that are owned by user #500 to be owned by another user, without modifying files owned by any other user. You're in that situation because you've copied a whole directory tree from another machine, where files inside that tree were owned by many different users, but you're only interested in updating those that were owned by "your" user at the moment, and not any of the files that are owned by user #501 or any other.

GNU chown supports an option --from=500 that you can use in combination with the -R recursive option to do this:

chown -R --from=500 yourusername /path/here

This will be the fastest option if you have GNU chown, which on CentOS you should.

Alternatively can use find on any system:

find /path/here -user 500 -exec chown yourusername '{}' '+'

find will look at every file and directory recursively inside /path/here, matching all of those owned by user #500. With all of those files, it will execute chown yourusername file1 file2... as many times as required. After the command finishes, all files that were owned by user #500 will be owned by yourusername. You'll need to run that command as root to be able to change the file owners.

You can check for any stragglers by running the same find command without a command to run:

find /path/here -user 500

It should list no files at this point.


An important caveat: if any of the files owned by user #500 are symlinks, chown will by default change the owner of the file the symlink points at, not the link itself. If you don't trust the files you're examining, this is a security hole. Use chown -h in that case.

  • Thanks a lot.. :) It was that simple, and I was beginning to lose hope.. – waiting_for_peace Sep 23 '14 at 9:28
  • 1
    Another caveat is that chown removes the setuid and setgid bits in the permissions. – Stéphane Chazelas Nov 3 '14 at 21:30
3

One problem with the chown command (but that's actually down to the chown() system call) is that it strips the setuid and setgid bits in the permissions as a security measure.

It also doesn't cover ACLs.

To work around that, an approach can be to use rsync on a directory over itself in update mode and with the --usermap option:

$ ls -ln
total 6732
-rwsr-xr-x  1 1002 1000 6888896 Nov  3 21:50 a
drwxr-xr-x+ 2 1002 1000    4096 Nov  3 21:46 x

$ getfacl -n x
# file: x
# owner: 1002
# group: 1000
user::rwx
group::r-x
other::r-x
default:user::rwx
default:user:1002:rwx
default:group::r-x
default:mask::rwx
default:other::r-x

$ sudo rsync -rAau --usermap=1002:1000 . .

$ ls -ln
total 6732
-rwsr-xr-x  1 1000 1000 6888896 Nov  3 21:50 a
drwxr-xr-x+ 2 1000 1000    4096 Nov  3 21:46 x

$ getfacl -n x
# file: x
# owner: 1000
# group: 1000
user::rwx
group::r-x
other::r-x
default:user::rwx
default:user:1000:rwx
default:group::r-x
default:mask::rwx
default:other::r-x

1002 changed to 1000 in both the files owners and ACL entries.

It will also process symlinks correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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