Newsletter Subscribe
Enter your email address below and subscribe to our newsletter
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.”:
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:
/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.
Table of Contents
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:
There you have it!
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
Let’s start with the first one.
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
Now, save changes and exit from the nano editor, and the user is added to the sudoers file successfully.
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
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
:
And there you have it.
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.