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:
Add New User
sudo useradd some_user_name
BashAdd User with Home Directory
You can use options with this useradd command to define your preferences. Like, for defining a custom home directory use the following command:
sudo useradd -m some_user_name
BashAdd User with Custom Home Directory
-m is for creating the home directory.
sudo useradd -m -d /home/some_custom_dir_1 some_user_name
BashHere -d is for defining a custom home directory.
useradd Command Details
For getting more information and options of useradd use the command:
useradd —help
BashYou can see the options available for useradd.
Add user group
To add a new group:
sudo addgroup some_group_name
BashWe can also specify which group a user belongs to while creating a new user:
sudo adduser -g some_group_name some_user_name
BashIf 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 existing_group_name existing_user_name
BashIf 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 existing_group_name existing_user_name
BashChange the password of the user
To change the password of the current user:
passwd
BashIt 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 your_existing_user_name
BashCommand 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 existing_user_name
BashThis 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 existing_user_name
BashDelete user Group
To delete a user group
sudo groupdel existing_user_name
BashThese are basic user management commands.