Open In App

How to Fix “Username is Not in the Sudoers File”

Last Updated : 17 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Encountering the “Username is Not in the Sudoers File” error in a Linux environment can be a frustrating roadblock for users attempting to execute privileged commands. The sudoers file is a crucial configuration that governs user permissions for administrative tasks. When a user is not included in this file, they are denied access to perform actions requiring elevated privileges. This article will guide you through the steps to resolve this issue, providing a comprehensive overview of how to add a user to the sudoers file and regain the necessary permissions for seamless system management.

Why “Username is Not in the Sudoers File” Error Occurs?

  1. User not added to the sudoers file: The sudoers file, located at /etc/sudoers, specifies which users or groups are granted sudo privileges. If a user is not listed in this file or is not a member of a group with sudo permissions, attempts to use sudo will result in the error.
  2. Syntax errors in the sudoers file: Mistakes in the syntax of the sudoers file can lead to parsing errors, causing the system to reject the file. These errors may include typos, incorrect formatting, or missing entries.
  3. Editing the sudoers file with incorrect tools: Modifying the sudoers file requires specific tools visudo to ensure proper syntax checking and avoid corruption. Editing the file directly with other text editors may introduce errors and trigger the mentioned error.
  4. File permissions: Incorrect permissions on the sudoers file itself can prevent users from accessing or modifying it. The file should be readable and writable only by the root user to maintain security.

How to Fix “Username is Not in the Sudoers File”?

In the Linux Distribution, there are two methods to solve this error. So we will look at each method in step by step approach.

  • Method 1: Changing Configuration File (/etc/sudoers.d)
  • Method 2: Adding User in Sudo Group

Let’s explore each of the resolving methods in detail with proper steps and commands.

Method 1: Changing Configuration File (/etc/sudoers.d)

The main configuration file of sudo for user entry is present in /etc/sudoers.d directory. So first if you have the root password kindly login as root for making a changes or entry in the configuration file.

Step 1: Login as Root User

sudo -s
  • -s: To start a shell with root privileges.
Login as Root User

Login as Root User

Step 2: Open the Configuration File

nano /etc/sudoers.d/kali-grant-root
  • nano: nano is the common text editor used by many Linux distributions.
  • /etc/sudoers.d/kali-grant-root: Since, for this demonstration, a Debian-based(kali) is used the main configuration for user grant resides in this location.
Opening Configuration File Using nano

Opening Configuration File Using nano

Step 3: Making Entry in Configuration File

demo ALL=(ALL:ALL) ALL
  • demo: You need to mention the username. For this demonstration username “demo” is used.
  • ALL=(ALL:ALL) ALL : It will give all administrative permission to this particular user.
Making Entry in Configuration File

Making Entry in Configuration File

Step 4: Re-evaluation of permissions

sudo apt update

To verify whether the error is fixed or not, update the repository with the help of the sudo command by using the “demo” user.

Updating the System to Verify the Fixing of error

Updating the System to Verify the Fixing of error

Method 2: Adding User in Sudo Group

In this scenario, you will see how we can add the user to the sudoer group with the help of the “usermod” command.

Step 1: Adding user into sudoers Group

sudo usermod -aG sudo demo
  • usermod: A “usermod” is a command utility to manage the permissions and do the configuration for each particular user.
  • -a: It means append the group or list of group
  • -G: It means to specify the groupname/username of which the user is also a member.
Adding User to sudoers group

Adding User to sudoers group

Step 2: Rechecking the Permissions (Verification)

After adding the user into the sudo group, restart the system to make it permanently changes. Then try to use the administrative task.

sudo passwd demo
  • passwd: It is a command utility tool used to change the user account password.

Rechecking the Permissions

Rechecking the Permissions

Conclusion

In conclusion, resolving the “Username is Not in the Sudoers File” error on Linux is crucial for restoring administrative privileges. This error can result from various issues, including user exclusion, syntax errors, or incorrect file permissions. The provided solutions—modifying the sudoers file in /etc/sudoers.d or adding the user to the sudo group using usermod—offer effective ways to address the problem. Understanding the root causes and implementing the appropriate method ensures a secure and efficient system management experience, allowing users to choose the solution that best fits their Linux distribution.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads