Skip to main content

User Management

Add User to a Group in Ubuntu

Here's how you can assign a new group to an existing user or change its primary user group in Ubuntu. Also learn about creating new users with given groups.

If you know the user name and the group name, you can add the user to the desired group in Ubuntu like this:

sudo adduser username groupname

That's the simplest and safest way of adding an existing user to an existing group.

The adduser is not a standard Linux command. While it comes preinstalled in Ubuntu, it may not be available in all the Linux distributions.

The usual Linux way of adding a user to a certain groups involves using the usermod command.

sudo usermod -a -G group_name username

Clearly, you need to be root or have sudo access for this administrative task.

That -a option is crucial here. It stands for 'append' and this way you add the user to a new group while keeping it in other groups as well. Without the -a option, you'll overwrite the group settings and the user will belong to only the specified new group which could be catastrophic.

Quickly verify if the user was correctly added to the desired group by checking its groups:

groups user_name
Adding user to a group in Ubuntu

That was quick and simple if you are a seasoned Linux user. But if you are new to it, let's see the steps for adding a user to a group in a bit more detail.

I'll go over the following in this tutorial:

  • Steps for adding a user account to a group
  • Creating new users with certain groups
  • Changing the primary group of a user

Adding a user to a group in Ubuntu

As a sysadmin, you might need to know a few details before you add a user to a group. You won't always have all the details at hand and you may have to search for them.

The entire process consists of the following steps:

  • Get the exact user name you are modifying and adding to a group
  • List the group it belongs to and the group you want to add it to
  • Add the user to the desired group
  • Verify that the user has been added to the group

Step 1: Get the user name

If you are not sure of the exact username, you can list all the users on your Ubuntu system.

compgen -u

Normally, the regular users (non-system users) are at the bottom of the long output of the compgen command:

root@learnubuntu:~# compgen -u

I'll be changing the group of the user abhi and add it to sudoer list by adding it to the sudo group.

Once you know the user name, it's time to find the details about the group.

Step 2: Get the groups details

First, check the groups the user belongs to:

groups username

Here's an example. As you can see the user abhi belongs to the group abhi. That's the Ubuntu way of creating the primary group with the same name as the user name.

root@learnubuntu:~# groups abhi
abhi : abhi

If needed, you can see all the user groups present on your Ubuntu system:

getent group

You can see all the groups and get the exact group name from the output. The output is several lines long, by the way.

root@learnubuntu:~# getent group

If the group doesn't exist, you can create a new group like this:

sudo groupadd new_group_name

Now, you have the user name and the group information. Time to add the user to the group.

Step 3: Add user to the group

For this, I recommend using the adduser command on Ubuntu:

sudo adduser username groupname

If it runs successfully, you should see an output like this:

root@learnubuntu:~# adduser abhi admin
Adding user `abhi' to group `admin' ...
Adding user abhi to group admin

Alternatively, you can use the usermod command. So, you'll be modifying the user's properties and adding it to an additional group.

Since you are adding it to an additional group, you should use the append mode with option -a:

sudo usermod -a -G group_name username

Without the option -a, you'll remove the user from existing groups and add it only to this new group. This is not something you would want in most cases. And hence, the append option here.

If things go right, you won't see any output. Yet, it is better to confirm things.

Step 4: Verify that the user has been added to the new group

The process is complete. You just have to verify that things went right.

Check the groups of the user and see if it belongs to the desired groups or not.

groups username
Adding user to a group in Ubuntu

In case you missed out on the append option and messed up a user's groups, you can still utilize the output you saw in the second step and add the user to all the groups it belonged to previously.

How to Change User in Ubuntu Command Line
Learn how to change to another user in Ubuntu, along with a few useful tips on switching users.

Creating new users with a certain group in Ubuntu

When you create a new user in Ubuntu, a new group is created with the same name as the user. This is the default or primary group of the user.

However, if you want to create a user with a certain group, you can do it easily with the useradd command:

useradd -G group_name username

Say, you have to create a new FTP user whose sole point is to access the files via FTP.

useradd -G ftp new_ftp_user

Changing the primary group of a user

When you create a new user in Ubuntu, it is assigned to a primary group by the same name as the user. When this new user creates a file, the file's group ownership belongs to the primary group.

If you want to change the primary group of the user for some reason, you can do that.

First, display the primary group of a user with the id command:

id -gn username

Now to change the primary group, use the option -g this time:

usermod -g group_name username
Learn Linux Quickly - Linux Commands for Beginners
Learn Linux Quickly doesn’t assume any prior Linux knowledge, which makes it a perfect fit for beginners. Nevertheless, intermediate and advanced Linux users will still find this book very useful as it goes through a wide range of topics. Learn Linux Quickly will teach you the following topics:Insta…

What next?

Well, that's all you need to know about changing the group of a user in Ubuntu command line.

Next, you may learn about removing users from groups in Ubuntu.

I highly recommend reading about file permission and ownership in Linux along with user management commands, if you want to learn more on this topic.

As always, feel free to leave a comment with your questions and suggestions.