
newgrp Command in Linux
The newgrp command in Unix and Linux systems allows you to change the current group ID (GID) of your session. This can be useful if you need to access files or execute commands that are restricted to a specific group.
- When you use newgrp followed by a group name, your current session's group ID changes to the specified group. If you don't provide a group name, it changes to your default group listed in the /etc/passwd
- If you include a hyphen (-) after newgrp, it reinitializes your environment as if you had just logged in. This means it resets environment variables and settings to the defaults for the new group.
- When you run the newgrp command, it tries to add the specified group to your user groupset. This means that it tries to change your current session to include the new group, allowing you to have permissions and access associated with that group.
If you are the root user, you won't be prompted for a password when changing groups. On the other hand, if you are not the root user, newgrp will prompt you for a password under the following conditions:
- You don't have a password: If your user account does not have a password, but the group you are trying to join does have one, you will be asked to enter the group's password.
- Not listed as a group member: If you are not listed as a member of the group in the system's group files (i.e., /etc/group or /etc/gshadow), and the group has a password, you will also be prompted to enter the group's password.
If there is an entry for the group in the shadowed group password file (/etc/gshadow), the password and member list for the group are taken from this file. If there's no entry in /etc/gshadow, the system uses the group entry in the /etc/group file.
In addition, if the group you are trying to join has no password set, and you are not listed as a member of the group in the system's group files, you will be denied access. This means you will not be able to switch to that group using newgrp.
Table of Contents
Here is a comprehensive guide to the options available with the newgrp command −
Syntax of newgrp Command
The following is the general syntax for the newgrp command:
newgrp [-] [group]
Examples of newgrp Command in Linux
The following are practical examples that illustrate how the newgrp command can help manage permissions dynamically within a session.
Change to a Specific Group
To change to a specific group, you can simply use the following command:
newgrp Tutorialspoint
This command changes the current group to "Tutorialspoint". Any files or operations performed after this will be done with the permissions associated with the "Tutorialspoint" group.

Reinitialize Environment and Change Group
To reinitialize the environment and change the group, you can use the following command:
newgrp - Tutorialspoint
The - option reinitializes the environment as if you logged in again, using the settings from the target group's default login shell. This is useful when you want to ensure a fresh environment while switching to a new group.

Change to Default Group
When you simply type newgrp without specifying a group name, it switches your current group to the default group defined for your user account. This is especially useful if you previously changed your group during a session and want to revert to your default group.
In this example, no specific group is mentioned, so the command defaults to the group assigned to your user in /etc/passwd.
newgrp
This command ensures that any new files you create or commands you execute will have the permissions associated with your default group.

Prompt for Group Password
To switch to a group that has a password (e.g., accounting) and you are not a listed member, you can use the following command:
newgrp accounting
If the password is correct, you're current group ID changes to "accounting". You now have the permissions associated with the "accounting" group for the duration of the session.
If the password is incorrect, you remain in your current group. The command fails, and an error message is displayed.
This process ensures that only authorized users can switch to groups that have restricted access through passwords.

Handle Group with No Password and Not a Member
If you try to change to a group (e.g., restricted) that does not have a password and you are not listed as a member, you can use the following command:
newgrp restricted
Since there is no group password and you're not a member, you will be denied access to the group.

Conclusion
The newgrp command is a useful tool for dynamically switching group permissions in a Linux environment. However, it comes with restrictions to ensure security and proper access control.
For groups without passwords, only listed members can switch to the group using newgrp. Attempts to switch to such a group without membership will result in a "Permission denied" error. This reinforces the importance of proper group membership management and the role of administrators in setting appropriate group access policies.
By understanding how newgrp works, you can effectively manage group-based permissions within their sessions.