Signup/Sign In

How To Add Users To Groups in Linux

Posted in Programming   JANUARY 6, 2022

    Add Useres

    Introduction

    In Linux, a group is a unit in which you may concurrently control numerous users' privileges. Linux groups enable you to handle multiple user rights fast and efficiently.

    What is a User Group in Linux

    In Linux, various users have distinct roles or responsibilities. Some users may require the ability to launch apps, while others are limited from accessing particular files and directories.

    Groups enable you to define categories of users with pre-set rights. Instead of handling permissions for each user account, you may add a user to a group to provide the required access.

    Primary Group

    The leading group is assigned to the logged-in user. Any files the user generates are immediately added to that group. A user may only belong to one major group at a time. A prominent group with the same name as the user is formed, and all user-generated files are included in that group.

    Secondary Groups

    A user may belong to several secondary groups (even none). Secondary groups are built to handle individual files and software programs. Members of the group inherit the read, write, and execute rights for that group.

    How to Create a User Group

    To establish a new group, input the following:

    sudo groupadd new group

    Replace the new group with the name you desire for your new group.

    How to Add User to Group

    Add an Existing User to an Existing Group

    Use the adduser command to add a user to a group:

    sudo adduser user name new group

    Use the useradd command to add a user:

    sudo useradd –G new group user name

    You may also use the usermod command to add a user to a group:

    sudo usermod –a –G group name user name

    The usermod command utilizes the –append and –group parameters to attach the user to a specific group. Without using –append, the user might be dropped from other groups.

    Add a User to Multiple Groups at Once

    Use the usermod command to specify multiple groups to add to:

    sudo usermod –a –G new group,new group2,new group3 user name

    Create a User and Add to Group

    This is handy for generating a new user on the fly for a particular software application. Enter the following:

    sudo useradd –G new group new user

    Next, assign a password to the new user:

    sudo passwd new user

    Change a Users Primary Group

    All preceding commands have been used to manage the secondary groups a user belongs to. In most circumstances, a user’s leading group is the same as their username.

    To change the leading users group, use the command:

    sudo usermod –g new group user name

    The lower-case –g denotes the leading group. (Upper-case –G refers to a secondary group.) A person can only have one leading group; hence the former primary group user name won’t be primary anymore for this user.

    How to Remove a User From a Group

    The gpasswd utility is used for managing groups. To delete a user from a group:

    sudo gpasswd –d user name new group

    Note: The gpasswd utility may also be used for other administrative activities such as creating group administrators and setting a password for access to group resources. Use the Linux man command man gpasswd for information.

    Delete a Group

    To remove a group, use the command:

    sudo groupdel new group
    

    Conclusion

    You should now have a decent grasp of Linux groups and add and delete members from such groups.

    About the author:
    Adarsh Kumar Singh is a technology writer with a passion for coding and programming. With years of experience in the technical field, he has established a reputation as a knowledgeable and insightful writer on a range of technical topics.
    Tags:ubuntuhowtolinux
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS