This article describes the steps to add users to groups in Ubuntu Linux.
A group is a container that can be used to organize or administer user accounts. Accounts in the same group will access and manage resources belonging to that group.
Every account on a Ubuntu Linux system belongs to one or more groups. There are two types of groups: Primary and secondary groups.
When you create a new account, a primary group similar to the account name will also be created. The account will automatically be added to the primary group. Resources (files, folders) created by the account will, by default, be assigned permissions of the primary group.
An account can be a member of both the primary and secondary groups. The secondary group helps assign permissions to accounts and services of other groups.
Below is how to add a user to a group in Ubuntu Linux.
Add a user to a group in Ubuntu Linux
As described above, you can create a group to restrict users’ access to resources. For example, if you add a user to the docker group, the user will inherit the group’s access rights and be able to run docker commands.
Below is a format to add a user to a group in Ubuntu Linux.
sudo usermod -a -G groupname username
Add a user to multiple groups.
sudo usermod -a -G group1,group2 username
The commands above assume that the groups you want to add a user already exist.
For example, to add the user geekrewind to the sudo group, you would run the following command:
sudo usermod -a -G sudo geekrewind
When adding a user to a new group, you use the -a (append) command. If the group doe not exists, the command will return an error that the group does not exist.
Change a user’s primary group in Ubuntu Linux
As stated above, whatever resources a user creates in Ubuntu Linux will assume the rights and access the user’s default group. This will allow the users to have access to all the resources created.
In Ubuntu Linux, you can change a user’s primary group. To change a user’s primary group, use the usermod command followed by the -g command option.
sudo usermod -g groupname username
For example, you use the commands below to change the primary group of the user geekrewind to developers.
sudo usermod -g developers geekrewind
You can specify a user’s primary and secondary groups when creating a user account. For example, use the below to create a new account named geekrewind and set its primary group users and secondary groups to wheel and developers.
sudo useradd -g users -G wheel,developers geekrewind
To list all groups in Ubuntu Linux, read the post below.
How to list groups in Ubuntu Linux
That should do it!
Conclusion:
This post showed steps to add a user to a group in Ubuntu Linux. Please use the comment form below if you find any errors above or have something to add.