Introduction

How do you create a new user with sudo privileges on a Ubuntu 18.04 machine?

On Unix-like operating systems, sudo allows users to run programs with the same security privileges given to the superuser (also known as root, administrator, admin, or supervisor). It originally meant "superuser do" because sudo was invented to run commands only as the superuser on older versions of Linux.

By giving a user on your machine sudo privileges, that user can execute commands and run programs with root or administrative permissions. You must be careful with this because many of the commands and programs that require that level of permissions can have harmful and/or malicious side effects on your machine.

In this article, we'll walk you through how to create a new user on your Ubuntu 18.04 machine and configure the user to have sudo privileges. And we'll also cover how to remove a user's sudo permissions if you ever have the need.

Let's get started!

Table Of Contents

Create a New User

First, let's add a new user to your machine.

You can do that with this command (replace bob with the username you want to use):

    
      adduser bob
    
  

You'll be prompted to set the password for your new user and also confirm it by retyping the value:

    
      Enter new UNIX password:
      Retype new UNIX password:
      passwd: password updated successfully
    
  

Next, you'll be prompted to set some information for your new user. You can fill them out or just press ENTER repeatedly to skip each one.

    
      Changing the user information for bob
      Enter the new value, or press ENTER for the default
          Full Name []:
          Room Number []:
          Work Phone []:
          Home Phone []:
          Other []:
      Is the information correct? [Y/n]
    
  

Your new user is now created!

Add Your New User to the Sudo Group

Now that you have a new user, you can add it to the sudo group and, therefore, give it sudo privileges.

Use this command to add your user to the sudo group:

    
      usermod -aG sudo bob
    
  

Your user is now part of the sudo group!

Test Your User's Sudo Access

Let's run a quick test command to verify that your user's new sudo permissions are working correctly.

First, switch from the root user to your new user with this command:

    
      su - bob
    
  

As your new user, you can run a sudo privileged command by prepending sudo to the command you wish to execute with superuser permission.

It would look something like this:

    
      sudo command_you_want_to_run
    
  

To serve as an example, let's list out the contents of the /root directory. This directory is normally only accessible to the root user.

    
      sudo ls -la /root
    
  

Since this is the first time you've used the sudo command in a session, you'll be prompted to enter the password for your sudo-enabled user:

    
      [sudo] password for bob:
    
  

If you entered the correct password and your user is in the sudo group, the sudo command will be successfully run with root privileges.

Remove Your User's Sudo Privileges

In the future, you may need to remove a user's sudo privileges for one reason or another.

To do this, you can use this command:

    
      deluser bob sudo
    
  

This will only take sudo access away from the user profile and will not delete the user itself.

Let's test whether or not the user has been removed from the sudo group.

First, switch over and use your user:

    
      su - bob
    
  

And retry the same command from the last section that lists the contents of the /root directory:

    
      sudo ls -la /root
    
  

This command should fail and output something similar:

    
      bob is not in the sudoers file.  This incident will be reported.
    
  

If the command was successful, make sure you have removed the user from the sudo group.

Conclusion

In this article, we went over how to create a new user, give it sudo privileges by adding it to the sudo group, and remove a user from the sudo group if you ever need to do so.

Thanks for reading and happy coding!