Linux newgrp Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the newgrp command in Linux to change the primary group membership of the current shell session. The lab covers understanding the newgrp command, creating and switching to a new group, and managing group permissions with newgrp. You will explore practical examples and learn how to effectively utilize the newgrp command for user and permission management tasks.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/UserandGroupManagementGroup -.-> linux/groups("`Group Displaying`") linux/UserandGroupManagementGroup -.-> linux/groupadd("`Group Adding`") linux/UserandGroupManagementGroup -.-> linux/chgrp("`Group Changing`") linux/UserandGroupManagementGroup -.-> linux/id("`User/Group ID Displaying`") linux/BasicFileOperationsGroup -.-> linux/chown("`Ownership Changing`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") subgraph Lab Skills linux/groups -.-> lab-422839{{"`Linux newgrp Command with Practical Examples`"}} linux/groupadd -.-> lab-422839{{"`Linux newgrp Command with Practical Examples`"}} linux/chgrp -.-> lab-422839{{"`Linux newgrp Command with Practical Examples`"}} linux/id -.-> lab-422839{{"`Linux newgrp Command with Practical Examples`"}} linux/chown -.-> lab-422839{{"`Linux newgrp Command with Practical Examples`"}} linux/chmod -.-> lab-422839{{"`Linux newgrp Command with Practical Examples`"}} end

Understand the newgrp Command

In this step, you will learn about the newgrp command in Linux, which allows you to change the primary group membership of the current shell session.

The newgrp command is used to switch the current user's primary group to a different group. This is useful when you need to perform actions that require the permissions of a different group.

Let's start by exploring the basic usage of the newgrp command:

## Check the current user's primary group
$ id -gn
labex

## Use newgrp to switch to a different group
$ sudo newgrp developers

Example output:

## No output, but the primary group has been changed

After running newgrp developers, the user's primary group is now set to the developers group. This means that any new files or directories created in the current shell session will be owned by the developers group.

To verify the group change, you can use the id command again:

$ id -gn
developers

The newgrp command is particularly useful when you need to perform tasks that require the permissions of a specific group. For example, if you're working on a project that requires access to a shared directory owned by the developers group, you can use newgrp to switch to that group and gain the necessary permissions.

It's important to note that the newgrp command only affects the current shell session. If you open a new terminal or log out and log back in, your primary group will revert to the default group set for your user account.

Create and Switch to a New Group

In this step, you will learn how to create a new group and switch to it using the newgrp command.

First, let's create a new group called "project-team":

## Create a new group
$ sudo groupadd project-team

Example output:

## No output, but the group has been created

Now, let's switch to the new "project-team" group using the newgrp command:

## Switch to the new group
$ newgrp project-team

Example output:

## No output, but the primary group has been changed

To verify that the group has been changed, you can use the id command:

$ id -gn
project-team

The output shows that the user's primary group is now "project-team".

When you create a new file or directory in the current shell session, it will be owned by the "project-team" group. This is useful when you need to collaborate on a project with a specific group of users.

Remember that the newgrp command only affects the current shell session. If you open a new terminal or log out and log back in, your primary group will revert to the default group set for your user account.

Manage Group Permissions with newgrp

In this final step, you will learn how to manage group permissions using the newgrp command.

Let's start by creating a new directory that will be owned by the "project-team" group:

## Create a new directory
$ mkdir ~/project/shared
$ sudo chown -R labex:project-team ~/project/shared
$ chmod -R 770 ~/project/shared

Example output:

## No output, but the directory has been created and permissions have been set

Now, let's switch to the "project-team" group using newgrp:

$ newgrp project-team

Example output:

## No output, but the primary group has been changed

With the "project-team" group as the primary group, you can now create new files and directories within the ~/project/shared directory, and they will be owned by the "project-team" group.

## Create a new file in the shared directory
$ touch ~/project/shared/project-file.txt

Example output:

## No output, but the file has been created

To verify the file ownership and permissions, you can use the ls -l command:

$ ls -l ~/project/shared
total 0
-rw-rw-r-- 1 labex project-team 0 Apr 12 12:34 project-file.txt

The output shows that the new file is owned by the "project-team" group, and the group has read and write permissions.

This workflow is useful when you need to collaborate on a project with a specific group of users. By switching to the appropriate group using newgrp, you can ensure that new files and directories are owned by the correct group, making it easier to manage permissions and access control.

Summary

In this lab, you learned about the newgrp command in Linux, which allows you to change the primary group membership of the current shell session. You explored how to use newgrp to switch to a different group, and how this affects the ownership of new files and directories created in the current session. Additionally, you learned how to create a new group and switch to it using the newgrp command, which is useful when you need to perform tasks that require the permissions of a specific group.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like