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 “
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 “
$ sudo groupadd testgroup
$ grep "testgroup" /etc/group
$ getent group testgroup
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
#Add any other user by specifying their login name
$ sudo usermod -aG testgroup linuxtldr
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
$ id linuxtldr
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
6. Lastly, list all the groups to which the user is assigned to confirm that “
testgroup” is included in the list.
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
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.
$ su - linuxtldr
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
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
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
#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
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
$ sudo sg docker -c "docker ps"
And here comes the end of this article!
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!