How to Add and Delete Users on Ubuntu?




How to Add and Delete Users on Ubuntu?

Hello Dev,

Today, I would like to show you how to add and delete users on Ubuntu. We will use how to delete users on Ubuntu. In this article, we will implement a how-to add a user with specific details on Ubuntu. We will look at examples of how to remove a user and home directory on Ubuntu.

Adding and removing users on a Linux system is one of the essential system administration tasks to familiarize yourself with. When you create a new system, you are often initially granted access only to the root account by default.

While running as the root user provides complete control over a system and its users, it is also dangerous and potentially destructive. It's advisable to add an unprivileged user for everyday system administration tasks and perform those tasks without root privileges. Additionally, you can create separate unprivileged accounts for any other users on your system. It is recommended that each user on a system have a distinct account.

There is a tool installed on Ubuntu systems for tasks requiring administrator privileges called sudo. In essence, sudo allows you to run a command as another user, including users with administrative privileges. This guide will show you how to create user accounts, assign sudo privileges, and delete users.


System running Ubuntu

Access Terminal Command line

sudo or root privileges on local or remote machines

Adding a User

To add a new user, execute the following command.

#! /bin/bash

sudo adduser <New User>

Regardless of the method chosen, you will be prompted to answer a series of questions.

Assign and confirm a password for the new user.

Enter any additional information about the new user. This step is optional and can be skipped by pressing ENTER if you do not wish to utilize these fields.

Finally, you will be asked to confirm that the information you provided is correct. Press Y to continue.

Your new user is now prepared for use and can be accessed by logging in with the provided password.

If you require your new user to have administrative privileges, proceed to the next section.

Granting a User Sudo Privileges

If your new user requires the ability to execute commands with root (administrative) privileges, you need to grant the user access to sudo. Let’s explore two approaches for this task: first, adding the user to a pre-defined sudo user group, and second, specifying privileges on a per-user basis in sudo's configuration.

By default, on Ubuntu systems, sudo is configured to grant full privileges to any user in the sudo group.

You can check the groups to which your new user belongs by using the groups command.

#! /bin/bash

sudo groups <New User>

#! Output

<New User> : <New User>

By default, a new user is only in their group because the 'adduser' command creates this group in addition to the user profile. Both the user and its group share the same name. To add the user to a different group, you can use the usermod command.

#! /bin/bash

usermod -aG sudo <New User>

The -aG option in the usermod command instructs it to add the user to the specified groups.

Please note that the usermod command itself requires sudo privileges. Consequently, you can only add users to the sudo group if you are signed in as the root user or as another user already a member of the sudo group. In the latter case, you will need to precede this command with sudo, as illustrated in this example.

#! /bin/bash

sudo usermod -aG sudo <New User>

Specifying Explicit User Privileges in /etc/sudoers

As an alternative to adding your user to the sudo group, you can use the visudo command. This command opens the configuration file /etc/sudoers in the system’s default editor, allowing you to explicitly specify privileges on a per-user basis.

Using 'visudo' is the recommended method for making changes to '/etc/sudoers' because it locks the file against multiple simultaneous edits and performs a validation check on its contents before overwriting the file. This precaution helps prevent situations where misconfigurations may occur.

If a misconfiguration occurs and you lose sudo privileges, you won't be able to use 'sudo' to fix the problem.

Execute the following command

#! /bin/bash

sudo visudo

Traditionally, visudo opened /etc/sudoers in the vi editor, which can be confusing for inexperienced users. However, on new Ubuntu installations, visudo will use the nano text editor, providing a more convenient and accessible text editing experience. Navigate using the arrow keys, and search for the line that reads like the following.

#! /etc/sudoers


Below this line, add the following highlighted line. Ensure to replace newuser with the name of the user profile to which you want to grant sudo privileges.

#! /etc/sudoers


<New User> ALL=(ALL:ALL) ALL

Add a new line like this for each user requiring full sudo privileges. Once you've finished, save and close the file by pressing CTRL + X, followed by Y, and then ENTER to confirm.

Testing Your User’s Sudo Privileges

Now, your new user can execute commands with administrative privileges.

When signed in as the new user, you can execute commands as your regular user by typing them as usual.

#! /bin/bash

<Any Command>

To execute the same command with administrative privileges, prepend sudo ahead of the command.

#! /bin/bash

sudo <Any Command>

When doing this, you will be prompted to enter the password of the regular user account with which you signed in.

Deleting a User

If you no longer need a user, it is advisable to delete the old account.

To delete the user without removing any of their files, execute the following command.

#! /bin/bash

sudo deluser <Delete User>

If, instead, you want to delete the user's home directory when the user is deleted, execute the following command.

#! /bin/bash

sudo deluser --remove-home <Delete User>

If you previously configured sudo privileges for the user you deleted, execute the following command.

#! /bin/bash

sudo visudo

#! /etc/sudoers


<Delete User> ALL=(ALL:ALL) ALL # Delete This Line

I hope it can help you...