How to Create User and Add to Sudo in Ubuntu? [2 Methods]

In the Linux-based operating system, Ubuntu, all users do not have the same privileges. By default, there is only one root user which has all the administrative power. It has the freedom that can customize, modify as well as crash the system. This is why running the system as the root user is not recommended and the substitute sudo program is introduced. When we add a regular user to the sudo program, it gains all the administrative privileges. In this article, I will show you 2 ways of “How to Create User and Add to sudo in Ubuntu”.

Process Flow Chart to Create User and Add to Sudo in Ubuntu

Flowchart of ubuntu create user and add to sudo.Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS

2 Methods to Create a User in Ubuntu and Add to Sudo

In Ubuntu, you can easily create new users from both the CLI(Command Line Interface) and GUI(Graphical User Interface). The distribution provides two different commands: the adduser & the useradd, to achieve this task from the CLI. In this section, you will learn both approaches to create a new user in Ubuntu using the CLI and the GUI.

Moreover, you can read our Comparative Analysis of Methods to distinguish between the two methods and best pick one for your needs.

Method 01: Create a User Account in Ubuntu and Add It to Sudo Group Using CLI

One of the convenient ways to create a new user in Ubuntu is by using the adduser command. This command by default adds the newly created user to a corresponding group, home directory and then prompts to set the new user password. However, you can use the useradd command to create a new user as well. But It will not be as interactive as the adduser command.

After creating a new user, by default, it will be added to the group under the username itself. If you want to give this user sudo privileges, you will need to add it to the system’s sudo group. Adding a new user to the sudo requires running the usermod command as a root user in your terminal.

In this example, I will create a new user named “new_user1” using the adduser command and then add it to the sudo group. You can follow the instructions given below to do the same:

  1. Open the Ubuntu Terminal.
  2. Type the following command in the command line and press ENTER to execute:
    sudo adduser new_user1
    EXPLANATION
    • sudo: Grants administrative privileges.
    • adduser: Adds a new user.
    • new_user1: Name of the user to be created.
  3. Then, give the password of your currently logged-in user and hit ENTER.Creating user with adduser command in ubuntu.
    EXPLANATION
    The command adds the desired “new_user1” user and its corresponding group to the system. While adding the new user with its group to the system, its User ID is also displayed inside the parentheses ().

    A home directory at the path “/home/new_user1” is also created with this command. All the default files and directories from the “/etc/skel” are copied into the home directory to establish the new user’s environment.

  4. Now, type the new password for your new user and press ENTER.
  5. Retype the password for confirmation and hit ENTER again.
  6. Continue pressing ENTER to skip the steps in the middle.However, you can give your desired information according to your need.
  7. Finally, type Y to confirm your given information.Setting password of newly created user in ubuntuAfter hitting Y the new user will be created in your system. Now, to add this user to sudo you will need to follow the next steps.
  8. Execute the following command to add the user to sudo:
    sudo usermod -aG sudo new_user1
    EXPLANATION
    • usermod: Modifies an existing user account.
    • -aG: Adds a user to the specific group.
    • new_user1: Username of the user to be given root privileges.
  9. Give the password for the currently logged-in user and hit ENTER.
  10. Now, check the updated groups of the user by running the command below.
    groups new_user1

    Adding user to sudo group.

    EXPLANATION
    In the image above, you can see that I have added the newly created use “new_user1” to the sudo group using the usermod command. As the output of the command does not show any visible differences for the user “new_user1”, I applied the additional groups command for verification. It displays all the corresponding groups of the “new_user1” where the sudo is also enlisted now.
    Furthermore, you can follow this guide to verify whether the newly created user has sudo access.

Method 02: Create a User Account in Ubuntu and Add It to Sudo Group Using GUI

Apart from using the terminal, you can create a user from the graphical user interface (GUI) as well. You do not need any command to create a new user and give it sudo privileges from the GUI. In this example, I will add a new user named “new_user2” using the GUI. Following the steps below you can do the same:

  1.   At first, click the Applications menu and then select Settings” from the available applications.Selecting Setting from applications menu.
  2.   Go to the “Users” settings as shown in the image below.Going to Users menu for creating new user in ubuntu.
  3. In addition, to perform actions as administrator press the Unlock button Enabling administrative privileges for creating new user.
  4. Further, Authenticate your user account by entering the password for the currently logged-in user.Authenticating current user account.
  5. Now, Click the Add User button at the top right corner.Adding new user in ubuntu.
  6. Additionally, Select the account type Administrator to give sudo permissions to the new user. Set the user name, and password. Finally, click on the Add button.Creating new user with sudo permissions.As a result of completing the aforementioned steps, you can see that I have successfully created a new user in Ubuntu. The user is automatically added to sudo as I created the account of Administrator type.Newly created user with sudo permissions.In the above image, you can see that the account has administrative privileges.
    However, you can also run commands from your terminal and verify whether the account is added to sudo or not.


