Linux is a multi-user operating system, which means that multiple users can use it simultaneously. Linux has a beautiful mechanism for managing users in a system. One of the most significant duties of a system administrator is to manage the system’s users and groups.
In this article, I’ll show you how to add and remove user accounts from the command line in Linux.
Table of Contents
Add New Users in Linux
To obtain the user manual, execute the command below.
man useradd
To add users, use the useradd command as follows:
sudo useradd -m <name of the user>
For example, if you wish to add the user cloudbooklet, the command would be:
sudo useradd -m cloudbooklet
By default, useradd creates a user without generating a home directory. So, we used the -m
flag to tell useradd to create a home folder.
If the command is successful, there will be no output, as shown below:
[cloudbooklet@cloudbooklet ~] $ sudo useradd -m cloudbooklet
In Linux, creating the user “cloudbooklet” involves assigning a unique user ID, updating the /etc/passwd
file with user details, and automatically generating a home directory at /home/cloudbooklet
for storing user-specific files and configurations.
The next step after creating a user is to assign a password to that user. Set a password for the newly formed user and enable login access with the “passwd
” command.
sudo passwd <username>
The command will prompt you for a new password and will request confirmation:
[ cloudbooklet@cloudbooklet ~ ] $ sudo passwd cloudbooklet
[ sudo] password for cloudbooklet :
Changing password for user cloudbooklet .
New password :
Retype new password :
passwd : all the authentication tokens updated successfully.
By executing the “passwd
” command, the user’s password is securely stored in the /etc/shadow
file in an encrypted format. Once the command is run successfully, the newly created user will gain the ability to log in using their provided credentials.
You may see the new user’s ID by running id -u username. In our example, cloudbooklet was assigned the ID 1001
:
[ cloudbooklet@cloudbooklet ~ ] $ id -u cloudbooklet
1001
Remove a User in Linux
In Linux, use the userdel
command to delete a user as follows:
sudo userdel <username>
By default, this command saves the user’s home directory as well as several other special files, such as the user’s cron task list. If you also want to delete these files, use the --remove-all-files
flag.
For example, if you wish to delete the user cloudbooklet as well as his home directory, run the following command:
sudo userdel -r cloudbooklet
Also read: For a more comprehensive overview of Deleting Files refer to our guide Deleting Files and Directories in Linux: Step-by-Step Instructions
Additional Options
Option | Description |
---|---|
-M | Create a user with a system-wide home directory. |
-N | Do not create a user entry in the /etc/passwd file. |
-L | Lock the user’s account. |
-U | Unlock the user’s account. |
-c | Set the user’s comment field. |
-e | Set the user’s expiration date. |
-f | Force the creation of the user, even if the username already exists. |
-t | Set the user’s time zone. |
-l | Set the user’s language. |
Add and Remove Users in Linux – FAQs
Can I remove many users at once?
Yes, you can remove many users at the same time by using the “userdel” command with their usernames separated by spaces.
In Linux, is it possible to rename a user?
You can rename a user by first creating a new user with the desired username, copying their data and settings, and then deleting the old user account.
Are graphical tools for managing users available in Linux?
Yes, Linux distributions frequently include graphical tools for managing user accounts such as “Users and Groups” or “User Manager” that provide a user-friendly interface.
In Linux, how do I see a list of all users?
You can use the “cat /etc/passwd” command or the “getent passwd” command to display a list of all users registered on the system.
Can I remove a user while deleting their files and home directory?
Yes, you can erase the user’s files and home directory as well as their account by using the “userdel” command with the “-r” option.
Conclusion
Finally, knowing how to Add and Remove users in Linux is critical for effective system administration and user management. Administrators may manage a safe, well-organized, and efficient Linux environment by following the correct procedures, applying best practices, and using the available commands. Please feel free to share your thoughts and feedback in the comment section below.