Group Management in Linux (CentOS7 /MINT RHEL7)
What is Group in Linux?
In Linux, a group is a
collection of users. The main purpose
of the groups is to define
a set of privileges like read, write, or execute permission for a given
resource that can be shared among the users within the group. Users can be added
to an existing group to utilize
the privileges it grants.
When
first time user account is created, a group with the same name is created.
By default
created group with user
Other
users can be added to the group later.
The
primary purpose of groups is to define a set of privileges such as reading,
writing, or executing permission for a given resource that can be shared among
the users within the group.
There are two types of groups in Linux operating
systems:
·
Primary group –
When a user creates a file, the file’s group is set to the user’s primary
group. Usually, the name of the group is the same as the name of the user. The
information about the user’s primary group is stored in the
/etc/passwd
file.
·
Secondary or supplementary group - Useful when you want to grant certain file permissions
to a set of users who are members of the group. For example, if you add a
specific user to the
docker
group, the user will inherit the access rights from the
group, and be able to run docker commands.
·
·
Add a New Group
Displaying
the groups an user is a member of
After
adding the user to a supplementary group, you can verify that it now actually
belongs to such group(s):
# id [username]
How to Add an Existing User to a Group
To add an existing user to a secondary group, use the
usermod -a -G
command
followed the name of the group and the user:sudo usermod -a -G groupname username
How to Add an Existing User to Multiple Groups in One Command
If you want to add an existing user to multiple
secondary groups in one command, use the
usermod
command followed by the -G
option name of the group separated by ,
(commas):sudo usermod -a -G group1,group2 username
How to Change a User’s Primary Group
To change a user primary group, use the
usermod
command
followed by the -g
option:sudo usermod -g groupname username
Deleting a group
To
delete a group, you’ll want to use groupdel,
# groupdel [group_name]
How to Create a New User and Assign Groups in One Command
The
following
useradd
command creates a new user named nathan
with primary
group users
and secondary groups wheel and developers.sudo useradd -g users -G wheel,developers nathan