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
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the |
|Conventions|| # - requires given linux commands to be executed with root privileges either directly as a root user or by use of |
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
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
If you'd like to also see the group IDs, you can use the
id command instead.
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
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
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
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
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.
- GNOME's default user manager is too limited to let us configure what groups users are in, so we need to install the
$ sudo apt install gnome-system-tools
- Open the "Users and Groups" menu by searching for it in the application launcher.
- Highlight the user you want to edit, and then click "manage groups."
- 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.
- 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.
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.