20

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?

6 Answers 6

12

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

1
  • In case you only allow public key authentication to your server, you would need to import the secret key ? May 8, 2016 at 15:43
10

You can do:

exec su -l $USER

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

1
  • just a side note: if you don't enter the password the shell will exit
    – mekb
    Mar 5, 2022 at 23:40
7

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
    +1 works without requiring password/authentication unlike su May 8, 2016 at 15:44
2

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

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.

-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
1
  • 3
    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, 2017 at 17:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .