Groups in Linux act as organization blocks that are used to sort and govern Linux user accounts. Groups can be used to set privileges on users, privileges like running applications, reading, or writing in directories.
There are two types of groups namely, Primary groups and Secondary groups. Primary groups are created when a Linux user creates a new file. Typically the group name is similar to the user who created the file. However, Secondary groups are used when you need to give a group of users specific permission on a file. Every Linux user must belong to only one primary group and none or more than one secondary group.
After following the guide, you shall be able to create a group and assign users to it. We shall also provide you some tips on Linux groups.
This tutorial was tested on Ubuntu, but the commands should work on all Linux distributions.
Adding Users to a Group in Linux
Linux Groups examples on Ubuntu
Example 1. To add a new group.
sudo groupadd fosslinuxgrp
Example 2. To add an existing user to an existing group.
sudo usermod -a -G fosslinuxgrp hendadel
Example 3. To change the primary group for an existing user.
sudo usermod -g fosslinuxgrp hendadel
Note that the “-g” option is to assign a primary group while the “-G” option from the previous example s to assign a secondary group.
Example 4. List all the assigned groups to the current logged in user.
Example 5. To display all the assigned groups for a specific user.
Example 6. To create a new user and assign an existing group to it.
sudo useradd -G fosslinuxgrp celine
This command creates a new user called “celine” and assigns it to an already existing group, which is “fosslinuxgrp.”
But do not forget to assign a password to the newly created user using the below command:
sudo passwd celine
Example 7. Add an existing user to multiple groups.
sudo usermod -a -G fosslinuxgrp,backup celine
Example 8. To display all the groups that are found on the Linux system, you can use the command as follows:
That’s all about adding users to groups on Linux. I hope you enjoyed it.