Applying ownership patterns from another server
Let's say that there are two dedicated servers, freshly installed, with identical software and users on both of them.
Suppose that, for whatever reason, I would like to copy ownership patterns from one of these servers to another. Is that possible?
Solution 1:
The first paragraph makes hardly any sense, but your actual question:
Can I recover and set up file permissions and ownerships using another server as a reference/template?
The short answer is: maybe...
The longer answer is that such a strategy will fail for every file that does not exist on both servers. And as far as scripting goes, you'll need to take particular good care with special files, (symbolic) links, special characters in file/directory names etc. etc.
But both the chmod
and chown
commands support a --reference
flag. You can point to an existing file and chmod
will use the permissions of that file instead of you needing to supply MODE values when changing the file mode.
Similarly chown
will use owner and group of that reference file/directory rather than specifying OWNER:GROUP values.
The exercise is then:
- Make the source, the reference directory trees you need available as a template on the destination server. (If you have sufficient empty space simply copy the source directory while preserving ownerships and mode settings, or alternatively use for instance NFS to export the source directory tree and mount that as the template.)
- Then run these crude
find
commands that executechown
andchmod
:
(Please note that these are only conceptual and untested. Please add restrictions for find
to only locate files and directories for instance and omit following symbolic links and such... )
To reset ownerships and modes on a corrupted /etc/
:
cd /template/etc
find . -exec chmod -v --reference='{}' /etc/'{}' \;
find . -exec chown -v --reference='{}' /etc/'{}' \;