LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-10-2007, 11:53 PM   #1
sleeping
LQ Newbie
 
Registered: Dec 2004
Distribution: ArchLinux
Posts: 22

Rep: Reputation: 0
Question Automatically changing the user's group based on working directory


Hello,

I am setting up a family computer on Linux, with each member (mom, pop and the kids) having their own separate user account. All of those users would also be a member of a group "family", and I'd put an extra folder /home/family where the group "family" would have all the permissions, so they can share files with each other (pictures, music, etc.).

Is there a way to have a newly created file automatically get a certain group ownership and permission based on which directory it's created in? As an example, if user "sarah" creates a file in the /home/family directory, I would need that file to automatically be owned by group "family" and have group read-write permission. However, if the same user creates a file in her own home directory, the file would need to *not* be owned by "family", or at the very least, not be group readable/writable.

I don't think umask would provide what I need, since either the new files' permissions aren't constant (if every file is created as group "family", but different permissions based on directory), or the new files' group ownership need to change.

The "solution" I'm thiking about is adding a cron job of "chgrp -R family /home/family/*" and "chmod -R g+rwX /home/family/*", but that seems both overkill and wasteful resourcewise (plus, finding the right frequency for the job is hard).

Is there a nice Linux tool that I don't know about that already performs this magic, or does any of you have a better solution than the cron job?

Thanks.
 
Old 03-11-2007, 12:44 AM   #2
justsimran
Member
 
Registered: Mar 2007
Location: Aus
Distribution: RHEL-4,5,UBUNTU,SUSE,FEDORA-6,7
Posts: 109

Rep: Reputation: 15
Is there a way to have a newly created file automatically get a certain group ownership and permission based on which directory it's created in? As an example, if user "sarah" creates a file in the /home/family directory, I would need that file to automatically be owned by group "family" and have group read-write permission. However, if the same user creates a file in her own home directory, the file would need to *not* be owned by "family", or at the very least, not be group readable/writable.

Is "Sarah" is the member of group "family"

In /etc/group
does it shw like this

family:x:id:sarah ?

i am just asking you bec i want to knw if she is the member assigned to this group or not?
 
Old 03-11-2007, 01:15 AM   #3
sleeping
LQ Newbie
 
Registered: Dec 2004
Distribution: ArchLinux
Posts: 22

Original Poster
Rep: Reputation: 0
Ok, I might not have made this clear. Yes, user "sarah" is a member of the group "family".

Here's a more direct example. 4 users : john jane sarah and mark. Each of them is a member of the group having the same name as their user (group "john", group "jane", etc.). All 4 of them are also members of the group "family".

I would like any file created in a given shared directory (/home/family) by any of those 4 users to be owned by the group "family", with group readable-writable permissions. However, if user "sarah" creates a file in her own home directory (/home/sarah), the file would need to be owned by the group "sarah".

The point of it all is, Jane has pictures she wants to load in the computer for the whole family to enjoy. She would then copy them to the shared directory, and the other members of the group "family" would have access to the files. However, she also has some private files that she puts in her home directory and doesn't want anyone else to access them.

I know this is possible manually with chgrp, chmod and newgrp. However, I would like to have it all happen automatically to minimize the amount of work needed to share the files.
 
Old 03-12-2007, 07:03 AM   #4
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
I believe the solution is easy .

1st, all the users' primary groups are their own:
  • john - john
  • jane - jane
  • sarah - sarah
  • mark - mark
Therefore you don't have to worry about their private files (in ~/) having group permissions turned on -- none of them are members of each others' groups. Also, since the norm is for a file to take the primary group of its creator, there is no ownership problem w/ these files.

So, 2nd, the trick is to force the group of all files created in ~/family/ to be "family". This is easy -- set its group to "family" & set the GID bit on the directory itself:
Code:
chgrp family ~/family/
chmod  g+s   ~/family/
Code Warning
I did r-RTM for chmod,
I did not actually test the code,
I have done this before successfully.


If you were running a system that makes "users" the primary group for all users, then you would have a more complicated problem.
 
Old 03-12-2007, 11:12 AM   #5
sleeping
LQ Newbie
 
Registered: Dec 2004
Distribution: ArchLinux
Posts: 22

Original Poster
Rep: Reputation: 0
Thanks for the hint. I'm still unfamiliar with the fancy permission bits (suid and sticky) and what they mean in different contexts. I had read the man for chmod, but it mentions "set user or group ID on execution (s)", didn't know it also worked for file creation. I'm gonna give it a try.
 
Old 03-12-2007, 01:28 PM   #6
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
That's what it means on a directory, different from a file; just as the "x" bit for a directory means access, not execute.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Kalamazoo Linux User's Group zeroability Linux User Groups (LUG) 0 08-23-2006 03:43 PM
Changing user's home directory? HOW barton Debian 10 04-04-2005 02:27 AM
Changing a user's group at login depending on the time of day Steve2001 Linux - General 10 10-11-2004 03:29 AM
Changing user's group? orange400 Linux - General 2 06-18-2004 04:30 AM
Changing a user's default group dogn00dles Linux - Security 4 07-09-2003 03:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration