How to Add and Delete Users on Ubuntu 24.04


Adding or deleting a user is the key activity of a Ubuntu administrator. Whenever a new user joins the workforce of a specific system, the administrator creates a dedicated account with the required permissions/privileges. Similarly, if a user leaves, the administrator has to delete or disable the user account and other necessary data/files (if required).

This post addresses the possible ways to add and delete users on Ubuntu 24.04:

Outline

Adding a User on Ubuntu 24.04

Like previous Ubuntu releases, the Ubuntu 24.04 is equipped with the command line and GUI support. A user can be added via both the interfaces (CLI and GUI). Let’s start with the useradd command:

useradd Command

The useradd command offers more control while adding a user on Ubuntu 24.04. It can be used to add a user with the very least privileges or the administrator can use various options to add the required features to the account being created.

Syntax:

sudo useradd [options] [user-name]

The options of the useradd command can be seen via the command:

useradd --help

Example 1: Add a User With Least Functionality

The basic usage of the useradd command adds the user with the least functionalities:

sudo useradd <user-name>

If you are adding a user with the useradd command’s basic mode, you won’t be allowed to switch to the newly created user because the password is not set. To make the user functional, add the password to the newly created user:

sudo passwd <user-name>

Once the password is set, you can easily switch/log in to that user.

You can verify the user’s creation via one of the following commands:

grep <user-name> /etc/passwd
id <user-name>

Note: A regular/normal user has a user ID >= 1000 whereas the 0-999 UIDs are reserved for the system users/accounts.

Example 2: Add a System User

The system user handles/manages the system-defined process and the system users are managed by the system itself. The system automatically creates the system user if required. However, if you want to create a system user, you can do it as:

sudo useradd --system <user-name>

Upon verification, you can see that the UID is “995” for the newly created system user:

Example 3: Create a User With an Existing Home Directory

By default, the user created with the “useradd” command only allocates the home directory but it does not exist physically on the system and is thus useless. To make it functional, you need to create a directory and then add the user to that home directory:

Creating a directory:

Adding the user with the home directory as created in the previous step:

sudo useradd --home <path-of-home-directory> <user-name>

Here’s the verification:

Similarly, you can use the “u” flag to define the specific user ID of the user to be created, and the “s” flag to define a login shell of the user. All these details can be checked from the manual of the useradd command:

man useradd

Let’s get into the other method:

adduser Command

The adduser command is the front-end or the more user-friendly interface of the useradd command. It uses the useradd command at the back end but provides an easier way, especially for beginners.

Let’s go through its syntax first:

Syntax:

sudo adduser <options> <usere-name>

Example 1: Add a Normal/Regular User

With no options, the adduser command creates the user with all the necessities that a normal user should have, i.e., home directory, password, phone, additional details, etc:

sudo adduser <user-name>

Look at the process of user creation, you do not have to set the home directory, password, and additional details. The adduser command does it all for you. Here’s the verification:

Example 2: Add a System User

You can add a system user using the “–system” flag:

sudo adduser --system <user-name>

The output shows that the system user with UID 122 has been created.

Advanced Usage of the adduser Command to Create a User

The adduser command provides a list of options to create regular as well as normal users. For instance, you can use the “–uid” flag or “–home dir” to specify the UID or user directory other than the default home directory. All these flags can be seen in the output of the following command:

adduser --help

  • Which Command to Use? adduser or the useradd

If you are a beginner, you must go for the “adduser” command which adds the user with default parameters. However, if you want more control over the user creation process (user with customized parameters), you should opt for “useradd”.

newusers Command | Adding Users in Bulk

The useradd and adduser commands add a single user at one time. What if you want to create multiple users at once? You can do it using the “newusers” command, as follows:

Step 1: Create a .txt File

First, create a .txt file and the details in it must follow the pattern of the “/etc/passwd” file. While creating, you must ensure that the user, or the user ID, does not exist already. Each line of the file must contain different user details.

We have added the details of the four users inside the “txt” file, i.e., genie1, genie2, genie3, and genie4:

Step 2: Add the Users

Use the “newusers” command followed by the name of the text file to add all the users:

sudo newusers users.txt

The tail command is applied on the used to confirm the addition of these four users:

That’s how you can add multiple users in Ubuntu 24.04.

