LinuxCommandLibrary

newgrp

Change current group ID

TLDR

Change user's primary group membership

$ newgrp [group_name]
copy

Reset primary group membership to user's default group in /etc/passwd
$ newgrp
copy

SYNOPSIS

newgrp [-l] [group]

PARAMETERS

-l
    Simulates a login shell. This means it reads the user's startup files (like .profile, .bashrc) as if they were logging in, which might reset some environment variables.

group
    The name of the group to switch to. If omitted, it defaults to the user's login group as defined in /etc/passwd.

DESCRIPTION

newgrp is used to change the current effective group ID of the user. It executes a new shell with the user's new primary group or an additional group specified. This command is typically used to gain access to files that are restricted to members of a particular group.

Unlike sg (which changes the effective group for a single command), newgrp starts a new login shell environment where the new group becomes the primary group for that session. The user must be a member of the target group. If no group is specified, it defaults to the user's login group. It's important to note that newgrp changes the primary group and creates a new session, so it doesn't affect the parent shell.

CAVEATS

newgrp executes a new shell. Changes made in the new shell (like group ID) do not affect the parent shell from which newgrp was invoked.
The user must be a member of the target group in /etc/group (or equivalent system database) to successfully switch.
newgrp changes the primary group of the new session, not just adds it to the list of supplementary groups.
Unlike su or sudo, newgrp does not require the user's password, only that the user is a member of the group.

ENVIRONMENT

When newgrp starts a new shell, it typically passes the environment variables from the calling shell. However, if the -l option is used, it often resets some environment variables as a login shell would, ensuring a cleaner session environment.

GROUP MEMBERSHIP

For a user to successfully use newgrp, they must already be listed as a member of the target group in the system's group database (commonly /etc/group). This command does not grant group membership but rather activates an existing one for the session.

HISTORY

newgrp has been a standard Unix/Linux command for a long time, providing a fundamental way for users to switch their primary group identity within a session. Its functionality is crucial for managing permissions and access rights in multi-user environments. It contrasts with su (switch user) and sg (switch group for a single command) by creating a new shell session with a persistent primary group change.

SEE ALSO

sg(1), groups(1), id(1), chgrp(1), gpasswd(1)

Copied to clipboard