This tutorial shows you step by step how to add a user to a group on Linux with several examples using the Linux command-line. It also explains how to add users and groups on Linux. The commands should work on any Linux Distribution and have been tested on CentOS, Debian, and Ubuntu.
Add a new Linux User to a Group
A Linux user can have one primary group and one or more secondary groups. The groups can be set as parameters of the adduser command when you create the user.
All commands have to be executed as the root user. On Ubuntu, please prepend the word "sudo " to all commands or run "sudo -s" to become root user.
Add Linux Group
As a first step, I will add a new group named "family" and a second group "friends":
groupadd family groupadd friends
Add User to Group
Then I will add a new user "tom" to our group "family". The family group will be added as a secondary group by using the -G parameter.
useradd -G family tom
Add User to two groups
Tom is now a member of the family group. The -G parameter allows it to list several groups separated by a comma. To add the user tom into the family and friends group, use this command:
useradd -G family,friends tom
Set User password
Please note that our new Linux user tom has no password yet, so he can't login. To set a password for this user, run:
And enter the new password twice when the command requests it.
In the above example, we added the user tom to a secondary group, the adduser command has created a new primary group with the name of the user automatically and assigned this group as a primary group.
- Username: tom
- Primary Group: tom
- Secondary Group: family (or family + friends if you followed the second example)
Set new primary group
Maybe you want that tom gets family as his primary group and friends as his secondary group? Then use this command instead:
useradd -g family -G friends tom
to create the user tom. the -g switch tells the useradd command to use family as the primary group. There is no group tom in this case.
Use the man (manpage) command to get a detailed description of all command-line options for useradd:
Add an existing Linux User to a Group
For this task, we will use the usermod command. Usermod allows it to modify various options of the user including the group memberships of the User.
First I will add a third group with the name colleagues.
Usermod command example
I will add the colleagues group as the secondary group to the user tom.
usermod -a -G colleagues tom
The command explained: The -a switch stands for "append", it is used in combination with the -G switch (that stands for the secondary group) only. The result is that we add tom to the group colleagues and this group is an additional or secondary group for the user.
The -G option allows it to add several groups at once by listing the group names separated by a comma. e.g.: "-G group1,group2,group3".
To change the primary group of the user tom to family, run:
usermod -g family tom
Use man (manpage) command to get a detailed description of all command-line options for usermod: