How to manage users on a Linux server?
Linux allows you to create separate user accounts and control which groups and permissions are assigned to them. User management commands normally require root privileges or access through sudo.
Create a new user
To create a new user with a home directory, run:
sudo useradd -m username
Replace username with the name of the user you want to create.
Next, set a password for the new user:
sudo passwd username
Enter the new password when prompted and enter it again to confirm.
Note:
If you are already logged in as root, you can run these commands without sudo.
Add a user to a group
To add an existing user to one or more additional groups, run:
sudo usermod -aG group1,group2 username
Replace group1 and group2 with the groups you want to assign and username with the corresponding user name.
Attention:
Use the -a option together with -G when adding supplementary groups. Running usermod with -G without -a replaces the user's existing supplementary group membership.
Check user information and groups
To view the user ID, primary group, and supplementary groups assigned to a user, run:
id username
You can also display only the user's group memberships with:
groups username
Lock or unlock a user account
To lock password authentication for a user account, run:
sudo passwd -l username
To unlock it again, run:
sudo passwd -u username
Delete a user
To delete a user account while keeping its home directory and files, run:
sudo userdel username
To delete the user together with the user's home directory, run:
sudo userdel -r username
Attention:
The userdel -r command removes the user's home directory and its contents. Make sure you have backed up any required data before using it.