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.
userdelrespectively. However, the
delusercommands 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.
adduser command is executed, the following things are done automatically:
- The newly created user will be given a unique UID (user ID)
- A new group will be created with the same name as the user
- The new group will be given a unique GID (group id)
- The user will be added to the new group
- An empty home directory for the user will be created
- Contents of
/etc/skelwill be copied to the user's home directory
- Additional information, if provided will be recorded
The contents of
/etc/skel include the following files:
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.
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
sudo deluser USERNAME
Running this command does the following things:
- Delete the specified user
- 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
$ 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:
|--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.
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.
I hope you like this tutorial on adding and removing users from the Ubuntu system in the command line.