- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Need a script to set user dirs back to correct own...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2006 03:45 PM
12-15-2006 03:45 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2006 05:01 PM
12-15-2006 05:01 PM
Re: Need a script to set user dirs back to correct owner
(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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2006 05:45 PM
12-15-2006 05:45 PM
Re: Need a script to set user dirs back to correct owner
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2006 06:07 PM
12-15-2006 06:07 PM
Re: Need a script to set user dirs back to correct owner
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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2006 11:46 PM
12-15-2006 11:46 PM
SolutionIf 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.
- Tags:
- chown
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2006 02:59 AM
12-16-2006 02:59 AM
Re: Need a script to set user dirs back to correct owner
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2006 04:31 AM
12-16-2006 04:31 AM
Re: Need a script to set user dirs back to correct owner
do
chown $user /home/$user
done
Or add the recursise to the chown:
chown -R $user /home/$user
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2006 04:54 AM
12-16-2006 04:54 AM
Re: Need a script to set user dirs back to correct owner
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2006 05:14 AM
12-16-2006 05:14 AM
Re: Need a script to set user dirs back to correct owner
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2006 05:40 AM
12-16-2006 05:40 AM
Re: Need a script to set user dirs back to correct owner
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2006 05:48 AM
12-16-2006 05:48 AM
Re: Need a script to set user dirs back to correct owner
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2006 09:49 AM
12-18-2006 09:49 AM