Cloning File Permissions and Ownership Between Machines

by Reed on September 17, 2010

Alternate Title: Help, I chown-ed my server!
Ever recursively screwed file system permissions?

Usually done with something like

chown -R user:user $basepath/$dir

or

chmod 600 -R $basepath/$dir

in a script? Forget to set $basepath and $dir just once, and you find yourself in all kinds of mess; with a server with many of the system file ownerships changed to user, or permissions broken.

If you’ve got another server with a similar OS release/filestructure, here’s a quick-and-dirty way to clone the file ownership and permissions settings from it to your busted server, via SSH:

find / -print0 |
xargs -0 stat -c "chmod %a '%n'; chown -h %u:%g '%n';" |
ssh bustedserver

This saves you having to restore the whole server from scratch and is much faster. You can also run the first part of the sequence and redirect it into a file then ship the file over to the broken server and run it. Remember, it’s mainly the contents of /usr and the root file system that will need to be corrected for the system to run; the contents of the /home directory will usually be owned by their respective users and can be recovered more easily.

The above won’t solve everything for you; but it will help a lot. You may want to use a more restricted find command so you don’t copy extraneous permissions: find / /usr /var -print0

Once you’ve run the above command, run “find / -user user” and manually fix directories one by one, re-running the “find / -user user” until you have verified correct ownerships. With some fast typing you should have your server up in 30 minutes or less!

Previous post:

Next post: