How to fix "user is not in the sudoers" error on Linux

Endrit Qerreti

Endrit Qerreti

Did you try to run a a command with sudo on Linux and it did not work because of the error "this user it's not in the sudoers file" .

Technically this is not an issue, it's just telling you that, the user account that you are trying to run the sudo command, doesn't have sudo access.

In this tutorial, you will learn how to fix this error, and how to add an user in the sudoers file, so you can run sudo commands from that user.

For example, I tried to run sudo apt update to update my system, and the command did not work because the account that was I using owlhowto did not have sudo access, which means it wasn't in the sudo group.

Ok, so let's fix this.

Step 1 - Login as root

First, you need to login as root. Run the command below, and then enter the root password and press enter

su root

You should see a similar output in your terminal, this means you are logged in as root

💡
Note: You must have access to user root in order to make changes on the sudoers file. 

Step 2 - Add user in the sudoers file

Adding user in the sudoers file is easy. All you have to do, is to open the sudoers file with a text editor, or running the visudo command once you are logged in as root. We'll cover both methods in the steps below.

Add user into sudoers file via visudo

1) Login as root

su root

2)Next, run the visudo command

sudo visudo

💡
This command will open the sudoers file just like any other text editor. However, the difference between visudo and other text editors is that, visudo validates and checks for syntax errors, and if you accidentally write a line that has syntax error the sudoers file won't be saved, and it will show you where the error is as shown in the image below. visudo is a much safer method to edit the sudoers file

There are 3 options that you can use on the "What now?" prompt :

e - Allows you to edit the sudoers file again

x - Exit without saving changes

Q - This option will quit and save changes , don't use this option if sudoers file has syntax errors as you will end up with a broken sudoers file which means a broken system.

3) Add the config below into the sudoers file

owlhowto ALL=(ALL:ALL) ALL

💡
owlhowto is the username of the account that we are adding into the sudoers file

Add user into sudoers file, using text editor

sudoers file is located on /etc/sudoers so to open this file with a text editor you need to run the command below

nano /etc/sudoers

💡
Note: The reason why we are not opening the sudoers file with sudo is because we are already logged in as root. 

Next, you need to add this configuration to the sudoers file

owlhowto ALL=(ALL:ALL) ALL

💡
Note: replace owlhowto with the username of your account before adding it on the sudoers file.

Once added, the configuration in the sudoers file should look like this

Now you need to save the changes by pressing CTRL + X, then press the Y key to confirm

Once you confirm with Y you will see the following screen, now you need to press enter.

After saving the changes, you will be redirected to the normal terminal. This means the changes you made in the sudoers file were successfully saved.

💡
Note: You will still be logged in as root, so you can safely exit the root shell session now, by running the exit command in your terminal

Step 3 - Test sudo command

Once you add the user in the sudoers file, you need to verify that the configuration was added correctly, and that the sudo access works now when you run the sudo command from that account.

To verify that the configuration is done correctly, you can view the content of the sudoers file, by using the cat command

cat /etc/sudoders

Output

owlhowto@debian:~$ cat /etc/sudoers 
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults	env_reset
Defaults	mail_badpass
Defaults	secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# This fixes CVE-2005-4890 and possibly breaks some versions of kdesu
# (#1011624, https://bugs.kde.org/show_bug.cgi?id=452532)
Defaults	use_pty

# This preserves proxy settings from user environments of root
# equivalent users (group sudo)
#Defaults:%sudo env_keep += "http_proxy https_proxy ftp_proxy all_proxy no_proxy"

# This allows running arbitrary commands, but so does ALL, and it means
# different sudoers have their choice of editor respected.
#Defaults:%sudo env_keep += "EDITOR"

# Completely harmless preservation of a user preference.
#Defaults:%sudo env_keep += "GREP_COLOR"

# While you shouldn't normally run git as root, you need to with etckeeper
#Defaults:%sudo env_keep += "GIT_AUTHOR_* GIT_COMMITTER_*"

# Per-user preferences; root won't have sensible values for them.
#Defaults:%sudo env_keep += "EMAIL DEBEMAIL DEBFULLNAME"

# "sudo scp" or "sudo rsync" should be able to use your SSH agent.
#Defaults:%sudo env_keep += "SSH_AGENT_PID SSH_AUTH_SOCK"

# Ditto for GPG agent
#Defaults:%sudo env_keep += "GPG_AGENT_INFO"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root	ALL=(ALL:ALL) ALL
owlhowto ALL=(ALL:ALL) ALL
# Allow members of group sudo to execute any command
%sudo	ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "@include" directives:

@includedir /etc/sudoers.d

And the configuration should be exactly the same as the configuration that we added earlier

Now, you can run a real test to see if the sudo command works.  Make sure you are logged in to the account that you added in the sudoers file.

For example, the command sudo apt update wasn't working for the user owlhowto, now that user owlhowto is added in the sudoers file, sudo command works with no issues.

Output

owlhowto@debian:~$ sudo apt update
Get:1 http://security.debian.org/debian-security bookworm-security InRelease [48.0 kB]
Hit:2 http://deb.debian.org/debian bookworm InRelease
Get:3 http://deb.debian.org/debian bookworm-updates InRelease [52.1 kB]
Get:4 http://security.debian.org/debian-security bookworm-security/main Sources [11.1 kB]
Get:5 http://security.debian.org/debian-security bookworm-security/main amd64 Packages [24.2 kB]
Get:6 http://security.debian.org/debian-security bookworm-security/main Translation-en [10.9 kB]
Fetched 146 kB in 1s (159 kB/s)           
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
6 packages can be upgraded. Run 'apt list --upgradable' to see them.

You can test any other command as long as it requires sudo access to run.

Conclusion

In this tutorial, you learned how to fix the error "user is not in the sudoers file", by adding the user in the sudoers file, using two different methods, by using visudo and by editing the sudoers file manually with a text editor. We also explained what the difference is between the two methods.