Linux user and group creation, modification and deletion

Linux User and Group creation, modification and deletion

Last Updated on January 24, 2024 by cscontents

Introduction

Linux User and Group management is one of the most crucial parts of Linux machine usage. Navigating the Linux command line can feel like cracking a code, especially when it comes to managing those mysterious things called users and groups. Fear not, fellow adventurers, this is a simple guide for you to understand Linux user and group creation, modification, and deletion.

Prerequisite

To understand this guide or document you need to have a basic level of understanding of the Linux command line and knowledge about Linux commands.

Linux User

We will use “test_user” as an example Linux user and “test_group” as an example Linux group in the below commands, please replace it with your user and group.

Creating a Linux User

Command to create a user:

sudo useradd test_user

To verify whether the user is created or not, you can check the /etc/passwd file.

cat /etc/passwd

To Add a Password to a Linux User

To add a password to a Linux user run the below command. Once you run the below command it will ask you to enter the ‘password’ and again it will ask you to enter the ‘confirm password’.

sudo passwd test_user

Adding a Linux User in a Group

Case 1:

We will create a new user and at the same time, we will add it to an existing Linux Group. Assuming we have an existing Linux Group “test_user”. Now let’s create a user “test_user” and add it to the Linux Group “test_group”.

sudo useradd -G test_group test_user

In the above command ‘-G’ flag stands for ‘–groups’.

Case 2:

If we want to add an existing Linux user to an existing Linux group, then run the below command. Let’s assume “test_group” is an existing group that was created previously and “test_user” is an existing Linux user that was created previously.

sudo usermod -aG test_group test_user

Removing a Linux User from a Linux Group

To remove a Linux user from a Linux group, run the below command. Let’s remove the “test_user” from the “test_group”.

sudo gpasswd --delete test_user test_group

Deleting a Linux User

Command to delete a user.

sudo userdel test_user

If you want to delete the Linux user as well as its home directory, then run the below command.

sudo userdel -r test_user

If you want to force delete a Linux user (for example, when the user is running an active process), run the below command.

sudo userdel -f test_user

Linux Group

We will use “test_group” as an example Linux group in the below commands, please replace it with your Linux group.

Creating a Linux Group

Command to create a group in Linux:

sudo groupadd test_group

To verify whether the group is created or not, you can check the /etc/group file.

cat /etc/group

Creating Linux Group with Specific Group ID

When a group is created a unique Group ID is assigned to that group which can be verified from the /etc/group file.

To create a Linux Group with a specific Group ID, for example, we will create a Linux user with Group ID 1005.

sudo groupadd -g 1005 test_group

In case, the above Group ID 1005 is already allocated to some other group, you will get some alert after running the above command.

Renaming a Linux Group

To rename a Linux Group, run the below command. For example, we want to rename “test_group” to Test_group_2”.

sudo groupmod -n test_group_2 test_group

Deleting a Linux Group

To delete a Linux Group run the below command.

sudo groupdel test_group

 

Thank You.

If you are interested in learning DevOps, please have a look at the below articles, which will help you greatly.