PDA

View Full Version : [Solved] Accidentally 'chown -R' to /usr/local in jail as root


tekkon
March 6th, 2009, 20:43
I am running Freebsd 7.1 release. I have an 'ezjail' created jail and I was logged in as root with 'jexec'.

I wanted to "chown -R www:www /usr/local/www" because of some permission issue.
Instead I accidentally "chown -R www:www /usr/local"

Now the all the file and directories in '/usr/local' is own by user "www"

I think I might have done some serious damage to the jail. I haven't executed any further commands after that. Is there any way to revert the last action made in Freebsd? Or could I restore the original permissions for '/usr/local' with 'ezjail-admin'?

crsd
March 6th, 2009, 22:53
It should be pretty safe to `chown -R root:wheel /usr/local` with some exceptions, running `find /usr/local/ -not -user root -or -not -group wheel` gives me etc/sasldb2.db owned by cyrus:mail and all directories in /usr/local/man owned by man:wheel (recursively, not files). YMMV, however.

anomie
March 6th, 2009, 22:59
I'd add that you need to get into the habit of backing up your jail regularly, and especially just before performing maintenance or upgrades. The cpdup (http://www.freshports.org/sysutils/cpdup/) program (in ports) is great for this purpose.

Mel_Flynn
March 7th, 2009, 05:30
for mfile in /var/db/pkg/*/+MTREE_DIRS; do
mtree -ude -f ${mfile} -p /usr/local
done

That takes care of all directories created by installed ports. Don't worry about the messages.

Then again, you could also reinstall all ports.

tekkon
March 7th, 2009, 14:49
for mfile in /var/db/pkg/*/+MTREE_DIRS; do
mtree -ude -f ${mfile} -p /usr/local
done

That takes care of all directories created by installed ports. Don't worry about the messages.

Then again, you could also reinstall all ports.

Thank you for the solution. For the code you provided, is it a shell script? I've yet to attempt shell scripting however would like to try. To be sure, do I add a shebang to the code above, then save to a .sh file and run it?

I am interested to know how the code works too.

Mel_Flynn
March 7th, 2009, 16:57
You can simply type it on the command line. The shell will execute after 'done'.

As for how it works:
When a port is installed it saves modes and ownerships of directories it uses in /var/db/pkg/<pkgname>/+MTREE_DIRS. This code walks all MTREE_DIRS files and restores those modes and ownerships if they have been changed.
See mtree(8) (http://www.freebsd.org/cgi/man.cgi?query=mtree&apropos=0&sektion=0&manpath=FreeBSD+7.1-RELEASE&format=html) for details.

tekkon
March 9th, 2009, 23:41
Thank you for the description. I have tried entering the commands you've provided. From the output, changes was made only to '/usr/local/man'.

I then reinstalled all ports which seems to have fixed the ownerships issues.