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?

Avdi
  • 353
  • 1
  • 3
  • 7

6 Answers6

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

sumar
  • 2,106
  • 12
  • 12
9

You can do:

exec su -l $USER

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

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.

Joseph Kern
  • 9,899
  • 4
  • 32
  • 56
2

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

Teddy
  • 5,204
  • 1
  • 23
  • 27
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.

mdpc
  • 11,856
  • 28
  • 53
  • 67
-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
Tombart
  • 2,143
  • 3
  • 27
  • 48
  • 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 '17 at 17:26