How to change the primary group of a user in Linux?
In the Linux Operating system, we have multiple options and commands available to change the primary group of a user. But there are two effective methods available for changing the primary group of a user:
- Method 1: Change the primary group using the usermod command
- Method 2: Change the primary group using /etc/passwd
- Method 3: Change the primary group using the newgrp command
So, let’s see each of the listed methods one by one:
Method 1: Change the primary group using the usermod command
Follow the below-mentioned steps to change the primary group using the usermod command in Linux.
Step 1: Check the primary group of a user (if doesn’t exist)
Syntax:
sudo groups username
- sudo: For granting root privileges, because the command will execute only if root permission is granted.
- groups: It is a command used to check the details groups of a particular user.
Command:
sudo groups anubhav
Here, we are checking whether the primary group for a user exists or not. In the below screenshot, we can see that the user “anubhav” is present in the group “users“. This will assure us that change the primary group in the following steps.
Step 2: Create a new group for the current user
Syntax:
sudo groupadd groupname
- groupadd: command to create a new group
Command:
sudo groupadd test
Here, we are creating a new group. In the below screenshot is demonstrated to create a group “test” and further used for making it as a primary group of “anubhav”.
Step 3: Setting or Modifying a User’s Primary Group
Syntax:
sudo usermod -g groupname username
- usermod: It is a command user to change/modify the user’s properties.
- -g: It is used to make the user’s primary group. The group should exist.
Command:
sudo usermod -g test anubhav
Here, we are adding the primary group, In the below screenshot we can see that “test” is assigned as the primary group of “anubhav”.
Step 4: Check/Verify the related group of a particular user
Command:
sudo groups anubhav
Here, we can again check the user’s group, In the screenshot below “test” is added to the primary group of “anubhav”.
Method 2: Change the primary group using /etc/passwd
Follow the below-mentioned steps to change the primary group using the /etc/passwd in Linux.
Step 1: Create a new group for a user (if doesn’t exist)
Syntax:
sudo groupadd groupname
- groupadd: command to create a new group
Command:
sudo groupadd gfg
Here, we are creating a new group, In the below screenshot we have created the group “gfg” and we will make it the primary group.
Step 2: Take a backup of the/etc/passwd file
Syntax:
sudo cp /etc/passwd backup-filename
- /etc/passwd: It is a plain text file that stores the user account information on Linux.
Command:
sudo cp /etc/passwd /etc/passwd.backup
Before we proceed to make changes in the/etc/passwd file, we need to create a backup. In the below screenshot, we have created a backup. In the worst scenario, if the system will show the errors then we can reset to its default setting by using the backup file.
Step 3: Alterusers/changes in /etc/passwd
Command:
sudo nano /etc/passwd
We can make changes using any Linux text editor. In the below screenshot, we are using the “nano” text editor to make it changes.
Step 4: Add a groupname in the user’s entry
Here, we need to mention the group name to make it a primary group. In the below screenshot, we have added an entry of a new group “gfg” after the GID(Group Identification).
Method 3: Change the primary group using the newgrp command
Follow the below-mentioned steps to change the primary group using the newgrp command in Linux.
Step 1: Check the current groups of a particular user
Syntax:
sudo groups username
- groups: It lists the Linux groups that the user belongs to.
Command:
sudo groups csuser
Here, we are checking whether the primary group for a user exists or not. In the below screenshot, we can see that the “users”, and “sudo” is a groups of “csuser”.
Step 2: Create a new group (if doesn’t exist)
Syntax:
sudo groupadd groupname
- groupadd: It is a command to create a new group.
Command:
sudo groupadd tech
Here, we have created a new group “tech” and we will make it a primary group of particular users.
Step 3: Append/Add group to the respective user
Syntax:
sudo usermod -aG groupname username
- usermod: It is used to modify/change in a user’s properties
- a: append the group
- G: For specifying the groupname
Command:
sudo usermod -aG tech csuser
Here, we need to append the group that we have created before such that we can make it as primary group of users. In the below screenshot, we have added the group “tech” in “csuser”.
Step 4: Adding new primary group
Syntax:
newgrp newgroupname
- newgrp: It is the command used for creating a primary group only for the current session.
Command:
newgrp tech
Here, we are making a primary group of particular users. In the below screenshot, we have created “tech” as a primary group using the newgrp for the current user.
Step 4: Verify the current group
Command:
sudo groups csuser
Here, we can check the primary user. In the below screenshot, we can see that “tech” is added to the primary group of “csuser”.