How to Add User to Group in Linux Ubuntu 19.04

How to Add User to Group in Linux Ubuntu 19.04

Do You have added a user in wrong group, and want to add in right group? or Add new user in group, or want to add a user in multiple group?

You will get reply of all questions from this article.

Only reading full article gives you the complete guide on how to Add User to Group in Linux, I am using Ubuntu 19.04 Operating system but this operation will be the same on other Linux. You can perform this task on CentOS, RHEL, etc.

Disclaimer: This article does not cover to create user in Linux, you can read “How to create user in Linux step by step Guide”

Article Content

Check Available Users and Groups in Linux

To Check current logged in user is the member of which groups, user following syntax:

$groups

For the Example, I logged in with ‘vijay’ user, And I want to know to which groups are belongs ot vijay.

I run command ‘groups and I got following result

vijay@Ubuntu-19:~$groups 
vijay adm cdrom sudo dip plugdev lpadmin sambashare
vijay@Ubuntu-19:~$

If you want to details of groups for current user then run following command $id

vijay@Ubuntu-19:~$id
uid=1000(vijay) gid=1000(vijay) groups=1000(vijay),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),118(lpadmin),129(sambashare)
vijay@Ubuntu-19:~$

All information is saved into /etc/group file. You can see all the group information from this file by using same command.

$cat /etc/group

vijay@Ubuntu-19:~$sudo cat /etc/group | tail -10
vijay:x:1000:
sambashare:x:129:vijay
systemd-coredump:x:999:
cyrage:x:1001:
ram:x:1002:
ansh:x:1003:
nonu:x:1004:
anshika:x:1201:
hema:x:1202:
amit:x:1203:
vijay@Ubuntu-19:~$

You can retrieve the user information from /etc/passwd file.

vijay@Ubuntu-19:~$sudo cat /etc/passwd | tail -10
vijay:x:1000:1000:Vijay,,,:/home/vijay:/bin/bash
systemd-coredump:x:999:999:systemd Core Dumper:/:/sbin/nologin
cyrage:x:1001:1001:Cyrage,,,:/home/cyrage:/bin/bash
ram:x:1002:1002::/home/ram:/bin/sh
ansh:x:1003:1003::/home/ansh:/bin/sh
nonu:x:1004:1004::/data/project/:/bin/sh
surya:x:1200:1004::/home/surya:/bin/sh
anshika:x:1201:1201::/home/anshika:/bin/sh
hema:x:1202:1202::/home/hema:/bin/sh
amit:x:1203:1203:Amitabh Bachhan:/home/amit:/bin/sh
vijay@Ubuntu-19:~$

To Check the information of the groups belong to particular username.

$sudo id username

vijay@Ubuntu-19:~$sudo id hema
uid=1202(hema) gid=1202(hema) groups=1202(hema)
vijay@Ubuntu-19:~$sudo id nonu
uid=1004(nonu) gid=1004(nonu) groups=1004(nonu)
vijay@Ubuntu-19:~$

How to create a new group

Before start adding and removing users form the group, I am going to show you, how can you create a group

To create new group you can use addgroup command followed by groupname

$sudo addgroup groupname

In the Example, I am going to add three groups name hr,mgr, and admin.

vijay@Ubuntu-19:~$sudo addgroup hr
Adding group `hr' (GID 1005) ...
Done.
vijay@Ubuntu-19:~$sudo addgroup mgr
Adding group `mgr' (GID 1006) ...
Done.
vijay@Ubuntu-19:~$sudo addgroup admin
Adding group `admin' (GID 1007) ...
Done.
vijay@Ubuntu-19:~$

You can check the information of new added group: See commands below.

vijay@Ubuntu-19:~$sudo cat /etc/group | tail -10
cyrage:x:1001:
ram:x:1002:
ansh:x:1003:
nonu:x:1004:hema
anshika:x:1201:
hema:x:1202:
amit:x:1203:
hr:x:1005:
mgr:x:1006:
admin:x:1007:
vijay@Ubuntu-19:~$

How to add Existing user to existing Group

You can add an existing user to an existing secondary group, use the usermod command followed by -a and -G option and the groupname and username

$sudo usermod -a -G groupname username

vijay@Ubuntu-19:~$sudo usermod -a -G hr hema
vijay@Ubuntu-19:~$

Check the status of groups for hema user.

vijay@Ubuntu-19:~$sudo id hema
uid=1202(hema) gid=1202(hema) groups=1202(hema),1005(hr)
vijay@Ubuntu-19:~$

How to add a user to multiple groups

It is quite easy to add a user to multiple groups. You can use the same command to add a user to the group but a little bit change. See in the example.

$sudo usermod -a -G group1,group2,group3 username

I am adding user “nonu” to groups hr,mgr,admin

vijay@Ubuntu-19:~$sudo usermod -a -G hr,mgr,admin nonu
vijay@Ubuntu-19:~$

Result is here:

vijay@Ubuntu-19:~$sudo id nonu
uid=1004(nonu) gid=1004(nonu) groups=1004(nonu),1005(hr),1006(mgr),1007(admin)
vijay@Ubuntu-19:~$

How to remove a user from Group

If you want to remove user from the group user gpasswd command followed by -d and username and group name

$sudo gpasswd -d username group

vijay@Ubuntu-19:~$sudo gpasswd -d nonu hr
Removing user nonu from group hr
vijay@Ubuntu-19:~$

Check the result:

vijay@Ubuntu-19:~$sudo id nonu
uid=1004(nonu) gid=1004(nonu) groups=1004(nonu),1006(mgr),1007(admin)
vijay@Ubuntu-19:~$

How to create a new user and assign new group

When your boss asks to create a new user ‘nitin‘, and it should be the member of mgr and admin group.

You can use command useradd command followed by -g for primary group -G for secondary group

$sudo useradd -g primary_group_name -G group1,group2 username

You can read full article on Create user in linux

vijay@Ubuntu-19:~$sudo useradd -g users -G mgr,admin nitin
vijay@Ubuntu-19:~$

Check the status of user nitin

vijay@Ubuntu-19:~$sudo id nitin
uid=1204(nitin) gid=100(users) groups=100(users),1006(mgr),1007(admin)
vijay@Ubuntu-19:~$

Conclusion

By now you have learnt many tasks of Linux System Administrator, You move one more step to become expert in Linux.

Manage users and groups is important task forever for Admin.

If you have any question related this article comment below.

You can write/contact me directly by using email address vijay@cyberpratibha.com

Cheers!

If Appreciate My Work, You should consider:

Leave a Comment