User management in Linux is one of the most important things for system administrators. But as a developer/software engineer you also have to do it sometimes. This article is here to help you with that.
We will learn a few things in this article, like user add, modify, delete, and user group management from the command line.
Let’s start.
Replace all text in the following commands which are written in <REPLACE_THIS_PART> format with your own value/options. (do not forget to remove the < and > also).
Add user
To add a user we need useradd command, which can be used like something below:
sudo useradd
You can use options with this useradd command to define your preferences. Like, for defining a custom home directory using the following command:
sudo useradd -m -d
Here -d is for defining a custom home directory and -m is for creating the home directory
For getting more information and options of useradd use the command:
useradd —help
You can see the options available for useradd.
Add user group
To add a new group:
sudo addgroup
We can also specify which group a user belongs to while creating a new user:
sudo adduser -g
If we do not define the group name with the argument -g while creating a user, then a new user group will be created with the same name as the user name, and that user will belong to that group.
Edit / modify a user group
To add users to a new group use usermod command. The primary group of a user can be changed with the following command.
sudo usermod -g
If you do not want the user to remove from the existing primary group and also add them to add to a secondary group:
sudo usermod -a -G
Change the password of the user
To change the password of the current user:
passwd
It will ask for the current password and for a new password. Enter those values and your password will be changed.
To change the password of a user use passwd command like below:
sudo passwd
Command usermod with option -p can be used for the same purpose, but passwd is preferred.
Delete user
To delete a user, use deluser command like below:
sudo userdel
This command will not delete the home directory of the user.
If you want to delete the home directory of a user while deleting that user, then:
sudo userdel -r
Delete user Group
To delete a user group
sudo groupdel
These are basic user management commands.