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 20.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
Article Content
- Check Available Users and Groups in Linux
- How to create a new Group in Linux
- How to add Existing user to existing Group
- How to add a user to multiple groups
- How to remove a user from Group
- How-to-create-a-new-user-and-assign-new-group
- Conclusion
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
[email protected]:~$groups vijay adm cdrom sudo dip plugdev lpadmin sambashare [email protected]:~$
If you want to details of groups for current user then run following command $id
[email protected]:~$id uid=1000(vijay) gid=1000(vijay) groups=1000(vijay),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),118(lpadmin),129(sambashare) [email protected]:~$
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
[email protected]:~$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: [email protected]:~$
You can retrieve the user information from /etc/passwd file.
[email protected]:~$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 [email protected]:~$
To Check the information of the groups belong to particular username.
$sudo id username
[email protected]:~$sudo id hema uid=1202(hema) gid=1202(hema) groups=1202(hema) [email protected]:~$sudo id nonu uid=1004(nonu) gid=1004(nonu) groups=1004(nonu) [email protected]:~$
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.
[email protected]:~$sudo addgroup hr Adding group `hr' (GID 1005) ... Done. [email protected]:~$sudo addgroup mgr Adding group `mgr' (GID 1006) ... Done. [email protected]:~$sudo addgroup admin Adding group `admin' (GID 1007) ... Done. [email protected]:~$
You can check the information of new added group: See commands below.
[email protected]:~$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: [email protected]:~$
How to add Existing user to existing Group
You can add an existing user to an existing secondary group, use the
$sudo usermod -a -G groupname username
[email protected]:~$sudo usermod -a -G hr hema [email protected]:~$
Check the status of groups for hema user.
[email protected]:~$sudo id hema uid=1202(hema) gid=1202(hema) groups=1202(hema),1005(hr) [email protected]:~$
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
[email protected]:~$sudo usermod -a -G hr,mgr,admin nonu [email protected]:~$
Result is here:
[email protected]:~$sudo id nonu uid=1004(nonu) gid=1004(nonu) groups=1004(nonu),1005(hr),1006(mgr),1007(admin) [email protected]:~$
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
[email protected]:~$sudo gpasswd -d nonu hr Removing user nonu from group hr [email protected]:~$
Check the result:
[email protected]:~$sudo id nonu uid=1004(nonu) gid=1004(nonu) groups=1004(nonu),1006(mgr),1007(admin) [email protected]:~$
How to create a new user and assign new group
When your boss asks to create a new user ‘
You can use command
$sudo
You can read full article on Create user in linux
[email protected]:~$sudo useradd -g users -G mgr,admin nitin [email protected]:~$
Check the status of user nitin
[email protected]:~$sudo id nitin uid=1204(nitin) gid=100(users) groups=100(users),1006(mgr),1007(admin) [email protected]:~$
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 [email protected]
Cheers!
3 thoughts on “How to Add User to Group in Linux Ubuntu 20.04 Complete Tutorial”
This was amazing! Thank you!
Thank you
adduser user group
is much simpler.
RTFM