Restoring original Solaris file permissions

Have you ever been in a situation where you have recursively changed the file permissions in a directory then realised you were in the wrong location?

For example (chown -R myuser /spacesomedir), realising there is a space between the / and somedir .... This action is catastrofic as we have just changed the ownership of all files under / (root) downwards.

Don't panic ... All is not lost ... We don't need to reinstall the system as Solaris maintains a database of all packages installed on the system.

In a nutshell, for every file and directory installed by any package on a Solaris system an entry is written into the Solaris Software repository (aka /var/sadm/install/contents)

Each entry contains all the information on a file, including it's path, owner, permissions. Using this information can be used to restore permissions and ownership which have accidentally changed

Here is a short step-by-step on how to restore the file permissions:

Note: The pkgchk utility does not restore setuid, setgid or sticky bits, you must set these manually (where appropriate)

  • Boot the system into single-user mode from alternative media (net, cdrom, etc.):
    ok boot net -s
  • Mount the affected file systems under the /a mountpoint (for example):
    # mount / /a
    # mount /usr /a/usr
    # mount /var /a/var
    # mount /opt /a/opt
  • Run the pkgchk command with the -R option to specify an alternative root path, and the -f flag to say that file permissions need to be fixed:
    # pkgchk -R /a -f