0

Related to this question, I recently detached a disk from a Linux cloud machine (Ubuntu 18.04). When I reattached it to a new VM (also Ubuntu 18.04) I discovered that the user and group information had been lost, and most of the files were now owned by '1003:1003'.

My goal is to give my username (which is the same on both machines) access to the files but I would like to avoid using chown to change the ownership on thousands of files, some of which have other owners and perhaps should not be changed. So I have two questions:

  1. Is there a way to easily reclaim the old group information that associates 1003 with my username and group?
  2. Let's say I do have to use chown: how do I change the ownership of only the files that are owned by '1003'?

Thanks

John
  • 181
  • /etc/group and /etc/passwd are editable by root and fairly self-explanatory. You will probably be able to recover your names there using the 1003 id. – Stephen Boston Jul 17 '20 at 03:50
  • Thanks Stephen. So if I made my username a member of the '1003' group, wouldn't that give me access to everything? I realized after posting the question that maybe that was all I had to do, and it seems a lot safer than recursively changing file ownership. – John Jul 21 '20 at 05:03
  • Changing file ownership and permissions can cause trouble so ... right. If group permissions will give you all the access you need then joining the group will do what you want, but joining the group will not give you ownership. So though it's likely you'll be able to read all the files you may not be able to modify them using your own user id. – Stephen Boston Jul 21 '20 at 11:04

1 Answers1

0

I finally stopped procrastinating and tried it, only to discover that 1003 did not appear anywhere in /etc/passwd or /etc/groups; somehow that user and group were lost during the disk transfer.

I solved the problem in what I think is a relatively safe way by changing only those files owned by 1003:1003 to a new owner:

This worked in Ubuntu 18.04:

chown -R --from=<old_owner>:<old_group> <new_owner>:<new_group> <directory>

...but I think some Linux distributions don't allow the --from flag, so this should work in those cases:

find . -user <old_user> -group <old_group> -exec chown <new_user>:<new_group> {} \;

Thanks to @Stephen Boston for advice.

John
  • 181