newgrp Command with Examples in Linux

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.

advertisement
advertisement

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:

  1. The user doesn’t have a password, but the group does.
  2. 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:

advertisement
$ 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.

advertisement

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.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.