You can add a user to a group on Linux using the usermod command. To add a user to a group, specify the -a -G flags. These must be followed by the name of the group to which you want to add a user and the username of the user.
Linux groups are collections of users and are used to define a set of privileges shared by those users. You may be wondering: How do I add a user to a group in the Linux operating system?
In this guide, we are going to discuss how to add a user to a group on Linux. We’ll give you an example of how to add an existing user to a group. Also, 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 on Linux. You can set individual permissions for each user. But, this can be impractical if you are working with multiple users who should have the same privileges.
Using groups, you can specify which users can read, write, or run a specific resource on a Linux computer. For example, we could specify that only one member of the “careerkarma” group could access the “/home/careerkarma/tutorials” folder on a server.
There are two types of Linux groups
: Parent group: This is the same as your
- login name, and it is the parent group that your user is part of. Other members of a group can’t access your files on a Linux computer.
- Secondary group : Child groups, also known as supplemental groups, allow you to 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 file access permissions.
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 The usermod command adds a user to a Linux group. -a -G should be used to add an
existing user account to a group
. The syntax of the usermod command is: usermod -a -G groupname username.
Let’s analyze this syntax:
The -a flag
- tells usermod to add a user to a
- The -G flag specifies the name of the child group to which you want to add the user.
If you want to change a user’s parent group, you can use the -g flag instead. You must use the sudo command to use
This is because usermod requires sudo privileges. This makes sense because usermod directly modifies user accounts on a Linux system.
User to Group
Example Let’s say you want to add the user “careerkarma” to the group “sudo” on our computer. We could do it using this command:
This command will add “careerkarma” to the “sudo” group. You will not see any output from this command.
But if you try to access a file that only another group could access, you’ll see that your privileges have changed. In this case, now the “careerkarma” user can use “sudo” to access the 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. However, you must separate the group names to which you want to add the user. To add “careerkarma” to the “sudo” and “test” groups, we could use this command:
We have added the user “careerkarma” to our two groups. Because our user is now part of the sudo group, he can run the “sudo” command. Our user can also execute any other Linux command that requires sudo privileges.
We can also access all the files accessible to the “test” group.
Add user to Linux group
: 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, using the -g option, add the user to a group.
Suppose we want to create a new user named cktutorials and add them to the parent group “staff” and the child group “test”. We could do it using this command: We
need to add “sudo”
to the beginning of our command so that it appears as “sudo useradd…” This is because, like usermod, useradd relates to accounts in the file system. These accounts are protected by sudo.
How to check a user’s group
The id command allows you to view all the groups to which a user has access. This makes it easy to see if you have successfully added a user to a group on Linux.
Here is the
syntax of the id command
We see an output like this:
This tells us that the main group that “careerkarma” is a part of 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.
If you want to work with Linux, you need to go beyond what is coding and the basics of the C programming language. You will also need to familiarize yourself with simple Linux commands, such as adding a user to a group.
The usermod command allows you to add users to groups on Linux. If the user you want to add to a group does not already exist, you can use the useradd -g command.
For more information about the 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 markups that you can use.
For tips on top Linux learning resources, courses, and books, read our complete guide How to Learn Linux.