Comparison of Methods to Create User and Add to Sudo

Since this article introduces multiple methods for a single task, you might get confused about which one to pick. Therefore, in this section, I am presenting a comparative analysis of the two different approaches for your convenience. You can learn the advantages and disadvantages of the available methods and choose what is the best for you.

Methods Pros Cons
Method 1
  • Utilizes functionality of the command line.
  • Can be used for scripting and Automation.
  • A faster process because CLI does not use additional system resources.
  • Requires knowledge of commands.
  • Has to perform a separate task for adding a user to the sudo group.
  • CLI tools are not readily available on all Linux distributions or on all versions (i.e.Alpine Linux, Arch Linux).
Method 2
  • Does not require any knowledge of commands.
  • Does not need any separate task  for setting the password and adding the new user to the sudo group.
  • Visual representation can seem easier and faster to beginner-level users.
  • Does not provide the same level of functionality as the CLI.
  • GUI uses additional system resources, thus makes the system slower.
  • Does not allow scripting/ automation or multitasking.

Summing up it can be said that both methods come with their own pros and cons. The suitable method varies from user to user. However, if you are comfortable with the CLI you can go for Method 1. On the other hand, If you do not have any idea about the Linux commands, you can use the approach through GUI.

Check if a User has Sudo Access in Ubuntu

In Ububtu you can check if the current user has sudo privileges or not with the whoami command as well.  In this example, I will verify if the newly created user “new_user1” has sudo access. For this, I will first need to switch from the current user to the “new_user1” user. Follow the steps below to do the same:

  1. Open the Ubuntu terminal.
  2. Then, type the following command in the command prompt and press the ENTER button:
    su - new_user1
    EXPLANATION
    • su: Switches user to run commands as another user.
    • new_user1: Username of the user to be given root privileges.
  3. Then, give the password of the new user and hit ENTER.
  4. Now, check the sudo access by typing the command below:
    sudo whoami
    EXPLANATION
    • whoami: Returns the username of the current user.
  5. Again, type the password for the currently logged-in user, and hit ENTER.
    Checking if a User has Sudo Access in Ubuntu.In the above image, you can see that the su command switched from the current user to the desired “new_user1” user. Moreover, typing the whoami command with sudo keyword returns the current user. Since the “new_user1” was added to sudo and has root privileges, this user is listed and shown as root user.


Check If the Newly Created User Exists in Ubuntu

In addition, to check whether a new user account is created, you can apply the given approach. Here, I will check if the newly created user “new_user1” exists on my system. You can do so by following the given steps:

  1. Open the Ubuntu terminal.
  2. Then, type the following command into the command prompt and hit ENTER:
    getent passwd | grep new_user1
    EXPLANATION
    • getent: Looks into the administrative database that is given as its argument.
    • passwd: Contains user information.
    • Pipe (|): Redirects the standard output.
    • grep: Looks for a specific pattern given to it as an argument.
    • new_user1: Username of the newly created user to be checked.

    Checking If the Newly Created User Exists in Ubuntu.In the above image, you can see that the new user “new_user1” exists on my system.

    OUTPUT ANALYSIS
    The output of the above image represents the following:

    • new_user1: Name of the user account.
    • x: Password field in a non-readable format ‘x’. which means that the password is stored in the /etc/shadow file.
    • 1001: The user ID (UID) for this user.
    • 1001: The group ID (GID) for this user.
    • ,,,: Comment field. These three consecutive commas mean that for this user it is empty.
    • /home/tom: Home directory of this user.
    • /bin/sh: Default log-in shell of this user.

Delete User and Home Directory in Ubuntu

