Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using bash shell. I want to run all commands of a specific user to be run as a specific group command in the shell. How can I do it? Can anything be can be done in ~/.bashrc ?

share|improve this question
Please clarify: if you want him to run "all commands" under "a specific group", is there a reason why you don't change /etc/passwd to make that group his default? Alternatively, what about putting a newgrp command in his ~/.bashrc? Or, is there a specific feature of sg that you need? – John1024 Dec 4 '13 at 6:13
what should i change in /etc/passwd ?? i dont think newgrp doesnt do anything rather than creating a group :) – Charan Pai Dec 4 '13 at 6:45
You are thinking of groupadd. I wrote up more on how to safely change group identity in /etc/passwd below. – John1024 Dec 4 '13 at 8:18
up vote 1 down vote accepted

If a user wants to spend the rest of his session in somegroup, he can run newgrp, as in:

newgrp somegroup

He may or may not be prompted for a password depending on permissions. See man newgrp for details.

The group that the user is in when he starts his session is determined by the fourth field of his line of the /etc/passwd file. If you don't want to futz with that file directly, there are system utilities to modify it safely. One is usermod:

usermod -g somegroup someuser

(somegroup should exist before you run this.) After running the above, the next time that someuser logs in, he will, by default, be in somegroup.

For information on how to interpret the fields in the /etc/passwd file, see man 5 passwd (the "5" is important). Groups and their corresponding group numbers are defined in the /etc/group file. See man 5 group for more info on it.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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