Home Linux CentOS How to Add User to a Group in CentOS 7

How to Add User to a Group in CentOS 7

Key Points:
How to create new groups?
How to add a user to a group?
How to remove a user from a single group?

  1. Primary Group – When a user is created, automatically a group is created with its username, it’s called primary group. A user creates files or directory and operating system assigns the group to them.
  2. Secondary Group – A user can belong to more than one group. It is used to share file or directory with the same group members.

Create A New Group

For example, you want to create a group name centos. You can check created group from path cat /etc/group

[root@localhost ~]# groupadd centos
[root@localhost ~]# cat /etc/group
create a group in centos
create a group in centos

How to add user in a Group

Let us assume, want to add user “arun” in a “centos” group. First, check what groups a user belongs and first group name is always primary group.

[arun@localhost ~]$ groups arun
[root@localhost ~]# usermod -aG centos arun
  • usermod: used to modify user group.
  • -a: –append, Add the user to the supplementary group(s). Use only with the -G option.
  • centos: it is the group name
  • arun: user name
add user in a group in centos 7
add user in a group in centos 7

How to remove a user from a single group

Remove user arun only from centos group

root@localhost ~]# gpasswd -d arun centos
command to remove user from a single group
command to remove user from a single group

Key File:

  • /etc/groups: Keeps the record of the groups created on a system.



LEAVE A REPLY

Please enter your comment!
Please enter your name here