How To Add Users To A Group In Linux

INTRODUCTION TO MANAGING USERS AND GROUPS IN LINUX


Are you self-managing user access to your Linux VPS? As the server administrator, what access management approach are you following? You can easily add members to the Linux group to reduce your manual efforts. When you assign directory logging access to a particular group, the members automatically inherit the permission rights! That’s why you should add a user to a Linux group! But how to do that? Are there any accessible Linux commands that you can run? Let’s confirm what scripts you can use!



UNDERSTANDING KEY FILES IN LINUX USER AND GROUP MANAGEMENT


Linux uses specific directory files to store user and group information. These files contain user account information and their passwords alongside pre-defined configurations of user groups. You can interact with these files to modify user access and confirm the group settings!

The /etc/passwd File


The /etc/passwd file is mainly used to manage user accounts. It contains essential information about each user and their home directory in a colon-separated format. Each line in this file represents a single user account and consists of several fields, such as the user name, password, etc.

Example: root:x:0:0:root:/root:/bin/bash

In this example, root is the user name, x indicates that the password is stored in /etc/shadow, 0 is the UID, 0 is the GID, root is the user info, /root is the home directory, and /bin/bash is the shell.

The /etc/group File


Before you learn how to create a user group in Linux, understand the /etc/group file! It lists the groups and their current members.

Example: sudo:x:27:user1,user2

Here, sudo is the group name, x represents the password, 27 is the GID, and user1 and user2 are members of this group.

Besides these two, various user management directories are available for different purposes. Keep exploring them to confirm your Linux Server Administrative skillset. Let’s examine the Linux command to add a user to a group.



MODIFYING AND MANAGING LINUX USERS IN GROUP


Always start by viewing the current group configurations before you change them. You can use safe commands like /etc/passwd and /etc/group to modify the Key Files and perform Ubuntu or Debian add user group. Here are the commands that you can try!

Viewing and Editing Key Files


The following command displays the file content without the risk of accidental modifications. You can use it to safely view the contents of /etc/passwd and /etc/group:

less /etc/passwd or less /etc/group


These commands open the files in a read-only pager. You can then scroll through the content and decide whether to add a group.

Please Note: Always use viewing commands like less or cat to confirm these files before making any changes. Avoid direct editing with text editors unless you're confident in your changes.



DISCOVERING HOW TO ADD A USER TO A GROUP LINUX


You can add a Linux user to a group by executing various commands that simplify user logging access to the server and its home directory. Let’s learn them one by one!

Using the “useradd” Command


The “useradd username” command creates a new user account and sets up default configurations such as the home directory and shell. You can give the new user a logging name and add a user to a Debian group with default settings. After creating the user, set a password using the “passwd username” cmdlet to complete the setup.

Using the “usermod” Command


The “usermod -aG groupname username” command adds an existing user to a primary and secondary group.

In this example, the “a” prefix appends the user to the supplementary group(s), and the “G” prefix specifies the group name(s). Since this modifies user properties, you can use it for Linux to add users to a group without removing them from existing classes.

So, you must use the -aG option every time you execute this to avoid overwriting existing group memberships.



EXAMPLES TO REMOVE OR DELETE USERS FROM GROUPS IN LINUX


Now you know how to create a user group in Linux and manage the members, it’s time to explore the best approaches to delete the members from the existing groups.

Example: userdel username

This cmdlet removes a user account from the system based on the login name of the user you provide. You can use “-r” to remove the user's home directory and “-f” to forcefully remove the user. Thus, you can delete the Linux, put users in classes, and remove the home directory and mail optionally.



CREATE & ADD GROUP LINUX COMMANDS WITH EXAMPLES


You can create a Linux primary group using the groupadd command example:

sudo groupadd newgroup


Mention the new group's name and perform Linux group add user in the system. Now, use the following example to verify the group creation:

grep new_group /etc/group


This updates the “/etc/group” files with the details of the newly created group and its home directory details. You can then display the new entries in this file. However, always use descriptive group names to avoid confusion when you add groups.



STEPS TO MODIFY & CHANGE A USER'S GROUP IN LINUX


Now, it’s time to learn how to manage members across different categories. You can always switch user memberships from one group to another or assign multiple memberships. Here’s how!

Examples For Modifying Secondary Groups


Secondary groups provide members with the appropriate permissions required for their tasks without altering their Linux primary group settings. This allows for more flexible and granular access control. To add a user to a secondary group, use the below command:

usermod -aG groupname username


To add a user to a group CentOS or Debian server, mention the group’s name and the user details.

Changing Primary Group


You can also set a primary group with the -g option for the specific user by executing the below:

usermod -g new_primary_group username


Here, the “-g” prefix sets the primary group, and the “new_primary_group” specifies the new primary group name. But, it doesn’t create a new primary group. So, ensure the new primary group exists before assigning it.

Changing Home Directory


You can modify the home directory when you add a user to a Linux group. Here’s an example command:

usermod -d /home/new_directory username


In this code, “-d” specifies the new home directory, and “/home/new_directory” provides its path. We suggest moving existing files to the new directory using the “-m” prefix and specifying the file names.



CHANGING USER PASSWORDS AFTER UBUNTU ADDING USER TO GROUPS


You can easily update the user passwords using the following commands in CentOS, Fedora, or any other Linux Distributions!

   For root: passwd username

   For logged-in users: passwd

You can use the user's login name, whose password is to be changed. This prompts the user to enter a new password. So, always follow best practices to update passwords regularly and confirm each password change.



ARE YOU NOW CLEAR WITH HOW TO ADD USER TO A GROUP IN LINUX?


We’ve discussed enough “usermod add to group” commands that you can use to control user’s logging access in Ubuntu, Fedora, or Unix servers. Now, utilize this knowledge to simplify user and group management across your CentOS, Ubuntu, or Debian Servers. Confirm the changes you make and proceed with securing user logging rights to your servers!

FAQs

You ask, and we answer! Here are the most frequently asked questions about How To Add Users To A Group In Linux!

Rashmi Debnath

Hi, I'm Rashmi D., a passionate content writer with 10+ years of expertise. By profession, I'm a cloud Architect who enjoys translating complex technologies into reader-friendly technical blogs and articles. I enjoy exploring and testing new trends and the latest tech inventions. Let's discover what's happening in the tech world together through my writings!