Linux groups are collections of users and are used to define a set of privileges those users share. When you’re working with a group in Linux, you may be asking yourself: How can I add a user to a group in Linux?
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 and how to add a new user to a group so you’ll have everything you need to get started.
What Are Linux Groups?
Linux groups help developers manage user accounts in Linux. While you can set individual permissions for each user, this can be impractical if you’re working with multiple users who should all have the same privileges.
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, give you the ability to share access to files and share file permissions to those files.
To add a user to a group, you’ll need to use the 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 Linux Group
Adding a user to a group in Linux is accomplished using the
usermod command. This command allows you to modify a Linux user. Specifically, we’re going to use the
The syntax for adding a user to a group is:
sudo usermod -a -G group_to_add username
-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.
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 and 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
How to Create a New User and Add Them to a Group
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
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 or not you have successfully added a user to a group in Linux.
Here’s the syntax for the id command:
If we run this command on our “careerkarma” user, we may 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.
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
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.