Change Primary group of a user in Ubuntu

Published: October 09, 2022

Change Primary group of a user in Ubuntu

There are two types of groups in Ubuntu -

  1. Primary group and,
  2. Secondary group.

An Ubuntu user would always have a Primary group. Apart from that, if needed, he/she can be a part of one or more than one Secondary groups. Furthermore, a user without a primary group isn't allowed to login.

It is worth mentioning here that, once we change the primary group of a user. The group ownership of all the files in the user's home directory are modified on its own. But, if a user has some files outside of home directory then, the group ownership of such files or directories won't change and the user himself/herself has to make necessary changes. Follow the article - Change Group ID of a file or directory in Ubuntu to check how it can be done.

A primary group is created with the same name as that of user. While we add a user in Ubuntu, in the background the system creates a primary group for the user alongside. To check for a user's primary and secondary group, use id command-line utility -

id <user_name>

It would show us all the groups a user is part of. Next, we cover how to change a user's primary group.

Note: Following operations require superuser privileges. In case you don't have one then, we advise you to contact your System Administrator for assistance.

Change Primary group of a user in Ubuntu

So, to change a user's primary group we use usermod command-line utility. Follow the syntax -

usermod -g <new_group_name> <username>

or, we can also use Group's ID as well -

usermod --gid <new_group_id> <username>

One thing we should keep in mind that, it is necessary for a group to exist before we can assign it to a user. Follow the article - Create a group in Ubuntu to know more.

For instance, if we have a user - testuser and would like to switch it to new group - newgroup. Then, we can use -

usermod -g newgroup testuser

To verify the changes, use -

id <user_name>

In conclusion, we have covered how to change primary group of a user in Ubuntu.