Linux Group Management: Easy Steps for Adding Multiple Users in Group at Once.

Mihir SuratwalaMihir Suratwala
3 min read

Last Blog Review -

In the last blog we learnt, how to create a Linux User group, but adding user’s one by one to the group & adding them one by one will help to add new user’s getting adding to the project later on, without over-riding the permissions of existing user.

User-Group Management, by adding multiple user’s at once -

All the groups which are created are stored in directory /etc/group directory. So, we will create a group first and then add the user's at once.

A) Group using multiple argument -M = It will add all the user together to the group and also overwrite the already present user in the group.

  • Lets create a group named appgr first using command "sudo groupadd appgr". Once you create the group you can check by going inside the directory /etc/group.

    So, as you see here the group "appgr" is created. Also, we can see the user "ubuntu" is present in this directory because the user is also considered an individual group. Now as you see "appgr:x:1001:" doesn't have any user named ahead of it. Once we add user at once they will be added in the group and we can check that.

i-XXXXXXXXXXXXXXXXX (EC2)
--------------------------
ubuntu@ip--------:~$ sudo groupadd appgr
ubuntu@ip--------:~$ sudo cat /etc/group
.
.
ubuntu:x:1000:
appgr:x:1001:
  • Let’s create user's for adding them in “appgr” group. Let’s say the user's are appus1, appus2, appus3.

      i-XXXXXXXXXXXXXXXXX (EC2)
      --------------------------
      ubuntu@ip--------:~$ sudo useradd appus1 -m
      ubuntu@ip--------:~$ sudo passwd appus1
      New password: 
      Retype new password: 
      passwd: password updated successfully
      ubuntu@ip--------:~$ sudo useradd appus2 -m
      ubuntu@ip--------:~$ sudo passwd appus2
      New password: 
      Retype new password: 
      passwd: password updated successfully
      ubuntu@ip--------:~$ sudo useradd appus3 -m
      ubuntu@ip--------:~$ sudo passwd appus3
      New password: 
      Retype new password: 
      passwd: password updated successfully
      ubuntu@ip--------:~$
    
  • After creating these user's we will put them in the group together using -M "multiple" argument using command

    "sudo gpasswd -M appus1, appus2, appus3 appgr"

i-XXXXXXXXXXXXXXXXXX(EC2)
--------------------------
ubuntu@ip--------:~$ sudo gpasswd -M appus1, appus2, appus3 appgr
ubuntu@ip--------:~$ sudo cat /etc/group    
.
.
ubuntu:x:1000:
appgr:x:1001:appus1,appus2,appus3
appus1:x:1002:
appus2:x:1003:
appus3:x:1004:
ubuntu@ip--------:~$

See here the group "appgr" has now all 3 the user's added together due to -M argument i.e. Multiple.

  • Now let's create 2 more user's "appus4", "appus5"
i-XXXXXXXXXXXXXXXXX (EC2)
--------------------------    
ubuntu@ip--------:~$ sudo useradd appus4 -m
ubuntu@ip--------:~$ sudo passwd appus4
New password: 
Retype new password: 
passwd: password updated successfully
ubuntu@ip--------:~$ sudo useradd appus5 -M
ubuntu@ip--------:~$ sudo passwd appus5
New password: 
Retype new password: 
passwd: password updated successfully
ubuntu@ip--------:~$
  • Next put them together in the group "appgr" using -M argument i.e. Multiple. When we do that the, group "appgr" which already has "appus1" and "appus2", “appus3” user's in it, they will be overwritten & only "appus4, appus5" user's will be there in the "appgr" group.
i-0699d57257c6a1732 (EC2)
--------------------------
ubuntu@ip--------:~$ sudo gpasswd -M  appus4,appus5 appgr
ubuntu@ip--------:~$ sudo cat /etc/group
ubuntu:x:1000:
appgr:x:1001:appus4,appus5
appus1:x:1002:
appus2:x:1003:
appus3:x:1004:
appus4:x:1005:
appus5:x:1006:
ubuntu@ip--------:~$
  • So, see after adding "appus4, appus5" user's together using -M argument in the group "appgr" then previous user's present in the group "appus1, appus2, appus3" are overwritten.

Conclusion -

In this way we can create group and add multiple user’s at once to the group, which is important for providing same set of permissions to the people belonging to same department having same set of actions to perform on a assignment. It save’s time in administrative task where you want to add multiple user’s in a group at once.

💡
If you enjoyed this post, don't forget to share it with your friends! Have questions or thoughts? Drop a comment below – I'd love to hear your insights and continue the conversation. And make sure to subscribe to get more content straight to your inbox! Stay tuned for more tips, ideas, and inspiration. Until next time!
0
Subscribe to my newsletter

Read articles from Mihir Suratwala directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mihir Suratwala
Mihir Suratwala

Hi, How are you !! Hope you doing good.... I got introduced to Cloud initially. As I went ahead learning what is cloud and how it works, then got to know a field which is DevOps that makes Cloud model more effective. So, as I started working & got good experience on AWS. I have been learning the DevOps tool and technologies on how to use it with the Cloud, which will give me good understanding on how Cloud and DevOps go hand in hand to deploy my applications.