This tutorial explains Linux “newgrp” command, options and its usage with examples.
What is newgrp Command?
The newgrp command in Linux is used to log in to a new group. It allows users to change their default group during a login session. If a hyphen (“-“) is included as an argument, the user’s environment is initialized as if they had just logged in; otherwise, the current working environment remains unchanged.
Syntax:
newgrp [-] [group]
- The hyphen (“-“) indicates initializing the environment as in a fresh login.
- “group” is an optional argument specifying the target group.
Description:
The newgrp command changes the current real group ID (GID) to the specified group or, if no group is specified, to the default group listed in the file /etc/passwd. Additionally, newgrp attempts to add the group to the user’s groupset.
If the user is the root, they won’t be prompted for a password. For non-root users, a password prompt may occur under the following conditions:
- The user doesn’t have a password, but the group does.
- The user is not listed as a group member, and the group has a password.
Access will be denied if there is no group password set or if the user is not a member of the group. The group’s information is obtained from shadowed group password file “/etc/gshadow” if available; otherwise, it’s taken from the “/etc/group” file.
Related Files:
/etc/passwd User account information. /etc/shadow Secure user account information. /etc/group Group account information. /etc/gshadow Secure group account information.
newgrp Command Examples:
Example 1: Basic Example
To log in to the group “abc,” use this command:
$ newgrp abc
Example 2: Use of hyphen
To log in to the group “abc” and re-initialize the user environment upon success, use this command:
$ newgrp - abc
Example 3: Resetting Group ID
To change the real group ID back to your original login group, use:
$ newgrp
This command reverts the real group ID to your default login group, restoring your original group access permissions.
Example 4: Changing Group
To change your current group to “staff,” use the following command:
$ newgrp staff
This command will switch your group to “staff” for the current session, allowing you to access files and resources associated with that group.
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.
- Check Linux Books
- Check Information Technology Books
- Practice Programming MCQs
- Apply for Programming Internship