Ubuntu: How to assign one or more groups to a user (via Shell/SSH)
Marc Wagner
October 20, 2022
To edit an existing user, use the usermod command on Linux.
Adding a group to a user #
The following command will help you to add a group to a user:
sudo usermod -a -G Group_Name Username
Adding multiple groups to one user at once #
If you want to add multiple groups to a user, you just need to extend the example above by multiple groups. This way you can add multiple groups to a user by using a comma separated list. Here is an example of how the whole thing is structured:
sudo usermod -a -G Group_Name,Group_Name_2,Group_Name_3 Username
Parameter | Description |
-a | Stands for “append”. You should specify this, otherwise you will remove all other groups of the user. |
-G | Stands for “groups”. Here you can specify all the groups that will be added to the user, separated by a comma. Important: without “-a” (append) all groups you don’t add here will be removed from the user. |
Display groups of a user #
If you are not sure which groups already exist for the desired user, you can simply display them. To do this, use the command “groups”. The following example shows you how to display all groups of a user:
groups Username
List all users in Linux #
In case you do not know the username, you can simply display all users. The users and their groups are stored in the file /etc/shadow. With the help of “cat” you can easily display them, as the following example shows:
sudo cat /etc/shadow
Artikel von:
Marc Wagner
Hi Marc here. I’m the founder of Forge12 Interactive and have been passionate about building websites, online stores, applications and SaaS solutions for businesses for over 20 years. Before founding the company, I already worked in publicly listed companies and acquired all kinds of knowledge. Now I want to pass this knowledge on to my customers.