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.
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.
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.
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