How to use the command "newgrp" (with examples)
- Linux
- December 25, 2023
The “newgrp” command is used to switch the primary group membership of a user. It allows the user to temporarily join a different group, giving them access to the group’s permissions and resources.
Use case 1: Change user’s primary group membership
Code:
newgrp group_name
Motivation: Changing a user’s primary group membership can be useful when you need to work with files or directories that belong to a different group. By switching to the desired group, you can perform operations that are limited to that group.
Explanation:
- “newgrp” is the command to switch the primary group membership.
- “group_name” is the name of the group you want to switch to.
Example output: Let’s assume we have a user named “john” who wants to join the group called “developers”. John can use the following command:
newgrp developers
After executing this command, John’s primary group membership will be switched to “developers”, and he will have access to the group-specific resources and permissions.
Use case 2: Reset primary group membership to user’s default group in /etc/passwd
Code:
newgrp
Motivation: Sometimes, you may want to reset your primary group membership to your default group as specified in the “/etc/passwd” file. This can be useful if you have previously switched to a different group and want to revert back to your default group.
Explanation: When the “newgrp” command is used without specifying a group name, it resets the primary group membership to the user’s default group as defined in the “/etc/passwd” file.
Example output: Assuming the default group for user “john” is “users”, he can use the following command:
newgrp
After executing this command, John’s primary group membership will be reset to “users”, which is his default group.
Conclusion:
The “newgrp” command is a versatile tool for managing primary group memberships. Whether you need to temporarily join a different group or reset your primary group to the default, this command provides the flexibility to easily switch between different group contexts.