Linux Command Reference ยท Permissions and Users
Linux newgrp command
Reference for the Linux newgrp command: change your primary group in a new shell, pick up new group membership without logging out, and compare it with sg.
newgrp starts a new shell in which your primary group (the group given to files you create) is changed to another group you belong to. It is also the quick way to pick up a group membership that was added after you logged in: usermod -aG only takes effect at the next login, but newgrp re-reads the group database for the new shell.
The new shell replaces your current one, so leave it with exit to return to the old group. If you are not a member of the group, newgrp asks for the group password (rarely set) and otherwise fails.
newgrp [GROUP]
Try it
The sandbox account belongs to one group only and has no user name, so the first example is read-only; the second shows how the group of new files comes from the primary group.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
newgrp [-] [GROUP]
sg GROUP [-c COMMAND]
| Argument | Description |
|---|---|
GROUP |
The group to become the primary group (a name or a GID). It must be one you belong to. |
- |
Make the new shell a login shell (reset the environment). |
sg GROUP -c CMD |
The non-interactive sibling: run CMD with that group, then return. |
Without GROUP, newgrp switches back to the primary group listed in /etc/passwd for your account.
Returns: the exit status of the shell you leave. If the group does not exist or you are not allowed, it prints a message and exits with 1.
Usage notes
- A new process, not a change in place. Variables you set in the old shell are lost unless exported; background jobs keep running with the old group.
- Supplementary groups stay. Only the primary group changes; you keep access through every group you already belong to. See
idfor the difference. - Why it matters: with a primary group
staff, a new file isalice:staffand mode 0644 (depending onumask). Afternewgrp projectit isalice:project. Team directories avoid this with the setgid bit. - Picking up new membership: after an administrator ran
usermod -aG project alice, runnewgrp projector log in again.groupsin an existing shell still shows the old list. - Group passwords (
gpasswd project) are almost never used; prefer real membership. - Scripts should use
sg, or run withsudo -g project, becausenewgrpstarts an interactive shell and does not return to the script's next line until it exits. - Shell differences: some shells implement
newgrpas a builtin (exec newgrp) so that the new shell replaces the old one cleanly.
Related
id,groups,usermod,groupadd,chgrp- Lessons: Users and permissions, Permissions