Automatically changing the user's group based on working directory
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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?
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.