How to Refresh and Reload Group Membership on Linux

Linux TLDR
Last Updated:
Reading time: 4 minutes
πŸš€
Quick Overview

To immediately detect the changes after adding the user (let’s say β€œlinuxtldrβ€œ) to the group (let’s say β€œdockerβ€œ) in Linux, use the following command:

$ sudo usermod -aG docker linuxtldr
$ newgrp docker

However, I advise you to read the entire article in order to understand the main reason for this problem and the different ways to resolve it.

In Linux, when you add a user to a new group, the user’s group membership won’t take effect immediately in their current session or any processes they have running.

Traditionally, Linux users are instructed to reboot their Linux machines or log in again to apply these changes, but are there any ways to refresh permissions after adding a user to a new group without rebooting or logging in again?

If this question arises in your mind, β€œIs it necessary to reboot or log in again after changing group membership?” then you are in the right place.

In this article, you will learn different ways to add users to groups and detect the changes without restarting your Linux machine or logging in again.

How to Refresh and Reload Group Membership on Linux

There are multiple ways to refresh and reload group membership on Linux without rebooting or logging in again, and one of the easiest ways I find is using the β€œnewgrp” command.

Method 01: Refreshing and Reloading Group Membership on Linux Using the Newgrp Command

To demonstrate the steps, we will create a new group, add our current user to that group, and apply the changes without refreshing or reloading. To begin…

1. Launch your terminal and use the following command to create a new group, which we’ll call β€œtestgroupβ€œ:

$ sudo groupadd testgroup

Output:

Creating new testgroup for demo purpose

2. Confirm the successful creation of the new group by inspecting the β€œ/etc/group” entries with the grep or getent commands.

$ grep "testgroup" /etc/group

#OR

$ getent group testgroup

Output:

Checking the testgroup is added in /etc/group file

3. After successfully creating and verifying the group, you can add the current user or any other user to this group by specifying their login name.

#Add the current user
$ sudo usermod -aG testgroup $USER

#OR

#Add any other user by specifying their login name
$ sudo usermod -aG testgroup linuxtldr

Output:

Adding current user to testgroup

4. Once the user has been added to the group, confirm the current group members by using the grep or id commands.

$ grep 'testgroup' /etc/group

#OR

$ id linuxtldr

Output:

Verifying the user is added to testgroup by checking the current members of testgroup

5. To refresh and reload the group membership information on your Linux machine without restarting or logging in again, use the following β€œnewgrp” command followed by the group name:

$ newgrp testgroup

Output:

Refreshing group membership without restarting or re-login

6. Lastly, list all the groups to which the user is assigned to confirm that β€œtestgroup” is included in the list.

$ groups

#OR

$ id

Output:

Verifying the user is added to testgroup

Tada!!! You successfully created a new group, added the user to that group, and refreshed the group membership information without restarting or re-logging your Linux machine.

Method 02: Temporarily Switch to the User with the New Group Membership

If you are a system administrator and have superuser privileges, you can use the su command to temporarily switch to the user with the new group membership.

For example, I’ve added the β€œlinuxtldr” user to the β€œtestgroup” group and immediately checked the group membership using the following command:

$ sudo usermod -aG testgroup linuxtldr
$ groups

Output:

Adding and checking the user group membership

In the image above, it’s clear that the group membership information hasn’t been updated. To address this, use the su command to initiate a new shell session and verify the group membership once more.

πŸ“
While performing these steps, you may be required to enter the user password.
$ su - linuxtldr
$ groups

Output:

Create a subshell and check the group membership information once again

Currently, you’re within a subshell (or child shell) spawned by your parent shell; therefore, when you exit this subshell (or session), you’ll revert to your initial user environment.

$ groups                                                                                                                           #Executing in subshell
$ exit                                                                                                                                 #Exiting the subshell
$ group                                                                                                                             #Executing in parent shell

Output:

Qutting subshell and rechecking the group membership

I assume you properly understood how this method works, what its drawbacks are, and when you should use it.

Method 03: Running Processes with the New Group Using the SG Command

The β€œsg” command, which stands for β€œset groupβ€œ, is used to execute a command with a specified group’s privileges.

It allows you to temporarily change the group context for a single or multiple commands without permanently affecting the user’s group membership.

This can be useful when you need to perform specific tasks with different group permissions without switching users or logging in and out.

Here’s an example in which I first verify the groups the current user is linked to, then create a file for group verification, and finally execute the file using the β€œsg” command to demonstrate that it runs within the specified group.

#Listing the users associated with which groups 
$ groups

#Creating a file and appending the group command in it
$ echo "groups" > myfile

#Checking the content of file
$ cat myfile

#Assigning executable permission to the file
$ chmod +x myfile

#Executing the file under testgroup with the sg command
$ sudo sg testgroup -c ./myfile

Output:

Executing file under specific group using the sg command

Here’s another example: you’re not a member of the docker group, but you want to run a docker command with docker group privileges using the sg command.

πŸ“
This example is not for practical use, as you can run the docker command with plain sudo privileges without using the β€œsg” command.
$ sudo sg docker -c "docker ps"

Output:

Running docker command under docker group privileges using sg command

And here comes the end of this article!

Final Word

I hope you find this article useful. If you have any questions or queries related to the article, do let me know in the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.