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.
- Basics of automation using Ansible | Automate any task
- Automation of Java installation – using Ansible
- Automation of Tomcat installation – using Ansible
- 10 frequently used ansible modules with example
- Jenkins Pipeline as code – High level information
- Jenkins pipeline script to build Java application and push artifacts into repository
- Jenkins pipeline script to build & deploy application on web server
- What is End-to-End Monitoring of any web application, and Why do we need it?
- What is “Monitoring” in DevOps? Why do we need to Monitor App/DB servers, Transactions etc.?
- DevOps Engineer or Software Developer Engineer which is better for you?- Let’s discuss
- How To Be A Good DevOps Engineer?
- How to do git push, git pull, git add, git commit etc. with Bitbucket