• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

mass chown home directories?

Red Squirrel

No Lifer
Is there a way I can mass chown all home directories so that they are owned by the user they are for? I also want to chmod them all 700, but I really dont want to have to do this for every single one. Is there some kind of bash script I can download or something? Also is there a way to make it so if a file gets created there, its automaticly owned by that user?
 
Originally posted by: RedSquirrel
Is there a way I can mass chown all home directories so that they are owned by the user they are for? I also want to chmod them all 700, but I really dont want to have to do this for every single one. Is there some kind of bash script I can download or something?
Is there something wrong with chmod 700 /home/* ?
Also is there a way to make it so if a file gets created there, its automaticly owned by that user?
I think that making the directory setuid (or setgid) will do that. Try chmod 4xxx for setuid (or 2xxx for setgid).

 
Actually chmod 700 /home/* will work, but chown will have to be done for each one individually, I'm just trying to find an easier way. Right now they're all owned by root since I used root to restore the backup after I did my clean install. Guess this is why /home is usually on a different partition...
 
Turns out it has to be 755 for mail to work... which is weird. I ended up just doing them all manually, but its just from a long term point of view, if ever I manage a linux server with 100+ users, I dont want to start having to do this when I migrate a server to a newer distro, or what not. Or is this why linux is being replaced by windows in the enterprise market?
 
You could write a fairly simple bash script to do it. Basically you would need to find out what user owns which directory by gathering the information from the /etc/passwd file, then have the script chown that directory to that user/group.

Also, take a look at the man pages for tar. There are switches that allow you to compress files for backup purposes that ALLOW you to keep the permissions on the files, so that when you extract them they do not change ownership to root.
 
Originally posted by: jfall
Also, take a look at the man pages for tar. There are switches that allow you to compress files for backup purposes that ALLOW you to keep the permissions on the files, so that when you extract them they do not change ownership to root.
Yes, that would be the right way to do it. Specifically, you want to use the -p (="preserve permissions") option.

Originally posted by: RedSquirrel
Actually chmod 700 /home/* will work, but chown will have to be done for each one individually
Sorry, I was reading chmod for chown for some reason.

Originally posted by: jfall
You could write a fairly simple bash script to do it. Basically you would need to find out what user owns which directory by gathering the information from the /etc/passwd file, then have the script chown that directory to that user/group.
Assuming it's a typical linux system where the name of the homedir = username = groupname, you can do it more easily, I think. Like

cd /home
for dir in `ls`; do
chown $dir.$dir $dir
done

Originally posted by: RedSquirrel
Turns out it has to be 755 for mail to work... which is weird.
I doubt that's necessary, but I saw that you're using Dovecot and I don't know if that changes anything. I have sendmail/courier-imap servers with mode 700 home directories that work just fine.

Or is this why linux is being replaced by windows in the enterprise market?
I suggest you learn more about the system before you pass judgment on it.





 
Yeah with 700 I could not delete mail, I dont know why. no errors or anything, but in thunderbird nothing happened if I hit delete. So I set it back to 755 and now it works. But this would not be good on a multiuser system since other users could read other people's mail/settings.
 
Right now they're all owned by root since I used root to restore the backup after I did my clean install. Guess this is why /home is usually on a different partition...

Why would root own them all? Every file copy, backup, etc command has support for saving permissions and ownership.

Turns out it has to be 755 for mail to work... which is weird.

Not really, the mailer daemon has to have access to their directories.

Also is there a way to make it so if a file gets created there, its automaticly owned by that user?

Not by that user but by the owning group of the directory. But you shouldn't be creating files in user's home directories as root a lot anyway, so it's not a big deal.

Or is this why linux is being replaced by windows in the enterprise market?

If it is then it's because those people are incapable of reading documenation, much like yourself.

Yeah with 700 I could not delete mail, I dont know why. no errors or anything, but in thunderbird nothing happened if I hit delete. So I set it back to 755 and now it works.

Then you need to figure out why that user didn't have permission to delete his mail, changing permissions to 755 wouldn't affect that anyway because you didn't enable any write bits that weren't already enabled.
 
This is why unix admins are supposed to know sacripting languages:

#!/usr/bin/perl

opendir HOME, '/home';
while(my $dir = readdir HOME){
if($dir ne '.' && $dir ne '..' && -d $dir){
`chown $dir $dir`;
}
}
closedir HOME;
 
Actually that's overkill notfred, you can do it in bash with:

for i in *
do chown -Rf "$i" "$i"
done

Just make sure you're run that inside of /home and you'll be fine, assuming you have no users that have a username starting with a period.
 
Back
Top