7

If I add myself to a group, it applies only to newly launched processes. Actually it's more restricted than that; it seems to apply only to new logins. I'm not sure what the exact mechanism is, but that's not my question (though I'm curious about that too).

My question is this: Is it possible to update my group membership in the current shell process?

I suspect the answer is no.

An example (this is not a direct copy-and-paste):

$ groups ; groups kst
kst oldgroup1 oldgroup2
kst : kst oldgroup1 oldgroup2
$ sudo adduser kst newgroup
[sudo] password for kst:
Adding user `kst' to group `newgroup' ...
Adding user kst to group newgroup
Done.
$ groups ; groups kst
kst oldgroup1 oldgroup2
kst : kst oldgroup1 oldgroup2 newgroup
$

After the adduser, my account is a member of newgroup, but that membership is not reflected in the current shell process.

The obvious solution is to log out and log in again, but that's inconvenient.

1 Answers1

6

It is not possible, from outside the process itself. See this SuperUser question for more discussion and an answer involving using a debugger to attach to the process and run setgid() from within it.

The reason it takes a new login to accomplish getting your new group memberships is that login (or sshd or whatever) runs as root, and must set process UID and GID for your shell process, which means it is already calling setuid and setgid.

bonsaiviking
  • 2,697
  • 16
  • 15