How to Add User to Group in Linux

Table of Contents
Add User to Group in Linux
You can add a Linux user to one or multiple Groups in Linux. In this tutorial, you are going to learn how to add user to group in Linux and perform create group, delete group, list group like operations. You can use following commands in Ubuntu, Debian, Linux Mint CentOS, Fedora and RHEL Linux etc.
There are two types of groups in Linux and they are:
-
- Primary Groups – Mostly the name of the user is same as primary group. As user creates a file it will be assigned to primary group of the user
- Secondary Groups – If you want to assign certain permissions to groups of users then you can use secondary groups. Secondary Group is also known as Supplimentary Group
Prerequisites
Before you start installing Postman in Linux. You must be logged in with a non-root user account on your server with sudo privileges.
How to Display User Groups:
To display a specific users all the groups you can use id
command or groups
command.
To display linux4one
user’s all the groups with id
command, you can type:
id linux4one
The output should be:

From above output you can see that linux4one user belongs to linux4one, wheel, vboxusers groups.
To display linux4one
user’s all the groups with groups
command, you can type:
groups linux4one
The output should be:

From above output you can see that linux4one user belongs to linux4one, wheel, vboxusers groups.
How to Add or Delete Group
You can create a new group by using groupadd
command. To create a group named demogroup
you should run following command:
sudo groupadd demogroup
To delete existing group you should run groupdel
command followed by group. For example to remove demogroup
you can type following command in the terminal:
sudo groupdel demogroup
How to Add User to Group
To add user to group you should use usermod command. When you use -a
(–apend) option with usermod command the user that you are adding to group will be appended to existing members. When you use -g
option with usermod command user will be added to primary group
and if you use -G
option with usermod command then the user will be added to secondary group
.
To add an existing user to the primary group, you should run the following command:
sudo usermod -a -G GROUPNAME USERNAME
For example to add linux4one
user to sudo
group you should use following command:
sudo usermod -a -G sudo linux4one
If you are using Fedora or CentOS Linux and want to add linux4one
user to sudo
group you should run following command:
sudo usermod -a -G wheel linux4one
To add user linux4one
to demogroup run below command:
sudo usermod -a -G demogroup linux4one
Confirm user is added to group by using following command:
id linux4one

How to Remove User From a Group
The gpasswd
command will be used to remove the user from the group with -d
option.
To remove user USERNAME from group GROUPNAME you should run the following command:
sudo gpasswd -d USERNAME GROUPNAME
For example, to remove user john
from group ftp
you should run following command:
sudo gpasswd -d linux4one demogroup
Confirm user is removed from group by using following command:
id linux4one

How to Add User to Multiple Secondary Groups
To add user to multiple groups you should use usermod
command with -G
option which is used to add user to secondary group.
The basic syntax for adding a user to multiple groups is as given below:
sudo usermod -a -G GROUPNAME1,GROUPNAME2,GROUPNAME3 USERNAME
For example to add user linux4one
to groups ftp and www, you should run below command in the terminal:
sudo usermod -a -G www,ftp linux4one
How to Change Users Primary Group
For changing users primary group you should run usermod
command with -g
option.
sudo usermod -g GROUPNAME USERNAME
To change john
user’s primary group to www
you can run below command in the terminal:
sudo usermod -g www john
How to Create User and Add it to Groups Same Time
You can create a user with useradd command followed by -g
option and add it to groups with -G options like given below:
sudo useradd -g PRIMARY_GROUP -G SECONDARY_GROUP1,SECONDARY_GROUP2 NEW_USERNAME
Conclusion
You have successfully learned how to add user to group in Linux. If you have any queries regarding this then please don’t forget to comment below.
LATEST POSTS
-
How to Install FFmpeg on Ubuntu 18.04
-
How to Install Atom Editor on Ubuntu
-
How to Install and Use Docker on Debian 9
-
How to Install WordPress with LEMP Stack on Ubuntu 18.04
-
How to Install PHP on Ubuntu 18.04
-
How to install Java on Ubuntu 18.04
-
How to Install Microsoft PowerShell 6.2.0 on Debian 10
-
How to install Dropbox on Ubuntu 18.04 Server and Desktop
-
How to Install XAMPP on CentOS 8
-
11 Wget Command Examples in Linux (Linux File Downloader)
-
How to Install Google Chrome on Ubuntu 19.04
-
How to Install Google Earth on Debian 10