You can add a user to a group in Linux using the usermod command. To add a user to a group, specify the -a -G flags. These should be followed by the name of the group to which you want to add a user and the user’s username.
Linux groups are collections of users and are used to define a set of privileges those users share. You may be asking yourself: How can I add a user to a group on the Linux operating system?
In this guide, we’re going to discuss how to add a user to a group in Linux. We’ll give you an example of how to add an existing user to a group. In addition, we’ll talk about how to add a new user to a group.
What is a Linux Group?
Linux groups help developers manage user accounts in Linux. You can set individual permissions for each user. But, this can be impractical if you’re working with multiple users who should all have the same privileges.
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
Using groups, you can specify which users can read, write or execute a specific resource on a Linux computer. For instance, we could specify that only a member of the “careerkarma” group could access the “/home/careerkarma/tutorials” folder on a server.
There are two types of Linux groups:
- Primary group: This is the same as your login name and is the main group of which your user is a part. Your files cannot be accessed by other members of a group on a Linux computer.
- Secondary group: Secondary groups, also known as supplementary groups, let you share access to files.
To add a user to a group, you’ll need to use the Linux sudo command. This is because adding users to a group modifies their access permissions to files.
Now that we know the basics of groups on Linux systems, let’s dive into how to add users to a group.
How to Add a User to a Group Linux
The usermod command adds a user to a Linux group. -a -G flags should be used to add an existing user account to a group. The syntax for the usermod command is: usermod -a -G groupname username.
sudo usermod -a -G group_to_add username
Let’s break down this syntax:
- The -a flag tells usermod to add a user to a group.
- The -G flag specifies the name of the secondary group to which you want to add the user.
If you want to change a user’s primary group, you can use the -g flag instead. You need to use the sudo command to use usermod:
sudo usermod ...
This is because usermod requires sudo privileges. This makes sense because usermod directly modifies user accounts on a Linux system.
Linux: Add User to Group Example
Let’s say you want to add the user “careerkarma” to the “sudo” group on our computer. We could do so using this command:
sudo usermod -a -G sudo careerkarma
This command will add “careerkarma” to the “sudo” group. You won’t see any output from this command.
But if you try to access a file that was only accessible to another group, you’ll see that your privileges have changed. In this case, now the “careerkarma” user can use “sudo” to access files because it has been added to the “sudo” group.
If you want to add a user to multiple groups, you can use the same command as above. But, you should separate the group names to which you want to add the user. To add “careerkarma” to both the “sudo” and “test” groups, we could use this command:
sudo usermod -a -G sudo,test careerkarma
We have added the “careerkarma” user to our two groups. Because our user is now part of the sudo group, they can execute the “sudo” command. Our user can also execute any other Linux command that require sudo privileges.
We can also access all the files accessible to the “test” group.
Add User to Group Linux: New User Example
There may be a case where you want to create a new user and immediately add them to a group. That’s where the useradd command comes in. The useradd command allows you to create a new user and by also using the -g option, add the user to a group.
Suppose we want to create a new user called cktutorials and add them to the primary group “staff” and secondary group “test”. We could do so using this command:
sudo useradd -g staff -G test cktutorials
We need to add “sudo” to the start of our command so it appears as “sudo useradd …” This is because, like usermod, useradd relates to accounts on the file system. These accounts are protected by sudo.
How to Check a User’s Group
The id command gives you the ability to see all the groups to which a user has access. This makes it easy to see whether you have successfully added a user to a group in Linux.
Here’s the syntax for the id command:
We see an output like this:
uid=501(careerkarma) gid=20(staff) groups=20(staff) ...
This tells us that the primary group of which “careerkarma” is a part is “staff”. We have shortened this output for brevity because this command can return a long list of groups, depending on how your system is configured.
The usermod command allows you to add users to groups in Linux. If the user you want to add to a group does not already exist, you can use the useradd -g command.
To learn more about usermod and useradd commands, run the man command in your terminal, followed by the command name. This will allow you to see the user manual for that command, which will provide you with more examples and flags you can use.
For advice on top Linux learning resources, courses, and books, read our complete How to Learn Linux guide.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.