Operating System - Linux
1827963 Members
2766 Online
109973 Solutions
New Discussion

Need a script to set user dirs back to correct owner

 
SOLVED
Go to solution
TheJuiceman
Super Advisor

Need a script to set user dirs back to correct owner

Hey everyone,

Somewhere along the way our perms got mixed up in /home. I would like a script that would let me quickly change all the users in /home so that they own their own dirs. Any ideas? Thanks.
11 REPLIES 11
Steven Schweda
Honored Contributor

Re: Need a script to set user dirs back to correct owner

What does it mean to "change all the users"?
(Or to change a user?)

Do you want to change the ownership of some
directories?

Do you want to change the ownership of all the files in a directory tree, or just of the
directory?

How do you know who owns a directory?

Stating a problem clearly can lead more
quickly to a solution.
Yogeeraj_1
Honored Contributor

Re: Need a script to set user dirs back to correct owner

hi,

unfortunately, you cannot "undo" this change. Unless you recover from backup the files that have not changed since the error had occurred, you will have to set the owner and group manually.


good luck

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Steven Schweda
Honored Contributor

Re: Need a script to set user dirs back to correct owner

> [...] you cannot "undo" this change.

With enough information and effort, you can
undo _any_ change.

"chown" will change the ownership of one or
more files, but you need to know which owner
to set for which files.

When we get enough information to know what
the actual work to be done is, it should be
possible to say how much effort is actually
needed to do it. (And how much critical
information is missing.)
Dennis Handly
Acclaimed Contributor
Solution

Re: Need a script to set user dirs back to correct owner

>would like a script that would let me quickly change all the users in /home so that they own their own dirs.

If you want to just do that for the directories and each home directory is the same as the user, you can do:

for dir in /home/*; do
dir_base=$(basename $dir)
chown $dir_base $dir
done

You don't mention if the group ownership was also messed up?

You could grep the passwd file to make sure each owner exists too. And find the proper group, if needed.
James R. Ferguson
Acclaimed Contributor

Re: Need a script to set user dirs back to correct owner

Hi:

Knowing *what* you did to your '/home' directory will certainly dictate the kind of attack necessary to fix it.

Did you inadvertantly 'chown -R' when your currently working directory was at '/home'?

Did you change both the ownership and the group membership or did you just change the group membership with 'chgrp'?

If you only changed the owner of directories and files within '/home', a solution like Dennis's will work. If you changed both the ownership and group membership of directories and files within '/home', then I'd parse '/etc/passwd'; extract the account name and 'gid' from each record of the file; and 'chown -R name:gid /home/name' accordingly. A pure shell script can accomodate this easily.

*IF* you have changed both the owner and group membership of directories and files within '/home' *and* you have a significant number of files within that have different group memberships even within an account; *and* can't restore a backup; then consider restoring a backup copy of your '/home' directory to a temporary directory. Then, programatically examine the temporary directory and issue commands to change the *current* files ownership and group membership to match that of the temporarily restored set.

Regards!

...JRF...
Geoff Wild
Honored Contributor

Re: Need a script to set user dirs back to correct owner

for user in `cat /etc/passwd |awk -F: '{print $1}'`
do
chown $user /home/$user
done


Or add the recursise to the chown:

chown -R $user /home/$user

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Doug O'Leary
Honored Contributor

Re: Need a script to set user dirs back to correct owner

Hey

awk -F: '$6 ~ /home/ {printf("%s:%d %s", $1, $5, $6)}' /etc/passwd | \
while read owner home
do
echo chown -R ${owner} ${home}
done

If the script prints out what you think it should, remove the echo.

hth;

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Peter Nikitka
Honored Contributor

Re: Need a script to set user dirs back to correct owner

Hi,

Warning - using Dough solution as-it-is will lead to system corruption!
<<
awk -F: '$6 ~ /home/ {printf("%s:%d %s", $1, $5, $6)}' /etc/passwd | \
while read owner home
do
echo chown -R ${owner} ${home}
done
>>

Since /etc/passwd contains system accounts as well, one command would be something like
chown -R sys:sys /

You won't execute that.

However the idea is correct - you just have to exclude some accounts, e.g.

awk -F: '$6 ~ /home/ {printf("%s:%d %s", $1, $5, $6)}' /etc/passwd | \
while read owner home
do
[ home = / ] && continue
echo chown -R ${owner} ${home}
done

A check for uid >100 (or so) is not bad, as well.

Some other hints:
- When using NIS, instead of reading /etc/passwd you can use 'ypcat passwd'.
- Permissions for NFS-mounted homes can only be changed, when root access to the NFS server is enabled.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Doug O'Leary
Honored Contributor

Re: Need a script to set user dirs back to correct owner

>>Warning - using Dough solution as-it-is will lead to system corruption!

You might consider rereading the code before coming out with statements like that.

awk -F: '$6 ~ /home/ {...}

Says if the 6th field, the home directory in this case, has home in it, then print out the format...

The whole point of echo'ing the output for the first run is also to ensure it's only geting what it's supposed to - just in case some app home directory is /opt/home_app.

Believe it or not, I have done this a time or two...

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Peter Nikitka
Honored Contributor

Re: Need a script to set user dirs back to correct owner

Sorry Dough,

missed that!

I was remembered by this to a holiday call to myself as 'request for rescue' by such an incident (which ended in a re-install).

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
TheJuiceman
Super Advisor

Re: Need a script to set user dirs back to correct owner

Thanks everyone.