Manually Adding a User Entry in /etc/passwd File

Whenever a user is created, all its details are stored in the “/etc/passwd” file. You can manually add the user entry in the “/etc/passwd” file to create a new user. However, this method is not recommended as it may create system failure while editing the “/etc/passwd” file or you may delete some important user data.

Here’s the two-step process to manually add the user through “/etc/passwd” file:

Step 1: Place the Entry in /etc/passwd File

To make changes in the “/etc/passwd” file, open it in any editor and add the entry at the end of the file, i.e., we are adding user “linux” with the “UID=1009”, “GID=1005”, “home directory=/home/linux”, and “shell=/bin/bash”:

Here’s the verification of the newly created user:

Step 2: Enable the User

Initially, the user added via this method would be disabled, you need to enable it by setting the password of the user:

sudo passwd <user-name>

These are all the possible command line methods to add a user on Ubuntu 24.04.

Ubuntu Settings | GUI-Based User Addition

To add a user from Ubuntu’s GUI, open the settings and navigate to the “Users” section inside the “System”, as seen below:

Unlock the settings:

Scroll down and click on the “Add User…” button:

An interactive user creation form will appear. Add the details of the user and click on “Add”. You can also choose the “Administrative” account by toggling ON the “Administrator” button:

That’s how you can add a user on Ubuntu 24.04.

Deleting a User From Ubuntu 24.04

The “uderdel” and the “deluser” commands can be used to remove the user from Ubuntu 24.04. Likewise addition, you can also remove the user from the GUI as well. Let’s demonstrate all these methods:

userdel Command

The userdel command (like useradd) is the primary utility to delete a user from Ubuntu. Here is the syntax of the userdel command:

sudo userdel <options> <user-name>

Example 1: Delete a Non-Logged in User

It is recommended to delete the non-logged-in user, i.e., for instance, we are logged in as a user “adnan” and we will delete the user named “genie”:

sudo userdel <user-name>

Example 2: Delete the Logged-In User | Deleting a User Forcefully

If it is necessary to delete a logged-in user, first, kill all the processes associated with that user using the “killall” command and then use the “-f” option with the “userdel” command to delete the user forcefully:

sudo killall -u <user-name>
sudo userdel -f <user-name>

Advanced Usage of the userdel Command

For more detailed usage of the “userdel” command, follow the supported options of this command:

userdel --help

deluser Command

The “deluser” acts as the front end of the userdel command. The syntax of the “deluser” command is provided below:

sudo deluser <options> <user-name>

The general command (without options) to delete the user is:

sudo deluser <user-name>

The general “deluser” command won’t remove the home directory. To remove the home directory as well, use the “–remove-home” flag with the username:

sudo deluser --remove-home <user-name>

Advanced Usage of the deluser Command

Get the help page and check the detailed usage of the “deluser” command:

deluser --help

Manually Deleting the User From /etc/passwd and /etc/shadow

Like user addition, you can delete the user entry from the “/etc/passwd” file to delete that user. For that, open the “/etc/passwd” and delete the complete entry, i.e., we are removing the user “genie2”:

After deletion, when you check the existence of the user. It won’t be available:

Note: Try not to opt for this method when you can delete a user with the “deluser” or “userdel” commands. As the manual changes in the file are risky.

Ubuntu Settings | GUI-Based Removal

To remove a user on Ubuntu 24.04 using the GUI, open the “Users” section in the “System” settings, i.e., (Settings > System > Users). Here, you have to unlock the users’ settings to make any changes:

Scroll down and click on the user you want to remove/delete:

Now, click on the “Remove User” button to proceed with:

Choose whether you want to keep/delete the files and settings and click on the “Remove” button:

This is all about adding and deleting a user on/from Ubuntu 24.04.

Bottom Line

The “useradd” is the recommended utility to add a user on Ubuntu 24.04 as it offers more control on the user addition. However, if a more easy method is the requirement, then you can go for the “adduser” command which is a script based on the “useradd” command.

Similarly, you can use “userdel” for more detailed options on deletion and the “deluser” command if you are new to Ubuntu. Moreover, both deletion and addition can be carried out using the GUI. That’s how you can deal with the addition and deletion of users on Ubuntu 24.04.

Print Friendly, PDF & Email
Categories