Add and Remove Users in Linux

linux

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.

Also, read Install Fail2ban on Ubuntu

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 devopstechy, the command would be:

sudo useradd -m devopstechy

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:

[devopstechy@devopstechy~] $ sudo useradd -m devopstechy

In Linux, creating the user “devopstechy” involves assigning a unique user ID, updating the /etc/passwd file with user details, and automatically generating a home directory at /home/devopstechy 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:

[ devopstechy@devopstechy~ ] $ sudo passwd devopstechy
[ sudo] password for devopstechy:
Changing password for user devopstechy .
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, devopstechy was assigned the ID 1001:

[ devopstechy@devopstechy~ ] $ id -u devopstechy
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 devopstechy as well as his home directory, run the following command:

sudo userdel -r devopstechy

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.
Additional key feathers

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.

AmritMatti

I’m the owner of “DevOpsTechy.online” and been in the industry for almost 5 years. What I’ve noticed particularly about the industry is that it reacts slowly to the rapidly changing world of technology. I’ve done my best to introduce new technology into the community with the hopes that more technology can be utilized to serve our customers. I’m going to educate and at times demonstrate that technology can help businesses innovate and thrive. Throwing in a little bit of fun and entertainment couldn’t hurt right?

AmritMatti

I’m the owner of “DevOpsTechy.online” and been in the industry for almost 5 years. What I’ve noticed particularly about the industry is that it reacts slowly to the rapidly changing world of technology. I’ve done my best to introduce new technology into the community with the hopes that more technology can be utilized to serve our customers. I’m going to educate and at times demonstrate that technology can help businesses innovate and thrive. Throwing in a little bit of fun and entertainment couldn’t hurt right?

View all posts by AmritMatti →

Leave a Reply

Your email address will not be published. Required fields are marked *