Linux: Add user to Group

Examples on how to add Linux users to groups with simple shell commands for 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 root user. On Ubuntu, please prepend the word "sudo " to all commands or run "sudo su" to become root user.

As a first step, I will add a new group named "family" and a second group "friends":

groupadd family
groupadd friends

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

Tom is now a member of the family group. The -G parameter allows it to list several groups separated by comma. To add the user tom into the family and friends group, use this command:

useradd -G family,friends tom

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:

passwd tom

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)

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 man (manpage) command to get a detailed description of all command line options for useradd:

man 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.

groupadd colleagues

Usermod example:

I will add the colleagues group as 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:

man usermod


1 thought on “Linux: Add user to Group”

Leave a Comment