OSETC TECH

Linux/Unix:How To Change Group Membership For User Account

Changing user’s group membership to another group is a frequent things for linux Admin. So how to add a user to another group? How to change user’s primary group in operating linux system or unix system. this post will guide you how to do this.

​You can use linux command usermod to change group information for a user, the syntax is as followss:

usermod -g <primary group name> <username>

usermod -G < another group name> <username>

usermod -aG <Another group name> <username>

Linux Change Group Membership

When you create a new user using “useradd” command,  you do not need to provide a group name, system will also create a group with the same name  of user. this group is user’s primary group. if you want put this user to others group or this user belongs to multiple groups you can use “-aG” option. if you just want to change primary group, then “-g” option is a good choice.

Let’s see some examples:

1. Change the primary group of a user

changing the primary gropu of “atest” user from “atest” group to “btest”, issue the following command:

usermod -g btest atest

Output:

[root@localhost ~]# id btest

uid=504(btest) gid=504(btest) groups=504(btest) context=root:system_r:unconfined_t:SystemLow-SystemHigh

[root@localhost ~]# usermod -g btest atest

[root@localhost ~]# id atest

uid=503(atest) gid=504(btest) groups=504(btest) context=root:system_r:unconfined_t:SystemLow-SystemHigh

[root@localhost ~]#

You’ll see the above ouptut that the primary group atest of atest user has been changed from “atest” to “btest”.

2. Adding anther group to a user 

If you want to add another group to a user group membership, you just use “-G” option, such as, adding “ctest” group to user “atest”, type the following command:

usermod -G ctest atest

Output:

[root@localhost ~]# id atest

uid=503(atest) gid=504(btest) groups=504(btest) context=root:system_r:unconfined_t:SystemLow-SystemHigh

[root@localhost ~]# id ctest

uid=505(ctest) gid=505(ctest) groups=505(ctest) context=root:system_r:unconfined_t:SystemLow-SystemHigh

[root@localhost ~]# usermod -G ctest atest 

[root@localhost ~]# id atest

uid=503(atest) gid=504(btest) groups=504(btest),505(ctest) context=root:system_r:unconfined_t:SystemLow-SystemHigh

[root@localhost ~]#

You should note that if you want to add the third group to “atest” user with “-G” option,  then you will get the unexpect result,  beside the primary group, others groups were removed, just left the last group. so how do I append more groups into user,  the better way is passing “-aG” option. type the following command:

usermod -aG ctest atest

See Output:

[root@localhost ~]# id atest

uid=503(atest) gid=504(btest) groups=504(btest),505(ctest) context=root:system_r:unconfined_t:SystemLow-SystemHigh

[root@localhost ~]# usermod -G dtest atest

[root@localhost ~]# id atest

uid=503(atest) gid=504(btest) groups=504(btest),506(dtest) context=root:system_r:unconfined_t:SystemLow-SystemHigh

[root@localhost ~]# usermod -aG ctest atest

[root@localhost ~]# id atest

uid=503(atest) gid=504(btest) groups=504(btest),505(ctest),506(dtest) context=root:system_r:unconfined_t:SystemLow-SystemHigh

[root@localhost ~]#

See also: usermod man page

Done….