How To Add User to Group in Linux

In summary, “usermod” command can add user to a group in Linux. To add a user to a secondary group run “sudo usermod -aG group-name username“. To change the primary group of a user run “sudo usermod -g group-name username


Adding users to groups in Linux is a fundamental task for system administrators and anyone managing a Linux-based system. Groups provide a way to organize and manage user permissions efficiently.

In this tutorial, we will learn the process of adding a user to a group in Linux using the command-line interface (CLI). We’ll cover adding users to both primary and secondary groups using the usermod command, providing you with the necessary knowledge to manage user access effectively.

How To Add User to Group in Linux

Groups in Linux

When a user belongs to a particular group, they inherit the permissions and rights that are assigned to that group.

For instance, members of the sudo group are allowed to run commands with root privileges by putting sudo before the command.

There are two types of group membership in Linux:

  • Primary group: When a user is created, they are assigned to a primary group. This defines their main group affiliation.
  • Secondary groups: A user can also belong to one or more secondary or supplementary groups in addition to their primary group.

Adding a user to supplementary groups gives them the permissions of those groups while their primary group remains unchanged.

Add User to Group in Linux

Adding a User to a Primary Group in Linux

  1. Check Existing Groups:
    Before adding a user to a group, it’s essential to know which groups are available on your system. You can list them using the getent or cat command, depending on your preference.
   getent group
   # OR
   cat /etc/group
  1. Add a User to a Primary Group:
    To add a user to a primary group, you’ll use the usermod command with the -g option followed by the group name.
   sudo usermod -g group_name username

Replace group_name with the name of the group you want to add the user to and username with the user’s name.

For example:

   sudo usermod -g developers storagedev
  1. Verify Changes:
    You can verify the user’s primary group membership using the id command:
id storagedev
uid=1001(storagedev) gid=3002(developers) groups=3002(developers)

It should display the primary group as the one you specified.

Adding a User to a Secondary Group in Linux

  1. Add a User to a Secondary Group:
    To add a user to a secondary group, use the usermod command with the -aG option followed by the group name.
   sudo usermod -aG group_name username

Again, replace group_name with the name of the group and username with the user’s name.

For example:

   sudo usermod -aG sudo storagedev
  1. Verify Changes:
    To verify the user’s secondary group memberships, you can use the groups command:
id storagedev
uid=1001(storagedev) gid=3002(developers) groups=3002(developers),27(sudo)

It will display a list of groups to which the user belongs, including the newly added secondary group.

You can add a user to multiple secondary groups by providing a comma-separated list of groups to –aG.

Conclusion


Managing user groups in Linux is crucial for effective access control and system administration. With the usermod command, you can easily add users to both primary and secondary groups, granting them the necessary permissions to interact with files, directories, and resources on your Linux system.

Buy me a coffeeBuy me a coffee

Add Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.