Upon login with .bashrc, how to set the user's group to a non-default one, say targetgroup?

Specifically, my problem is that I can execute, on the command line:

newgrp - targetgroup

but when I include this line in .bashrc, the terminal freezes upon login.

This question relates to Problem while running "newgrp" command in script but I have insufficient reputation to comment. So I tried:

echo "Before newgrp"
/usr/bin/newgrp - targetgroup <<EONG
echo "hello from within newgrp"
echo "After newgrp"

which gives:

Before newgrp
Before newgrp
Before newgrp
Before newgrp
Before newgrp
Before newgrp
Before newgrp
Before newgrp
After newgrp

so the trick for KornShell does not appear to work for bash, as I had to exit with ^C.

Is there any way to make newgrp work or another .bashrc line that would set the group to targetgroup upon every login? (NB I don't have superuser priviledges.)


For CentOS 6 you can try adding (without dash)

newgrp targetgroup

to your .bash_profile.

At least for CentOS 6.10 this changes the effective group to targetgroup in the interactive shell

  • for new login shells
  • after source ~/.bash_profile

Group will not be changed when "only" starting another bash or source ~/.bashrc in an existing console.

| improve this answer | |
  • It does something different, but still does not work: Before newgrp hello from within newgrp uid=535(myuser) gid=528(targetgroup) groupes=535(myuser),528(targetgroup) contexte=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 After newgrp [myuser@machine ~]$ vim test7 [myuser@machine ~]$ ls -lrt total 52944 [...] -rw-rw-r--. 1 myuser mygroup 0 22 mar 17:21 test7 – Gene Arboit Mar 22 '19 at 21:23
  • Hi Gene, I do not fully get what you want to achive with the second part "After newgrp". When I "uid=1000(user) gid=100(users)" add "newgrp cdrom" to my .bashrc and execute "bash", then use "id" within this bash it shows "uid=1000(user) gid=24(cdrom) groups=24(cdrom),20(dialout),... So the effective group has changed and e.g. "touch xyz; ls -al xyz" results in "-rw-r--r-- 1 user cdrom 0 Mar 22 22:33 xyz" – Marvin Mar 22 '19 at 21:30
  • I was indeed going for something like your "touch xyz; ls -al xyz". However, with your test, I still get the same default mygroup: "-rw-rw-r--. 1 myuser mygroup 0 22 mar 17:51 xyz". Maybe the machine I have access to is missing some updates... it just seems that newgrp should be a basic command :\ – Gene Arboit Mar 22 '19 at 21:56
  • Hi Gene, most likely we are missing something obvious :) What linux distribution and version are you using? And how to you reload your .bashrc, after you have added the "newgrp" line? (Typing "bash" on the commandline and continue from there? Using "source .bashrc" ... ?) – Marvin Mar 23 '19 at 9:23
  • Hi Marvin... With the command "lsb_release -a" it returns: "LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch Distributor ID: CentOS Description: CentOS release 6.5 (Final) Release: 6.5 Codename: Final" ... Yeah, I've been using "source .bashrc" to test it... and starting other ssh sessions sometimes to double-check as well. It'd be awesome if you could think of something that'd fix this! ^__^ – Gene Arboit Mar 23 '19 at 21:04

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.