When you log out and log back in, Linux refreshes your user session and access tokens.
Here is exactly what happens under the hood regarding groups:
1. Group Membership Update
When you log in, the system reads your information from /etc/group and /etc/passwd.
- If you were added to a group (e.g.,
developers) while you were already logged in, the terminal session you were using wouldn't know about it yet. - Logging back in creates a new process tree for your user that carries the updated list of Group IDs (GIDs).
2. Permissions Take Effect
Until you re-login, you might get "Permission Denied" errors when trying to access files owned by your new group. After logging back in, the kernel recognizes your new group membership, and those "Permission Denied" errors will disappear.
3. Environment Variables & Shell
A fresh login also reloads your shell configuration files (like .bashrc or .zshrc). If any logic in those files depends on your group membership, those changes will now be active.
Is there a way to avoid logging out?
If you are in the middle of a project and don't want to log out, you can use the newgrp command to open a new sub-shell with the updated group membership active:
newgrp developers
Try this experiment:
- Add yourself to a new group:
sudo groupadd testgroup && sudo usermod -aG testgroup labex - Run
groups. You likely won't seetestgroupyet. - Run
newgrp testgroup. - Run
groupsagain. You will see it now!
Does that explain why the re-login step is so important in Linux administration?