How to Add User to Group on Linux
Introductions
Before we begin talking about how to add user to group on Linux, let's briefly understand – What is Linux Group ?
Linux groups are an organizational unit that are used on Linux to organize and manage user accounts. The fundamental goal of groups is to define a set of privileges for a certain resource that may be shared across the users inside the group, such as reading, writing, or executing permission.
In Linux operating systems, there are two types of groups:
- The Primary group - When a user creates a file, the file's group is automatically set to that of the user. The name of the group is usually the same as the user's name. The
/etc/passwd
file contains information about the user's primary group. - Secondary or supplementary group - When you want to give a group of users certain file rights, this is a good option. If you add a user to the docker group, for example, that user will inherit the group's access rights and be able to run docker commands.
A user can only be a member of one primary group and one or more secondary groups. A user can only be added to a group by root or users with sudo access
In this tutorial, you will add user to group on Linux. We will also address a few FAQs on how to add user to group on Linux.
Advantages of Linux Group
- Access control: Linux groups enable fine-grained permissions management, like granting access to specific files or directories.
- Simplified administration: Groups streamline the process of managing user accounts, reducing administrative overhead.
- Collaboration: Group membership facilitates easy collaboration among users working on shared projects or resources.
- Security: By assigning privileges to groups, Linux enhances security by minimizing direct user access and enforcing user separation.
- Efficiency: The use of groups simplifies user management tasks, allowing for efficient and systematic organization within a Linux system.
How to Add an Existing User to a Group
Use the usermod -a -G
command, followed by the name of the group and the user, to add an existing user to a secondary group:
sudo usermod -a -G groupname username
You would run the following command to add the user vegastack
to the sudo
group, for example:
sudo usermod -a -G sudo linuxize
When adding a user to a new group, always use the -a
(append) option. If the -a
option is not used, the user will be removed from all groups that are not included after the -G
option.
The usermod
command returns no output if it is successful. It only tells you if the person or group you're looking for doesn't exist.
How to Add an Existing User to Multiple Groups in One Command
Use the usermod
command followed by the -G
option name of the group separated by ,
(commas) to add an existing user to several secondary groups in one command:
sudo usermod -a -G group1,group2 username
How to Remove a User From a Group
Use the gpasswd
command with the -d
option to delete a user from a group.
We'll remove the user's username
from the group groupname
in the following example:
sudo gpasswd -d username groupname
How to Create a Group
Use the groupadd
command followed by the group name to establish a new group:
sudo groupadd groupname
How to Delete a Group
Use the groupdel
command followed by the group name to delete an existing group:
sudo groupdel groupname
How to Change a User’s Primary Group
Use the usermod
command with the -g
option to alter a user's primary group:
sudo usermod -g groupname username
We'll change the primary group of the user vegastack
to developers
in the following example:
sudo usermod -g developers vegastack
How to Create a New User and Assign Groups in One Command
The useradd command
creates a new user named Mike
who belongs to the primary group users
and the subsidiary groups wheel and developers.
sudo useradd -g users -G wheel,developers Mike
Display User Groups
Use the id
command followed by the username to display entire user information, including all the groups to which a user belongs:
id username
The command will print information about the currently logged-in user if you don't specify a username. Let's have a look at the user vegastack
:
id vegastack
Output
uid=1000(linuxize) gid=100(users) groups=100(users),10(wheel),95(storage),98(power),990(libvirt),993(docker),999(kvm)
The user's primary group is users
, and it belongs to the wheel
, storage
, libvirt
, docker
, and kvm
supplementary groups, according to the result.
To see the user's supplemental groups, use the groups
command:
groups vegastack
Output
wheel storage power users libvirt docker kvm
If no username is specified, the groups command will output the groups
of the currently logged in user.
FAQs to Add User to Group on Linux
How can I check if a user is already a member of a group?
You can use the command id <user_name>
to see the group membership details of a user. It lists the user's primary group and supplementary groups, if any.
Do I need to restart the system for changes to take effect?
No, you don't need to restart. The changes usually take effect immediately.
How can I see a list of all the groups on my Linux system?
You can use the cat
command to view the /etc/group
file, which contains a list of all the groups on the system: cat /etc/group
.
Can I add a user to a group without using the terminal?
Yes, Linux provides graphical user interface tools like "User and Group Accounts" in system settings, where you can manage user groups by selecting the user and adding them to specific groups.
How can I check which groups a user belongs to?
Use the groups
command followed by the username. It will display a list of groups that the user is a member of. Example: groups username
.
Are there any restrictions on adding users to groups?
Administrators have the authority to add users to groups, but some systems may have restrictions in place to prevent unauthorized changes to group membership.
Are there any security implications of adding users to groups?
Adding users to groups should be done with caution. Group permissions can grant access to sensitive resources, so consider security implications and limit group membership to individuals who require such access.
Conclusion
We've taught you how to add a user to a group in this lesson.
Any Linux distribution, including Ubuntu, CentOS, RHEL, Debian, and Linux Mint, can use the same commands.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.