Add or remove users in Ubuntu command line
Basics

How to Add and Delete Users in Command-Line in Ubuntu

Pratham Patel
Pratham Patel

Table of Contents

If you manage an Ubuntu system, sooner or later, you'll need to add new users to the system or remove the existing ones.

Assuming that you are using an Ubuntu server, I'll discuss the command line methods here.

Below is the command to add a new user in Ubuntu:

sudo adduser USERNAME

Upon running this command, you will be asked for a few details. Once that information is provided correctly, a new user 'USERNAME' will be created.

And, to delete an existing user from Ubuntu, use this command:

sudo deluser USERNAME

After this command exits successfully, the user will be removed from the system. Their home directory will be untouched.

💡
The standard Linux commands for adding and removing users are useradd and userdel respectively. However, the adduser and deluser commands in Ubuntu are more user-friendly. Read more on the difference between useradd and adduser commands.

Add a new user in Ubuntu

Adding a new user to an Ubuntu system can be easily achieved by the adduser command. It is actually a Perl script that uses the standard useradd command. It creates a user with a password and home directory by default.

The syntax for the adduser command is as follows:

sudo adduser USERNAME

As you can see, you need to be a sudoer or root to run this command.

When the adduser command is executed, the following things are done automatically:

  1. The newly created user will be given a unique UID (user ID)
  2. A new group will be created with the same name as the user
  3. The new group will be given a unique GID (group id)
  4. The user will be added to the new group
  5. An empty home directory for the user will be created
  6. Contents of /etc/skel will be copied to the user's home directory
  7. Additional information, if provided will be recorded

The contents of /etc/skel include the following files:

  • .bash_logout
  • .bashrc
  • .profile

Enough talk. Let us try creating a new user called 'pratham'.

$ sudo adduser pratham
Adding user `pratham' ...
Adding new group `pratham' (1001) ...
Adding new user `pratham' (1001) with group `pratham' ...
Creating home directory `/home/pratham' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for pratham
Enter the new value, or press ENTER for the default
	Full Name []: Pratham Patel
	Room Number []:
	Work Phone []:
	Home Phone []:
	Other []:
Is the information correct? [Y/n] y

Once the details are provided, a new user will be created. Did you notice that I did not provide details like the room number, work phone, etc and the user was still created? Except for the user's password, everything else is optional.

The user password can be changed later as well. To check if the user was created/exists, you can look for their username in the /etc/passwd file like so:

$ grep pratham /etc/passwd
pratham:x:1001:1001:,,,:/home/pratham:/bin/bash

Since the output contains the username I was looking for, the user was created.

DigitalOcean – The developer cloud
Helping millions of developers easily build, test, manage, and scale applications of any size – faster than ever before.
Get started on DigitalOcean with a $100, 60-day credit for new users.

Deleting an existing user in Ubuntu

You can use deluser command to delete users in Ubuntu command line. It is also a Perl script and uses the userdel command underneath.

Below is the syntax for the deluser command:

sudo deluser USERNAME

Running this command does the following things:

  1. Delete the specified user
  2. If an empty group exists with the same as the user, delete it as well

The user's home directory is untouched.

Let us try deleting the user that was previously created.

$ sudo deluser pratham
Removing user `pratham' ...
Warning: group `pratham' has no more members.
Done.

Now, if you check the /etc/passwd file, you will notice that the user does not exist.

$ grep pratham /etc/passwd

Additionally, if you wish to delete their home directory, use the --remove-home option.

$ sudo deluser --remove-home pratham
Looking for files to backup/remove ...
Removing files ...
Removing user `pratham' ...
Warning: group `pratham' has no more members.
Done.

And with that, my user's home directory was removed, along with the user.

The deluser command has a few more useful options:

Option Description
--remove-home remove the users home directory and mail spool
--remove-all-files remove all files owned by user
--backup backup files (in current directory) before removing
--backup-to target directory for the backups
--system only remove if system user

More on it some other day.

MEGA
MEGA provides free cloud storage with convenient and powerful always-on privacy. Claim your free 20GB now

Conclusion

Both adduser and deluser are not standard Linux commands. They are scripts that use the actual useradd and userdel commands underneath. However, creating as well as deleting users with the scripts is much easier with adduser and deluser.

If you use useradd, by default, it doesn't ask to create a password or home directory for the user. These are additional pain and the adduser takes good care of it.

Next, you may want to learn about changing users in Ubuntu or even listing all the available users:

How to List Users in Ubuntu Command Line
Learn various ways of listing users in Ubuntu Linux. You’ll also learn the difference between the system and regular users.

I hope you like this tutorial on adding and removing users from the Ubuntu system in the command line.