Add or remove a user on the Linux servers
These instructions cover adding and removing users on a Linux server. The process may differ between RHEL-based and Debian-based distributions, where you may be prompted for a password and additional user information. It is not recommended to work under the root account on a regular basis. Any mistake or account compromise gives full control over the server. Instead, create a dedicated user and grant only the permissions required for their tasks (principle of least privilege). Use sudo to run individual commands with elevated privileges instead of logging in directly as root.
To add a user, for example "introserv", the procedure differs between Debian-based and RHEL-based distributions.
For Debian-based systems (Debian, Ubuntu)
Use the interactive adduser script. It creates the user, sets the password, and prompts for additional information in a single step:
adduser introserv
For RHEL-based systems (RHEL, AlmaLinux, RockyLinux, CentOS)
In RHEL-based distributions, adduser is a symlink to useradd and is not interactive. Running adduser introserv is equivalent to running useradd introserv, and the password must be set separately.
Create a user:
useradd -m introserv
Set a password for the user:
passwd introserv
The useradd command creates a new user with system defaults specified in the /etc/default/useradd file.
The following actions are performed when a user is created:
- a new user with the name "introserv" is created
- a group with the same name is created
- a home directory is created in /home/introserv
- files from the /etc/skel directory are copied to the home directory (this directory contains files used as a template for all new users)
To add a new user to the sudo group, run the following command:
For Debian-based systems:
usermod -aG sudo introserv
For RHEL-based systems:
usermod -aG wheel introserv
The changes take effect after the user logs out and logs back in.
To remove a user from the sudo group, run the following command:
For Debian-based systems:
gpasswd -d introserv sudo
For RHEL-based systems:
gpasswd -d introserv wheel
To delete a user, run the following command:
For Debian-based systems:
deluser --remove-home introserv
For RHEL-based systems:
userdel -r introserv
Both commands remove the user along with their home directory. Files owned by the user outside the home directory will not be removed automatically. Without the --remove-home flag (Debian-based) or -r flag (RHEL-based), the home directory will remain on the server.
Before deleting a user, make sure they have no active running processes:
ps -u introserv