How to Fix User Is Not in the Sudoers File in Ubuntu
This guide explains how to resolve the āuser is not in the sudoers fileā error in Ubuntu. This issue occurs when a standard user attempts to execute administrative commands without proper privileges. We will cover methods to grant sudo access using recovery mode or an existing administrative account, ensuring you can regain control of your system safely and efficiently.
Understanding the Error
The error message āuser is not in the sudoers file. This incident
will be reportedā appears when you try to use the sudo
command with an account that lacks administrative permissions. Ubuntu
restricts sudo access to specific users defined in the sudoers
configuration file to protect the system from unauthorized changes. To
fix this, you must add your user account to the sudo group.
Method 1: Using Another Administrative Account
If you have access to another user account on the system that already has sudo privileges, this is the easiest method.
Log in to the account with sudo access.
Open the terminal.
Run the following command, replacing
usernamewith the name of the account that needs access:sudo usermod -aG sudo usernameLog out and log back into the restricted account.
Test the access by running
sudo ls /root.
Method 2: Using Recovery Mode
If you do not have any other account with sudo privileges, you must use the GRUB recovery menu to access the root shell.
Restart your computer.
Hold the
Shiftkey or pressEscrepeatedly during boot to display the GRUB menu.Select Advanced options for Ubuntu.
Choose the kernel version ending with (recovery mode).
In the Recovery Menu, select root (Drop to root shell prompt).
The filesystem is mounted as read-only by default. Remount it as read-write by entering:
mount -o remount,rw /Add your user to the sudo group using the following command:
usermod -aG sudo usernameType
rebootto restart the system normally.
Verifying the Fix
Once you have logged back into your user account, verify that the permissions were applied correctly. Open a terminal and run:
sudo whoamiIf the command returns root without displaying the
sudoers error, the issue is resolved. You now have administrative
privileges on your Ubuntu system.