In this tutorial, we’ll discuss how to add a user to a group on Linux. We’ll also show you how to remove users from groups and how to create, delete, and list groups.
We hope that you will open a Linux distribution console to follow the guide on this page so you can better understand how to add users and groups in Linux.
We also recommend that you test any online tutorials or guides on a virtual machine (vmware or virtualbox) before deploying to a production server, so that you don’t mess with your running system when something goes wrong.
You can learn how to install VMware on Ubuntu, CentOS, and VirtualBox on Ubuntu, Fedora and CentOS.
To add users to a group, you must be logged in as a user with access Sudo or root.
Set on Linux
A group is an organizational unit used to organize and manage user accounts on Linux. The main purpose of groups is to assign a set of privileges such as read, write or execute permissions to a specific resource that can be shared among the users in the group.
There are two types of combinations in Linux:
- Primary group– When a user creates a file, the file group is assigned to the main user group. Usually, the group name is the same as the username. Information about a user’s primary group is stored in a file
- Secondary group Or additional group – useful when you want to grant specific file permissions to a group of users who are members of the group. For example, if you add a specific user to a group
docker, This user will automatically have the same access rights as the other users in the group
dockerHe is able to carry out various orders
Each user can have only one primary group, or they can be linked to one or more secondary groups.
How to add a user to the group
To add existing users to a secondary group, use the command
usermod Followed by the options
-G And group name:
sudo usermod -a -G namagrup username
For example, to add a user
adhit For a specific group
sudo , You can run the following command:
sudo usermod -a -G sudo adhit
Always use the toggle switch
-a (Append) when adding a user to a new group. If you eliminate
-a, The user will be deleted from any group not yet registered
If successful, command
usermod It will not display any output. The usermod command will only display a warning when the user or group does not exist, so you must create the group first.
How to add existing users to multiple groups
If you want to add existing users to multiple secondary groups in one command, use the command
usermod Followed by a flag
-G The group names are separated by
sudo usermod -a -G group1,group2 username
How to remove a user from a group
To remove a user from a group, use the command
gpasswd, In the following example we delete the named username
rachma From a specific group
sudo gpasswd -d rachma webmaster
Create a new group on Linux
To create a new group, you can use the command
groupadd Followed by the group name:
sudo groupadd groupname
How to delete a group on Linux
To delete an existing group, use the command
groupdel Followed by the group name:
sudo groupdel groupname
How to change the main user group in Linux
To change the user’s home group, use the command
usermod Followed by the options
sudo usermod -g namagrup username
In the following example, we are changing the main user group
sudo usermod -g developers linux
How to create a new user and assign a group
The following useradd command will create a new username Bagasse With the basic group
users And the secondary group is on
wheel And the
sudo useradd -g users -G wheel,developers bagas
View Linux user groups
To display complete user information including all member groups, use the command
id Followed by your username:
If you delete the username, the command prints information about the currently registered user. Let’s check the user
uid=1000(linux) gid=100(users) groups=100(users),10(wheel),95(storage),98(power),990(libvirt),993(docker),999(kvm)
From the above output, we see the primary user group
linux is being
users And be in another group like
docker And the
groups To view additional user groups:
wheel storage power users libvirt docker kvm
Just like the requests
id If you delete the username, command
groups It will print the group of users who are currently logged in.
In this tutorial, you learned how to add users to groups. The same commands apply to any Linux distribution, including Ubuntu, CentOS, RHEL, Debian, and Linux Mint.