X
    Categories: Linux

How to: linux add user to group

This is a post about How to: linux add user to group. This post describes quick way to linux add user to group while creating user as well as for existing users.

Types of Linux group:

  1. Primary group: This is the default group used when users logs in to the system. Most of the cases its same as of the username.User always part of only single primary group.
  2. Secondary group (AKA supplementary group): This is group which is other than primary to which user can belong. A user can belong maximum 32 secondary groups.

For more details about user management commands click here:

Linux add user to group examples:

1. Primary group

  • Default primary group:

By Default when a user is created it belongs to same group.

[root@rhel1 ~]# useradd uxtecho_user

To confirm

[root@rhel1 ~]# id uxtecho_user
uid=508(uxtecho_user) gid=508(uxtecho_user) groups=508(uxtecho_user)
[root@rhel1 ~]#

In the above output by default “uxtecho_user” user belongs to primary group with same username.

Access your virtual pc in the cloud safely at CloudDesktopOnline.com and Add Office 365 on the same desktop with Apps4Rent.com – with Chrome or other browsers on your favorite PCs/Macs/Android/iOS/Linux devices.

  • Linux add user to group while creating user.

Let’s say you want to specify non default primary group while creating user. eg. You want to create user manmohan and you want to be part of sales group as a primary group.

[root@rhel1 ~]# useradd -g sales manmohan

Recheck

[root@rhel1 ~]# id manmohan
uid=510(manmohan) gid=509(sales) groups=509(sales)
[root@rhel1 ~]#

Make sure that specified group must exists on the system other wise you will end with below message from system.

useradd: group 'sales' does not exist

In case of above error create group first using groupadd command followed by useradd command.

[root@rhel1 ~]# groupadd sales
  • Linux add user to group for existing user.

In case you have already created user and later on you want to change the primary group of the user. Then use usermod command. For example you want to change primary group from sales to accounts for the user manmohan. Use below command for the same.

[root@rhel1 ~]# usermod -g accounts manmohan

Recheck:

[root@rhel1 ~]# id manmohan
uid=510(manmohan) gid=510(accounts) groups=510(accounts)
[root@rhel1 ~]#

2. Secondary group (supplementary group)

  • Linux add user to group while creating user.

You can specify secondary group while creating the user itself with the help of “-G ” option.

[root@rhel1 ~]# useradd -G sales foo

Recheck using below command:

[root@rhel1 ~]# id foo
uid=511(foo) gid=511(foo) groups=511(foo),509(sales)
[root@rhel1 ~]#

Above output confirms that sales is the secondary group whereas primary group is foo only.

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

You can specify multiple secondary group using comma while creating the user itself in useradd command.

[root@rhel1 ~]# useradd -G sales,accounts,marketing bar

Confirm using below command:

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

Also there is one more way for confirmation:

[root@rhel1 ~]# cat /etc/group|grep bar
sales:x:509:foo,bar
accounts:x:510:bar
marketing:x:512:bar
bar:x:513:
[root@rhel1 ~]#
  • Linux add user to group for existing user.

You can also change secondary group for existing users using “-G” option for usermod command. Let’s say you want to change the secondary group of the user foo from sales to accounts.

[root@rhel1 ~]# usermod -G accounts foo
[root@rhel1 ~]# id foo
uid=511(foo) gid=511(foo) groups=511(foo),510(accounts)
[root@rhel1 ~]#

In case you want to add one more secondary group instead of changing it as above then you must use “-a” (append) option.

[root@rhel1 ~]# usermod -a -G marketing foo
[root@rhel1 ~]# id foo
uid=511(foo) gid=511(foo) groups=511(foo),510(accounts),512(marketing)
[root@rhel1 ~]#
  • Linux add user to group for existing user with multiple secondary groups.

You can specify multiple secondary groups using comma for existing user with command in usermod command.

[root@rhel1 ~]# usermod -a -G sales,hr foo
[root@rhel1 ~]# id foo
uid=511(foo) gid=511(foo) groups=511(foo),509(sales),510(accounts),512(marketing),514(hr)
[root@rhel1 ~]#

In case you don’t specify -a option then current secondary groups will be flushed and replaced by specified group names.

[root@rhel1 ~]# id foo
uid=511(foo) gid=511(foo) groups=511(foo),509(sales),510(accounts),512(marketing),514(hr)
[root@rhel1 ~]# usermod -G sales,hr foo
[root@rhel1 ~]# id foo
uid=511(foo) gid=511(foo) groups=511(foo),509(sales),514(hr)
[root@rhel1 ~]#

NOTE:So make sure you use -a option incase you want to append to existing group instead of changing the existing secondary groups.

Related Post