How to set up newgrp on .profile?

Hello all,

I am having an issue setting up an environment on .profile. I want to set up my .profile in that way that everytime I login to the host, it should automatically set up my group. I tried the folllowing but it doesn't work. It is on the solaris OS.

HOSTNAME=$(hostname)

if [[ "$HOSTNAME" = "tpat1a" ]] || [[ "$HOSTNAME" = "tpat2a" ]]
then
newgrp unixteam
umask 002
fi

Any help will be appreciated.

Thanks
PT

In what way does it not work? Have you tried putting an echo statement in there to see if it's at least trying to run that code?

What is your login shell?

I am working on ksh. When I inserted that script on my .profile and executed it (. ./.profile), it didn't change my group. I am wondering if there is anything wrong on my script or Does this should be different on unix and linux host? Do I need to modify anything to make it work?

Just checking, the group exists right? Can you check the value of hostname? Try running the command in the command line and see if it changes.

--ahamed

Yes, the group exists. I can use this "newgrp <groupname>" from command line. I have to type it all the time everytime I log in. I want to keep it in my .profile so that I don't have to type it again and again. Most of the time I forgot and it messess up everything.

Try executing the .profile with -x option and see if there are any errors. bash -x .profile Also, check if the hostname is correct.

--ahamed

Put an 'echo' statement in your .profile so you can tell if it's actually being run.

The hostname is correct. While executing the bash -x .profile there is one error found on:
integer KSHLEVEL=0
.profile: line 167: integer: command not found.

So, when you execute this .profile manually, is the group getting changed? Can you paste the relevant parts of this if loop getting executed?

--ahamed

No, it didn't change the group. Is there anything else I can do?

Not really, because newgrp starts a new shell.
The only thing you can try is exec it (as the last command) in .profile

umask 002
exec newgrp unixteam

This should replace the current (login-)shell by a new one.
But if it fails it will disable system login.

1 Like

I haven't tried the following command. I am scared if it disable the system login. Any suggestions whether to use it or not.
umask 002exec newgrp unixteam

Why not change their login group instead of putting newgrp in their profile?

how can I change the login group to unixteam? Now, Its login group is same as username.

Unlike bash, ksh implements newgrp as a built-in command that is equivalent to exec /usr/bin/newgrp and thus never returns.

To avoid the implicit exec in ksh, invoke the non-builtin version of newgrp, i.e. /usr/bin/newgrp.

1 Like