A Linux groups gotcha

You have to log out and log back in after adding yourself to a group

Suppose you add yourself to a group with:

sudo usermod -aG examplegroup $USER

You might expect that you can immediately view all files with the new permissions afforded to you by your membership in examplegroup. Alas, no. Your group memberships are cached in your session. To see what groups you are effectively a part of (for the current session) with:

id

To see what groups you're in globally (as stored in /etc/group), use:

groups $USER

You can "remove" yourself from a group with:

sudo gpasswd --delete $USER examplegroup

but you'll still have permissions associated with examplegroup (due to the session cache).

The easiest way to update your effective groups is to log out and log back in. If you want to make sure there aren't any processes around with the stale groups info, you can kill them all:

sudo pkill -u $USER

If you just added yourself to a new group, you can update your membership in that group with:

newgrp examplegroup

However, it has side-effects. It starts a new shell, and changes the current real group ID to the specified group, meaning any files you create will be associated with that group. So I think it's better to log out and log back in.


If you enjoyed this post, please let me know on Twitter or Bluesky.

Posted December 27, 2024.

Tags: #linux