1. Overview

In Linux, some commands can only be executed by the root user. By default, this superuser has access to all commands and resources in our system.

However, performing sensitive tasks with no restrictions is dangerous. For instance, an intentional or accidental mistake may break the whole system. As a result, Linux encourages the use of sudo. This is a command that provides a normal user with the privileges of a superuser that are necessary to execute administrative tasks. While working with sudo, we might encounter the error ‘Username is not in the sudoers file. This incident will be reported.’

In this tutorial, we’ll discuss the different approaches used to fix this error.

2. Reasons for the Error

First, let’s note that sometimes we don’t need to fix this error, such as if the system administrator has intentionally restricted our access as a security measure. This restriction causes the problem we now face. In this case, we’ll have to work with the permissions that the administrator has defined for us.

In contrast, if we have genuine control over our system, we can fix this issue.

So let’s begin by reproducing the error:

$ sudo ls
[sudo] password for francis: 
francis is not in the sudoers file.  This incident will be reported.

Above, ‘francis is not in the sudoers file’ simply means that the user francis isn’t present in the sudoers file. This is a file that defines both user and user group privileges for sudo-related tasks. Additionally, ‘This incident will be reported’ declares that Linux creates a report of this failed sudo operation. This report records what happened during this sudo incident.

3. Adding the Username to the Sudoers File

Our main goal here is to add the user directly to the sudoers file. So we need to switch to the root user, since francis can’t perform any administrative tasks:

$ su root
Password: 

Once we’ve made the switch, we’ll edit the sudoers file using nano. This is a text editor that allows us to edit files from the command line:

# nano /etc/sudoers

Now the sudoers file is ready for editing.

Let’s add a line under the user privilege specification. Its purpose is to grant the system user the mentioned superuser privileges:

# User privilege specification
root	ALL=(ALL:ALL) ALL
francis  ALL=(ALL:ALL) ALL

Now francis can perform tasks that require root access.

Shortly after making the change, we need to save these changes and exit from the text editor. To do this, we’ll press the keyboard keys CTRL+X to exit, Y to save, and Enter to submit. Finally, we can exit from the root session.

4. Adding the Username to the sudo Group

As in the solution above, it’s important that we first switch to the root user:

$ su root
Password: 

The su command allows us to perform tasks with the permissions of another user, which in this case is root.

Next, we’ll show the contents of the sudoers file. We’ll focus on the lines that declare the privileges of users, as well as those of user groups:

# cat /etc/sudoers
...

# User privilege specification
root	ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo	ALL=(ALL:ALL) ALL

...

As shown above, the root user and the members of the admin and sudo groups have superuser privileges. Then again, the sudo group is present, since we’re in a Debian-based distribution. In a Red Hat-based Linux distribution, we’ll encounter the wheel user group instead. It’s the equivalent of the sudo user group for Red Hat-based distributions.

Now, since we’re operating in Debian, let’s add our user to the sudo group:

# usermod -aG sudo francis

Here, the usermod command allows us to modify our user’s attributes. In particular, we use the -G option to declare that we’d like to update the group information for our user francis. Also, the -a option makes certain that other groups associated with this user aren’t deleted in the process. As a result, francis can now perform administrative tasks with sudo. In Red Hat distros, replacing sudo with wheel provides similar results.

Once we’re through, we’ll exit from the root user session:

# exit
exit

Now we’re back to our previous user session.

5. Conclusion

In this article, we briefly explained the meaning of the Linux error ‘Username is not in the sudoers file. This incident will be reported.’ Then we demonstrated two applicable solutions to solve this issue.

Comments are closed on this article!