Add User To Group(s) in Linux
Add User To Group(s) in Linux
In this tutorial, we will explain how to add a user to a group in Linux systems. We will
also show you how to remove a user from a group and how to create, delete, and list
groups.
Linux Groups #
Linux groups are organization units that are used to organize and administer user
accounts in Linux. 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.
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 group’s access
rights and be able to run docker commands.
Each user can belong to exactly one primary group and zero or more secondary
groups.
Only root or users with sudo access can add a user to a group.
For example, to add the user linuxize to the sudo group, you would run the
following command:
Always use the -a (append) option when adding a user to a new group. If you omit the
-a option, the user will be removed from any groups not listed after the -G option.
On success, the usermod command does not display any output. It warns you only if
the user or group doesn’t exist.
In the following example, we are removing the user username from the group
groupname :
In the following example, we are changing the primary group of the user linuxize to
developers :
$ id username
If you omit the username, the command will print the information about the currently
logged-in user. Let’s check the user linuxize :
$ id linuxize
Output
uid=1000(linuxize) gid=100(users) groups=100(users),10(wheel),95(storag
From the output above, we see that the primary group of the user is users and it
belongs to wheel , storage , libvirt , docker , and kvm supplementary groups.
Use the groups command to display the user’s supplementary groups:
$ groups linuxize
Output
If no username is passed to the groups command, it will print the currently logged in
user’s groups.
Conclusion
In this tutorial, we have shown you how to add a user to a group.
The same commands apply for any Linux distribution, including Ubuntu, CentOS, RHEL,
Debian, and Linux Mint.
BUY ME A COFFEE