Is it possible to execute some bashrc file for all users in a group? If you can do that system-wide in /etc/profile then why not for a usergroup?


In /etc/profile add:

if [ `id -ng` = "the_cool_group" ] ; then
    # do stuff for people in the_cool_group
| improve this answer | |

You could add something like this to /etc/bash.bashrc:

# running group-based bashrcs

for group in $(id -Gn); do
  if [ -f "$group_bashrc" ] && [ -r "$group_bashrc" ]; then
    command . "$group_bashrc"

Then you can create a /etc/bashrc-by-group/mygroup and put the initialisations for the members of the mygroup group in there.

That assumes your groups are well tamed: that the group names don't contain blank, slash or wildcard characters, that they're not . or .., and that you have a one-to-one mapping between group name and group id. If not, you can use id -G instead of id -Gn and use /etc/bashrc-by-group/groupid.

| improve this answer | |

/etc/profile is seen by all users. ~/.profile is seen by the one user logging in: it's a file in the user's home directory, and each user has their own directory; the login program sets the environment variable HOME to the path of the user's home directory. The same goes for other files such as /etc/bash.bashrc and ~/.bashrc — many applications have a system-wide configuration file under /etc and a per-user configuration file in the user's home directory.

There is no similar mechanism for groups — groups don't have a home directory. Thus there is no straightforward way to apply a configuration file to all users in a group.

As other answers have shown, you can add commands in a system-wide shell initialization file inside a conditional statement to run them only when the user belongs to a particular group. Note that a user can belong to more than one group. It's up to you to decide whether you want to run commands only when the group is the user's primary group or whenever the user belongs to the group.

case "$(id -gn)" in
  foo) somecommand;; # the user's primary group is foo
case ":$(id -Gn | tr \\n :)" in
  *:foo:*) somecommand;; # the user belongs to the group foo

On the difference between .profile and .bashrc, see Is there a ".bashrc" equivalent file read by all shells?

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.