11

From my understanding, a Linux session caches its group memberships at login. Then if a new group membership is added (e.g. with adduser someuser somegroup), the user must log out and log in again to be able to take advantage of the new membership.

My question is: is there any way to refresh group memberships in-process, without logging in again, exiting the current process, or starting a new process?

8

You can start new login from your session

$ su - your_login

It doesn't affect your running process. If you are using Xserver, you can start xterm and type this command (affect only this terminal session) The only way to make it persistient is to finish your session and start it again

  • In case you only allow public key authentication to your server, you would need to import the secret key ? – Cyril Duchon-Doris May 8 '16 at 15:43
4

I don't think so. But you can use the newgrp command to spawn a new shell with the new group. This is not permanent.

newgrp superawesomegroupname

You'll need to log your users off-then-on-again.

  • 1
    +1 works without requiring password/authentication unlike su – Cyril Duchon-Doris May 8 '16 at 15:44
2

Others have mentioned "su - $USER" and "newgrp", and I thought I should mention that you should also look at "sg".

1

You can use gpasswd for immediate change:

gpasswd -a someuser somegroup

There's no need for creating new shell or relogin. Afterwards verify the group membership using getent:

getent group somegroup
  • You are creating the group, then you are looking up that group. This has nothing to do with the discussed question - the change is not effective in your current session. – Str. Aug 18 '17 at 17:26
1

You can do:

exec su -l $USER

That implicitly refreshes the groups list without having to exit the terminal.

0

What about this (although it only meets the requirement that you would not have to relogin)

exec /bin/bash -l

The -l flag would do assume a new login shell and the exec would replace the current shell with that new one. BTW, don't mistype the name of the shell :-)

One problem with using newgrp and su as suggested previously is that a new sub-shell would be created. Yes you could preceed them with an exec command as well.

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.