Conversely, you can easily remove a user from the CLI using the userdel command in Ubuntu. In this example, I will delete the user account “new_user1” along with its home directory from my system. To do the same follow the below procedures:

  1. At first, open the Ubuntu terminal.
  2. Type the following command in the command line and press the ENTER button:
    sudo userdel -r new_user1
    EXPLANATION
    • userdel: Deletes a user account.
    • -r: Removes home directory and its contents.
    • tom: Username of the user to be deleted.
  3.   Accordingly, give the password of the currently logged-in user and hit ENTER.
  4. Now, to check if the user deletion was successful by typing the command below, and press ENTER:
    getent passwd | grep new_user1
    EXPLANATION
    • getent: Looks into the administrative database that is given as its argument.
    • passwd: Contains user information.
    • Pipe (|): Redirects the standard output.
    • grep: Looks for a specific pattern given to it as an argument.
    • new_user1: Username of the newly created user to be checked.

    Deleting User and Home Directory in Ubuntu.In this image, you can see that I have removed the user “new_user1” along with its home directory. Further, the ls command here shows all the available user home directories except for the “new_user1”.

Remove a User in Ubuntu from the GUI

If you do not have any knowledge of the commands in Linux, you can remove a user from the GUI also. To perform this task you must log in as a root user or, you must have the same administrative privileges as a root user. If you have such permissions, then you can follow the given steps to delete a user from your system using GUI:

  1.   At first, click the Applications menu and then select Settings” from the available applications.Selecting Setting from applications menu.
  2.   Go to the “Users” settings as shown in the image below.Going to Users menu for creating new user in ubuntu.
  3. Since, you need administrative privileges, press the Unlock button to perform actions as administrator.Enabling administrative privileges for creating new user.
  4. Furthermore, Authenticate your user account by entering the password for the currently logged-in user.Authenticating current user account.
  5. SELECT the desired user account for deletion and then CLICK the Remove user button. In this example, I will delete the user “new_user2” by clicking the Remove user button under this user.Removing desired user account from ubuntu through GUI.
  6. Now, a dialog box will appear on your screen giving multiple options regarding the deletion. You can either keep or delete the user’s home directory along with the user account. To completely remove the user SELECT the “Delete Files” option.Confirming user account removal from GUI.Successfully deleted user account from ubuntu using GUI.Upon completion of the given steps, the desired user account will disappear from your system. You can modify the deletion or cancel the task from the last step.


Conclusion

This article here presents a comprehensive guide to creating a new user in Ubuntu and adding it to the sudo group. Users of beginner to advanced levels can use the explained methods for achieving their goals. You will also be able to learn about some subsidiary tasks related to the addition of a new user. I hope these elaborative approaches will aid your journey with Ubuntu and enhance your experience in Linux.

People Also Ask

How do I create a new sudo-enabled user in Ubuntu?

To create a sudo-enabled user in Ubuntu: log into the server as the root user, then add a new user to the system, add the user to the sudo group, and finally test the sudo access of the user.

What are the basic requirements to create an Ubuntu user and add to sudo?

The basic requirements to create user and add to sudo in Ubuntu are:

  1. Need to be a root user or have root/sudo privileges to add, remove or modify a user in Ubuntu.
  2. The USER_NAME must begin with a lowercase alphabetic character and not contain any spaces or ‘@’. Furthermore, it can only include some valid letters, numbers, and symbols like ‘-’, ’_’, ‘.’ etc.

How do I create an admin in Ubuntu?

From the command line, you can create a regular user using the adduser or useradd commands and then add it to the sudo group to make it an admin user. However, while creating a user through the GUI you can choose the option of Administrator Account Type to create an admin.

What is the difference between useradd and adduser?

The useradd is a faster and lower level command that lacks entirely setting up a user account. On the other hand, the adduser is an interactive command that prompts all the required information such as user password, home directory & group by default.

How do I list all users in sudo?

You can list the available names under the sudo group by typing the getent group sudo command. Additionally, this command will return other information along with sudo usernames.

Is root user the same as sudo?

No. The root user is a special user account that has unrestricted access to the system. Whereas, sudo stands for  “substitute user do” or “super user do” which temporarily allows a regular user to exercise root privileges.

How to set sudo password?

To set the sudo password you will need to log in as the root and run the passwd command from the terminal. Then, you can change/set a new password for the user.

What is the difference between su and sudo?

To run commands as root using su you will need to enter the root password. On the other hand, to run commands as root using sudo, you will need to enter the password of the current user.


Related Articles

5/5 - (2 votes)
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Anonnya Ghosh

Hello there! I am Anonnya Ghosh, a Computer Science and Engineering graduate from Ahsanullah University of Science and Technology (AUST). Currently, I am working as a Linux Content Developer Executive at SOFTEKO. The strong bond between Linux and cybersecurity drives me to explore this world of open-source architecture. I aspire to learn new things further and contribute to the field of CS with my experience. Read Full Bio

Leave a Comment