Linux Add User To Group

In this Linux add user to group tutorial you will gain in insight on how to add user to group on any Linux. Most users, and especially administrators, of a Linux system will eventually run into the need to do some user account management. This may include adding or deleting a user from the system, or adding a user to a group and removing a user from a group.

In this guide, we’ll show you the step by step instructions to add a user to a group on a Linux system. It’s possible to add new users to a group when the new user is being created, or you can add existing user accounts to a group. This can be done through both GUI and command line. But the command line method will work exactly the same across any Linux distribution you may be running, including Ubuntu, Debian, Red Hat, CentOS, AlmaLinux, openSUSE, etc. We’ll be covering both methods in this guide, so you can pick whichever option is most convenient for you.

In this tutorial you will learn:

  • How to add a new or existing user to a group from command line
  • How to add a user to a group from GNOME GUI

Adding a user to a group on a Linux system

Adding a user to a group on a Linux system
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Add a user to group via command line



To read all the groups available on your system, you can execute the following command in terminal.

$ cat /etc/group

Viewing all the groups available on the system

Viewing all the groups available on the system

This will also show you what users belong to which groups. As you can see in the screenshot, our user “linuxconfig” is already part of a few groups.

To see a list of all the groups that your current user is in, you can just execute the groups command.

$ groups

Show the groups that the current user is in

Show the groups that the current user is in


If you’d like to also see the group IDs, you can use the id command instead.

$ id

Show the group the current user is in, while also showing all the group IDs

Show the group the current user is in, while also showing all the group IDs

When adding a new user to the system with the useradd command, you can use the -G option to specify which groups you want the user to be a part of. Note that the groups you specify must already exist on the system (in other words, this will not create new groups). Make sure you list each group separated by a comma, with no extra whitespace in between. In this example, we’ll create a new user called “testuser” and, at the same time, add the account to the cdrom and plugdev groups.

$ sudo useradd testuser -G cdrom,plugdev

The above will add user to group on any Linux. To verify that the new user has successfully joined these groups, execute the groups command and pass the name of the new user to the command.

$ groups testuser


Creating a new user and adding it multiple groups at the same time

Creating a new user and adding it multiple groups at the same time

As you can see in the screenshot above, the user is now part of the cdrom and plugdev groups. It’s also part of the “testuser” group. Whenever a new user is created, a new group with the same name is also created, and the user is a part of it automatically.

If you want to add an existing user account to a group, that can be done with the usermod command. Check the following syntax where we add “testuser” to the “sambashare” group. Note the syntax here, which is a little tricky, because you need to specify the group name and then the user name.

$ sudo usermod -a -G sambashare testuser

And then verify the user is added…

$ groups testuser

Adding an existing user to a group

Adding an existing user to a group

To add an existing user to multiple groups at the same time, use the same syntax as above while separating each group name by a comma. Check this example where we add testuser to three more groups, and then verify that it was successful.

$ sudo usermod -a -G adm,dip,lxd testuser
$ groups testuser
testuser : testuser adm cdrom dip plugdev lxd sambashare

Adding a user to multiple groups at the same time

Adding a user to multiple groups at the same time

There are more methods of adding users to groups, such as manually editing the /etc/group file, but the examples covered above are the easiest and most recommended approaches. See the section below if you’d prefer to use GUI to add users to groups.

Add a user to a group from GNOME GUI

Adding users to a group via GUI will vary a little, depending on the desktop environment you have installed, or what Linux distro you’re running. The instructions below will show you how to add a user to a group in the GNOME desktop environment on Ubuntu. Other environments should have a similar method.

    1. GNOME’s default user manager is too limited to let us configure what groups users are in, so we need to install the gnome-system-tools package.
      $ sudo apt install gnome-system-tools
      
    2. Open the “Users and Groups” menu by searching for it in the application launcher.


Open the Users and Groups settings menu

Open the Users and Groups settings menu
    1. Highlight the user you want to edit, and then click “manage groups.”

Click the Manage Groups button

Click the Manage Groups button
    1. In this menu, you can manage the groups on the system, as well as add or remove users from groups. Click the “add” button to add a user to a group.


Add users to a group by clicking on the Add option

Add users to a group on You Linux by clicking on the Add option
    1. Enter the name of the group you want to add the user to, and then select which user(s) you want to add. Then click OK. Afterwards, you can exit the Users settings menu and your changes will take effect immediately.

Type the group name and select the user you want to add to the group

Type the group name and select the user you want to add to the group

Closing Thoughts

In this guide, we saw how to add a new or existing user account to a group via command line and GNOME GUI. We also learned how to view the groups and group ID of any user account. This is a basic task that most users will find they need to do at some point, and Linux makes the process quick and easy.



Comments and Discussions
Linux Forum