newgrp
Start a shell with a different active group
newgrp [-l] [GROUP]By CMD Script Team · 3 min read · Last updated
On this page
newgrp [-l] [GROUP][X]- Optional — the command works without it
X...- Repeatable — you can pass more than one
ALLCAPS- A placeholder — replace it with your own value
Options
| Flag | Description |
|---|---|
-l | Start the new shell as a login shell and reinitialize its environment |
GROUP | Make the named group the active group in the new shell |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
newgrp starts a nested shell with a selected group as its effective group. New files
created from that shell normally use the new effective group, subject to setgid
directories and other filesystem rules. It does not permanently change the account's
configured primary group.
Because it creates another shell rather than modifying its parent, run exit to return
to the original environment.
Beginner examples
newgrp developers— start a nested shell whose active group isdevelopers.id -gn— confirm the effective group inside that shell.- Create a test file and inspect it with
ls -lto verify its group ownership. exit— leave the nested shell and return to the original shell.
newgrp developers
id -gn
exit
Advanced examples
newgrp -l developersuses the portable login-shell form and reinitializes the environment as a login shell would. On Linux systems using shadow-utils, a bare-is also accepted as the login-shell spelling.- Where available, use
sg developers -c 'touch release.marker'to run one command non-interactively withdevelopersas the effective group. - Compare
id -gnbefore, during, and after the nested shell to make the scope clear. - In a setgid directory, verify actual ownership because directory inheritance rules can determine the group of new files.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Common mistakes
- Assuming
newgrppermanently changes the account's primary group. - Forgetting that it creates a nested shell and accumulating multiple shell layers.
- Putting
newgrpcasually in shell startup files, which can cause recursion, confusing prompts, or hard-to-debug environment changes. - Expecting it to grant membership that the account does not have; authorization still follows the system's group policy.
- Using an interactive shell when
sg GROUP -c 'COMMAND'would express the task better.
Tips
- Run
id -gnbefore and afternewgrpso you know which shell is active. - Use
exit, rather than opening another terminal layer, to restore the parent shell. - Test file creation in the actual target directory because setgid inheritance matters.
- Use the login form only when you intentionally want the environment reinitialized.
Best practices
- Keep
newgrpan explicit interactive action; do not place it casually in startup files. - Prefer
sg GROUP -c 'COMMAND'for a single non-interactive command where supported. - Use a fresh login for normal long-lived supplementary-membership changes.
- Verify both the effective group and resulting file ownership before sensitive work.
- Exit the nested shell promptly when the group-specific task is complete.
Real-world use cases
- Creating release artifacts with the correct project group without changing account configuration permanently.
- Testing access immediately in a temporary shell after a group-policy adjustment.
- Working briefly in a shared directory whose files should use a particular group.
- Running one build step with
sgwhen an interactive nested shell is unnecessary.
Common interview questions
- Does
newgrpchange the primary group permanently? No. It changes the effective group in a nested shell; the account database is unchanged. - How does it affect new files? They generally use the shell's effective group, subject to directory setgid and filesystem behavior.
- How do you return to the original group context? Run
exitto leave the nested shell. - What is the non-interactive alternative? Where available, use
sg GROUP -c 'COMMAND'.
Frequently Asked Questions
Does newgrp permanently change my primary group?
No. It starts a nested shell with a different effective group and does not rewrite the account's primary-group setting.
How do I return to my original shell?
Run exit or press the shell's end-of-file key combination to leave the nested shell.
How does newgrp affect files I create?
New files use the nested shell's effective group, subject to directory setgid behavior and other filesystem rules.
What should I use for one non-interactive command?
Where available, use sg GROUP -c 'COMMAND' instead of creating an interactive nested shell.
Cheat sheet
Download a quick-reference cheat sheet for newgrp.