Managing Users on Ubuntu – Create and Delete a User with Sudo Privileges

Tutorial: Managing Users on Ubuntu – Create and Delete a User with Sudo Privileges

Introduction

This tutorial will guide you through the process of creating a new user on an Ubuntu system, granting them sudo privileges, and then deleting the user when they are no longer needed. This is useful for managing temporary or limited-access users on your system.

We will:

  1. Create a user named <username>
  2. Grant the user sudo privileges
  3. Verify the user’s sudo access
  4. Delete the user when they are no longer required

Prerequisites

  • A system running Ubuntu (or a similar Debian-based distribution)
  • A user with administrative privileges (sudo access)
  • Basic knowledge of terminal commands

Steps

Step 1 – Create a New User

To create a new user, use the adduser command. This will prompt you to set a password and provide additional optional information.

sudo adduser <username>

You will see prompts like the following:

Adding user `<username>` ...
Adding new group `<username>` (1001) ...
Adding new user `<username>` (1001) with group `<username>` ...
Creating home directory `/home/<username>` ...
Copying files from `/etc/skel` ...
New password:
Retype new password:
passwd: password updated successfully

Press Enter to skip the optional information, or fill it in as needed.


Step 2 – Grant Sudo Privileges to the User

To allow the new user to execute administrative commands, add them to the sudo group:

sudo usermod -aG sudo <username>

Step 3 – Verify Sudo Access

To confirm the user has sudo privileges, switch to the new user and run a command with sudo:

su - <username>
sudo whoami

Enter the password for <username> when prompted. If the output is root, the user has successfully been granted sudo privileges.


Step 4 – Delete the User

When the user is no longer needed, you can delete them. You have two options:

  1. Delete the user without removing their home directory: sudo deluser <username>
  2. Delete the user and remove their home directory: sudo deluser --remove-home <username>

Step 5 – Remove the User’s Mail Spool (Optional)

If the user had an email account on the system, remove their mail spool:

sudo rm -rf /var/mail/<username>

Conclusion

In this tutorial, you learned how to create a new user on Ubuntu, grant them sudo privileges, verify their access, and delete the user when necessary. Managing users effectively helps maintain the security and organization of your system.

For further reading, check out:

  • How to Manage Groups on Ubuntu
  • Understanding User Permissions in Linux

Leave a Comment