How to add a user to a group on Linux

This is a post on how to add a user to a group in Linux. We will describe a quick way to add a user to a group when creating a user, as well as for existing users.

Linux group types:

  1. Primary group: This is the default group when users log on to the system. In most cases, use it as the username. A user is always part of only one main group.
  2. Secondary group (additional group AKA): This is a group, which is different from the primary, to which the user can belong. A user can belong to a maximum of 32 secondary groups.

For more information on managing command users, click here:

An example of adding a user to a group in Linux OS:

1. Primary group

  • By default, the main group is:

By default, when a user is created, it belongs to the same group.

[[email protected] ~]# useradd andreyex_user

Confirm

[[email protected] ~]# id andreyex_user
uid=508(andreyex_user) gid=508(andreyex_user) groups=508(andreyex_user)
[[email protected] ~]#

In the above output, by default the user β€œgrayex_user” belongs to the main group with the same name.

  • Linux OS add user to group when creating user.

Let’s say you want to specify a non-default primary group when creating a user. For example: You want to create a user masterkey and you want to be part of the sales group as the main group.

[[email protected] ~]# useradd masterkey -g sales

Double check

[[email protected] ~]# id masterkey
uid=510(masterkey) gid=509(sales) groups=509(sales)
[[email protected] ~]#

Make sure the specified group must exist in the system, otherwise you will see below messages from the system.

useradd: group 'sales' does not exist

In case of error above, create the group first with the groupadd command before the useradd command.

[[email protected] ~]# groupadd sales

  • Linux OS add user to group for existing user.

In case you have already created a user and in the future you want to change the primary group of the user. Then use the usermod command. For example, you want to change the primary group sales to accounts for the user masterkey. Use the following command for this.

[[email protected] ~]# usermod -g accounts masterkey

Double-check:

[[email protected] ~]# id masterkey
uid=510(masterkey) gid=510(accounts) groups=510(accounts)
[[email protected] ~]#

2. Secondary group (additional group)

  • Add a user to a group when creating a user in Linux OS.

You can specify a secondary group when creating a user directly with the -g option.

[[email protected] ~]# useradd -G sales delphi

Double-check with the following command:

[[email protected] ~]# id delphi
uid=511(delphi) gid=511(delphi) groups=511(delphi),509(sales)
[[email protected] ~]#

The above output confirms that sales is a secondary group while the primary group is delphi.

  • Linux add user to group when creating user with multiple secondary groups.

Multiple secondary groups can be specified using commas when creating the user itself in the useradd command.

[[email protected] ~]# useradd -G sales,accounts,marketing bar

Confirm with the following command:

[[email protected] ~]# id bar
uid=512(bar) gid=513(bar) groups=513(bar),509(sales),510(accounts),512(marketing)
[[email protected] ~]#

In addition, there is another way to confirm:

[[email protected] ~]# cat /etc/group|grep bar
sales:x:509:delphi,bar
accounts:x:510:bar
marketing:x:512:bar
bar:x:513:
[[email protected] ~]#

  • Add a user to a group for an existing user on Linux.

You can also change the secondary group for existing users using the -g option to the usermod command. Let’s say you want to change the secondary group of the delphi user to sales.

[[email protected] ~]# usermod -G accounts delphi
[[email protected] ~]# id delphi
uid=511(delphi) gid=511(delphi) groups=511(delphi),510(accounts)
[[email protected] ~]#

In case you want to add another secondary group instead of changing it as described above, then you must use the β€œ-a” (add) option.

[[email protected] ~]# usermod -a -G marketing delphi
[[email protected] ~]# id delphi
uid=511(delphi) gid=511(delphi) groups=511(delphi),510(accounts),512(marketing)
[[email protected] ~]#

  • Add user to group for existing user with multiple secondary groups in Linux OS.

Multiple secondary groups can be specified using a comma for an existing user with the usermod command.

[[email protected] ~]# usermod -a -G sales,hr delphi
[[email protected] ~]# id delphi
uid=511(delphi) gid=511(delphi) groups=511(delphi),509(sales),510(accounts),512(marketing),514(hr)
[[email protected] ~]#

If you did not specify the -a option, the current secondary groups will be dropped and replaced with the specified group names.

[[email protected] ~]# id delphi
uid=511(delphi) gid=511(delphi) groups=511(delphi),509(sales),510(accounts),512(marketing),514(hr)
[[email protected] ~]# usermod -G sales,hr delphi
[[email protected] ~]# id delphi
uid=511(delphi) gid=511(delphi) groups=511(delphi),509(sales),514(hr)
[[email protected] ~]#

NOTE: Therefore, make sure you use the -a option if you want to add to an existing group instead of modifying existing secondary groups.

Sidebar