2011-04-06

Automatically switch main group when logging in to a host

I have accounts on couple of hosts where I don't have root powers, and on some of them I am working alongside others. We're in the same group, but our primary groups are named after ourselves. Sometimes we need to edit files the other person has made and it can be a pain if one of us forgets to set the permissions such that the other user can edit the file.

Instead of having to remember to run chgrp semsorgrid newfile and then chmod g+w newfile every time we make a new file, we can change our primary group after logging in with newgrp semsorgrid and set the file creation mask to give the group maximum privileges with umask 002. But we still have to remember to do that when logging in.

I tried putting those commands in our shell configuration files (my .zshrc and his .bashrc) but then when logging in new shells are spawned recursively. The solution is to check which group we're in and conditionally run newgrp like this:

umask 007
if [ $(groups | awk '{print $1}') != "semsorgrid" ]; then
    exec newgrp semsorgrid
fi

This replaces the shell which was originally spawned with a new one with the primary group properly set, then when that shell initializes it skips the newgrp command since the primary group is already "semsorgrid".

No comments:

Post a comment