Home » 7 Easy Ways to Add a User to a Group on Linux

7 Easy Ways to Add a User to a Group on Linux

Let’s be honest, Linux is tough! Even the smallest tasks require so much effort. If you’re a hardcore Linux user and you plan to stay the same for years to come, you need to have a complete understanding of the platform. If you’re reading this blog, then your current problem is finding how to add users to a group on Linux. 

Changing the group a user is assigned to is pretty easy. If you don’t know how to, just keep reading. 

User accounts can be assigned to one or more groups on Linux. You can configure file permissions and other types of access by group. 

If you’re having trouble in adding a user to a new group or a secondary group on Linux, then all you need to do is write a few lines of code. Don’t worry, we’ll share the code with you.

Let’s get started. 

How to Add a User to a Group in Linux

There are 7 techniques we’ll be teaching you that you can use to add a user to a group in Linux. 

1. Add a New Group

Create a new group on your system and then add the user into the group. Use the command “groupadd” and replace “new_group” with the name of the group you want to create. 

To run this command, you’ll need to  use sudo as well. If you’re using Linux Distributions that don’t use sudo, you’ll need to run the “su” command to get the permissions required before running the command. 

The final command will look something like this:

Sudo groupadd mynewgroup

2. Add an Existing User Account to a Group

If you want to add an existing user to a group, you can run this command on your device. Use the usermod command and replace the examplegroup with the name of the group. Here’s how the command will look like:

Usermod -a -G examplegroup exampleusername

If you want to add the user Chris to the Group red, use the following command. 

Usermod -a -G red chris

3. Change The User’s Primary Group

A user account can be part of multiple groups, but there’s always a “primary group”. A user’s login process and files and folders are all stored in the primary group.

To add a user to other group, you can change the user’s primary group. To change a user’s primary group, you’ll need to run the “usermod”  command. When you run the command, replace “examplegroup” with the name of the group you want to be the primary one. Also, replace the “exampleusername” with the name of the account user.

Here’s how the code will come out to be:

“usermod -g groupname username”

Do keep in mind that we’ve used the lowercase -g. This is used when you assign a primary group. When you use an uppercase -G, you assign a new secondary group.

4. View the Groups a User Account is Assigned To

You can view all the groups the current user account is assigned to and then you can change the group. Run the groups command. You’ll see a list of groups. 

If you want to see all the numerical IDs associated with each group, run the ‘id’ command instead.

To view the groups that a user is assigned to, run the ‘groups’ command and specify the name of the user account. Here’s what the command looks like “groups exampleusername”.

You can view the numerical IDs associated with each group by running the id command and specifying a username. Here’s how the command will look like “id exampleusername”.

The first group in the groups list or shown after “gid=” in the id list is the user account’s primary group. The other groups are secondary groups. 

5. Create New User and Assign a Group

You could also create a new user account that has access to a specific resource or directory, like a new FTP user. You can choose the group the user will be assigned to when creating the user account. 

Use this command to do so, “useradd -G examplegroup exampleusername”.

For example, to create a new user account named chris, and assigning the account to the FTP group, you’d have to run the command “useradd -G ftp chris”.

And ofcourse, you’ll have to aassign a password to the user afterwards. To do so, we’ll have to run a command, such as “passwd chris”.

6. Add a User to Multiple Groups

While assigning the secondary groups to user account, you can assign a number of groups at once. How? You can just separate the groups with a comma.

Take a look at this code: “usermod -a -G group1,group2,group3 exampleusername.”

Let’s say you want to add a user named Chris to groups red, blue, and green, you’d run this code – “usermod -a -G ftp,sudo,example geek”.

You can add as many groups as you want, just separate the groups with a comma.

7. View All Groups on the System

If you want to check all the groups on the system, you can use the “getent” command. The command looks like: “getent group”.

This command will also give you a bird’s eye view of which user accounts are members of which groups.

Back to top