Handling "User is not in sudoers file" Error in Ubuntu
Here are different reasons why you see the user not in sudoers file error and how you can approach the fix.
— Abhishek
On Ubuntu, if you have to run any commands that requires root permission, you use it with sudo.
sudo any_command
But if you are new to the whole Ubuntu thing and if you are using a non-admin user, it will not be able to run commands with sudo.
Instead, you'll see this error:
user is not in the sudoers file
In some cases, the same error could be extended to:
user is not in the sudoers file. This incident will be reported.
Here's why you see this error
- The user account you are using does not have sudo permission (99% of the time)
- The /etc/sudoers file has different permission
- The /etc/sudoers file is messed up
Fix 1: Give user sudo access
If you cannot use sudo with the current user, you need to give it sudo access.
Now, if user A cannot run commands with sudo, user A cannot just give itself sudo access.
You can either log in as root user (if that's allowed) or you can log in as another user that sudo access (if that's available).
- If you are using this system managed by someone else, you'll have to ask your sysadmin for sudo access.
- If it's your own system then most likely it is a secondary user you are using as the primary user is configured with sudo access. So give the secondary user sudo access.
Bottom line, switch to root or a user with sudo access and add the user to sudo group:
sudo adduser username sudo
Fix 2: Wrong permission on /etc/sudoers file
The /etc/sudoers should have 440 file permission:
-r--r----- 1 root root 1800 Aug 23 2022 /etc/sudoers
If it is anything else, you have trouble.
You can rectify the error by logging in as root and change the permission:
chmod 440 /etc/sudoers
Fix 3: /etc/sudoers file is all messed up
/etc/sudoers
file in Nano or Vim directly. Always use visudo
and it will open the config file for editing. However, if you make any mistake in the config file, it won't be saved. That saves you from a messed up sudoers file.This is a long shot but here's what you can do. You go into the recovery mode and from there drop to the root shell. Then you fix the sudo issue.
If you are using a cloud server or VPS, check your provider on how to access recovery mode.
On the physical server, reboot your Ubuntu system. Press and hold the shift key when the system is booting up. This should bring the grub screen.
Enter Advanced options:
And then enter the recovery mode:
Here, you should see a drop to root shell prompt:
Mount the filesystem with read, write permission.
mount -n -o remount,rw /
Give yourself permission to edit the file:
chmod u+x /etc/sudoers
And then edit the file to fix the errors:
visudo /etc/sudoers
Save the changes and give it the correct permission again:
chmod 440 /etc/sudoers
Exit the root shell and reboot the system.
wsl.exe -u root visudo
Did you manage to fix the error?
It's not a straightforward do this to fix this kind of scenario, unfortunately. You have to choose the solution based on the problem scenario. I have tried to cover the most common ones but I cannot just guess what's wrong with your sudoer file if you have a messed up one. You have to find that out yourself with some effort.
Let me know if you need any suggestions.