Enter your email address below and subscribe to our newsletter

Add user to sudoers in Ubuntu

Learn how to add user to sudoers file in Ubuntu using two simple methods.

Share your love

When you create a new user in Ubuntu and immediately try to execute commands with superuser privileges, then it will give you an error saying “user is not in the sudoers file. This incident will be reported.”:

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

The reason behind this error is the user is not in the sudoers group, and only users added to the sudoers group can execute commands with superuser privileges (commands starting with sudo).

In this tutorial, I will walk you through two ways you can add a user to sudoers in Ubuntu or any other Linux distribution:

  1. Using the usermod command (easy and recommended)
  2. Manually adding users to the /etc/sudoers file (complex but gives you more control)

Note: Make sure you follow any of the given methods by a user who already has superuser privileges, or else, it will throw multiple errors.

Now, let’s start with the first method.

1. Using the usermod command

You can use the usermod command to add users to specific groups, and the same goes for adding users to the sudoers group.

To add a user to the sudoers group, execute the following command by a user who already has superuser privileges (as the following command needs to be executed with sudo):

sudo usermod -aG sudo username

In my case, I want to add a user ankush to the sudoers group, so I’ll be using the following command:

sudo usermod -aG sudo ankush

Now, log in as a user that you recently added to the sudoers group using the following command:

su - <username>

Finally, check if the user has superuser privileges or not using the following command:

sudo whoami 

The output should return root as shown here:

Add user to sudoers file in debian linux

There you have it!

2. Adding user to sudoers file manually

As mentioned earlier, this method is a little complex compared to the previous method but gives you flexibility. For example, you can use sudo with a specific user without entering a password (not recommended) or you can specify what commands should not prompt you for a password.

So in this section, the following ways

  • Add user to sudoers file to elevate superuser privileges
  • Add user to sudoers file and have password-less sudo executions

Let’s start with the first one.

1. The manual approach

To add a user to the sudoers file manually, first, you have to execute the visudo command so that the /etc/sudoers can be opened with the nano editor.

To use the nano editor as a default text editor, use the following command:

sudo EDITOR=nano visudo

Now, go to the end of the file in nano editor by pressing Alt + / and paste the following lines (by replacing <username> with the target username):

<username>    ALL=(ALL:ALL) ALL
Add user to sudoers file in Ubuntu manually

Now, save changes and exit from the nano editor, and the user is added to the sudoers file successfully.

2. No password prompt when using sudo

I don’t recommend adding this to your configuration file, as your system is exposed to the internet and any malicious script can do whatever it wants without a finch.

Sure, if your system is isolated and only operates over the local network, then it can be a great idea!

To add a user to the sudoers file and enable password-less operations for sudo, first open the sudoers file using the following:

sudo EDITOR=nano visudo

Go to the end of the file by pressing Alt + / and paste the following line to the configuration file by replacing the <username> with the actual username:

<username>  ALL=(ALL) NOPASSWD:ALL
Use sudo without password

Save changes, exit from the nano editor, and now, if you try executing a command with sudo, it won’t ask for the password. Here’s the output when I executed sudo apt update:

Using sudo command without password

And there you have it.

Wrapping Up…

In this tutorial, I went through 2 ways you can add a user to the sudoers file. I find the first one most useful, as it requires a single command to get the job done.

If you decide to go with the second method, be cautious!

Have any suggestions or queries? Feel free to leave us a comment.

Share your love
Kabir
Kabir

A tech journalist whose life revolves around networks.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!