How to Add User to a Group in Linux Which Already exist.

In this method, we shall create a new user for demonstration purposes. Then, we shall add it to an already existing group.

Step 1: Creating a user(Optional)

We shall create a test user using the useradd command. This command is used to add users to the linux system.

useradd geek


Here, we are using the username as geek. You are free to use any name of your choice.

Adding geek user to the system

Now, to verify whether the user ahas been added or not, use the following command.

cut -d: -f1 /etc/passwd | grep 'geek


The cut command gets content from the file based on the condition given. Here

  • We are selecting the first field with option -f1.
  • The fields are divided by the delimites : passed with -d.
  • We access the /etc/passwd file which contains all the users that exists in the system. (You need either root or sudo access to read this file)
  • Then, we grep our user name ‘geek’ to see if it is added to the users list.

Checking the user in /etc/passwd

Here, we are getting the output geek which means that user has been added successfully.

Step 2: Adding the created user to the sudoers group

Now, to add any user to a group, we have the usermod command which is for user modification.

The syntax is:

usermod [options] [other fields...] [username]


Here, we will use the -aG option which means append to group then the first argument after the -aG option we will pass the group name and then, finally the username.

In order to add the geek user to sudo group, we need the following modifications:

usermod -aG sudo geek


Once, this command is executed. You can check all the groups that the geek user belongs to by using the groups command.

groups geek


This will list all the groups that the user ‘geek’ is a member of.

Verifying the user’s groups

As we can see, the geek user belongs to 2 groups, geek(every user gets a group of its own name) and sudo. Thus, we successfully added the user geek to sudo group.

How to Add User to a Group in Linux

A group in Linux is a way to put users with similar access and permissions in a collection. By using groups, an administrator can define access control and permissions for all the users belonging to that group. Without groups, the administrator would have to define roles for individual users however, with groups those roles can be given to a group which in turn, will apply to all the users in the group.

In this article, we shall learn how to add a user to a group. We shall see different methods to do the same.

Table of Content

  • What is Linux Group?
  • Pre-requisites to Add a User to a Group in Linux
  • How to Add User to a Group in Linux while creating the user.
  • How to Add User to a Group in Linux Which Already exist.
  • Frequesty Asked Questions

Similar Reads

What is Linux Group?

A Linux group is a collection of user accounts that share common access permissions to files, directories, and other system resources. Each user on a Linux system is associated with one or more groups, and groups are used to simplify the process of managing user access and privileges....

Pre-requisites to Add a User to a Group in Linux

A linux machine with root/sudo access. Basic understanding of Linux terminal. Understanding of useradd and usermod command....

How to Add User to a Group in Linux while creating the user.

Step 1: Creating a user while adding it to a group....

How to Add User to a Group in Linux Which Already exist.

In this method, we shall create a new user for demonstration purposes. Then, we shall add it to an already existing group....

Frequesty Asked Questions

Why would I need to add a user to a group in Linux?...

Conclusion

In this article we discussed hot to add a user to Linux groups . Linux Hroups which are integral for organizing users with shared access requirements, streamlining permissions management, and enhancing system security. Through Group Identifiers (GID) and primary/secondary group distinctions, users inherit permissions efficiently based on group memberships, reducing administrative complexities. This article outlined practical steps for adding users to groups in Linux using commands like useradd and usermod, catering to users with root or sudo access. Linux groups play a crucial role in providing a structured and secure approach to user access within the Linux environment, contributing to effective system administration....