Overview
The following post will demonstrate the steps how to create groups in Linux as well as how to delete groups in Linux and how to add user to the group and how to remove a user from the group.
This is a continuation of our Linux user management post series. In one of our earlier posts, we covered the process how to create and delete users in Linux via terminal and how to add sudo privileges to the user(you can check out these procedures here - add sudo privileges, create a user).
Now in this post, we’re going to showcase the procedure how to create and delete groups in Linux via terminal. This is also another essential skills a Linux user should know since this procedure is a part of user management practice.
We’ll also cover how to add and remove users to/from these groups we created. These actions do require to be logged in the Linux system either as a user with sudo privileges or as a root user.
We’ll be using commands “groupadd” and “groupdel” to perform the actions mentioned here.
Why is it important to do group management?
Having groups allow system administrators to apply certain permissions or access to groups of users, thus creating user group segregation. This practice will enable us having more control of our Linux system, especially on a production system where we want to a secure environment but still allow some users to have some access to the system.
How to create groups in Linux
Command that you will use for this is:
groupadd –g groupdID groupUsername
-g groupID
The “-g groupID” parameter indicates that a group number will follow(will be assigned to the group and act as a ID number). This is the group number that will be assigned to this new group. The group number must be unique.
groupUsername
The name of the group you would like to add. Group names should be entered in lowercase and may contain underscores. It is recommended that you do not use the same group name more than once.
We will now create a group with its id number and a name with a command:
sudo groupadd –g 15000 testgroup
Once you have done that just hit Enter and the group should be created, if there is no errors that means that everything Is fine and the group is created. Since new groups are added to the end of the system group file called “/etc/group” you can use tail command to check if your group is created. Just type “sudo tail /etc/group” after the system prompt to show the last few lines of the system group file.
sudo tail /etc/group
As you can see group has been created.
How to delete groups in Linux
To remove a group we will use a command “groupdel” and type the name of the group you want to remove.
sudo groupdel groupname
And your group should be removed. To check if the group was removed you can use tail command like we did for checking if you group was created, but this time only your group will not be on the group list.
sudo tail /etc/group
Add user to the group
To add an existing user to a secondary group, use the usermod –a –G groupname username command followed the name of the group and the user.
sudo usermod -a -G groupname username
There is no information that your command was successful, usermod command does not display any output. It warns you only if the user or group doesn’t exist. Always use the –a (append) option when adding a user to a new group. If you don’t, user will be removed from any other group, and only will be in the group you just added him.
How to add a user to multiple groups with one command
It is the same procedure as adding user to only one group, but this time you have to name more groups in just one command, for example:
sudo usermod –a –G groupname1,groupname2,groupname3 username
How to remove user from the group
Now we will show you how to remove a user from a group. For this you are going to use command gpasswd with the –d option.
sudo gpasswd –d username groupname
This time command will tell you that user was removed from the group, like shown in picture
And now you have successfully removed a user from a group.
Summary
To summarize what we have covered in this post. We went through the steps how to create groups in Linux, how to delete user group in Linux and how to add a user to the group and how to remove a user from the group. All these commands shown in today’s article can also apply for any Linux distribution, including Ubuntu, CentOSn, RHEL, Debian, Fedora, and Arch Linux.
Thank you for your time.