When working with Ubuntu 22.04 installed in VirtualBox, you may encounter an error message stating “Username is not in the sudoers file. This incident will be reported” when attempting to run commands with sudo privileges.
This error occurs because the user you’re logged in as doesn’t have the necessary permissions to execute commands with administrative privileges.
In this article, we will guide you through the steps to fix this error by adding your user to the sudoers file.
Steps to fix “Username is not in the sudoers file” Error
We will fix these errors by creating an admin user group (if doesn’t exist) which will have root user privileges. Then we will assign our default user to that admin group. So, mutually it will automatically get those missing privileges and the error will go away.
mount -o remount,rw / #to enable filesystem read-write mode sudo groupadd admin #to add admin group sudo usermod -aG admin username #to assign username to admin group sudo -l -U username #to check updated user previleges
Keep reading for a detailed explanation of each step along with screenshots.
Step 1: Accessing Ubuntu recovery mode:
- Launch the VirtualBox containing Ubuntu 22.04. (Press Shift key while booting)
- When the GRUB bootloader menu appears, select “Advanced options for Ubuntu.”
- Choose the entry with “(recovery mode)” appended to the kernel version and press Enter.
- In the Recovery Menu, select “root: Drop to root shell prompt” and press Enter. If the root password for maintenance is asked, enter the password you had set at the time of the virtual machine setup for the default user.
Now enter the following command to remount the directory filesystem with read-write mode,
mount -o remount,rw /
Step 2: Add admin user group
After dropping to the root shell prompt, you will see a command prompt that starts with “#”. This indicates that you have root privileges. To create a new group called “admin,” enter the following command:
sudo groupadd admin
Step 3: Assign username to the admin group
Next, add your username to the “admin” group using the following command:
sudo usermod -aG admin username
Replace “username” with your actual username on Ubuntu. For example, in the above screenshot, the username is “imran”.
Note: alternatively you can directly use the “sudo” group to assign related privileges to username. Just use “sudo” instead of “admin” in the above command.
Step 4: Verifying the user attributes
You can verify the username’s group association by using the following command,
sudo -l -U username
Step 5: Reboot and test changes
Restart your Ubuntu virtual machine by entering the command:
Once the system restarts, log in using your username and password. Open the Terminal by pressing Ctrl+Alt+T or searching for “Terminal” in the application launcher. Test the sudo command by running:
sudo apt update
If the command executes successfully without any errors, then you have resolved the issue.
This method is 100% working for the mentioned case where Ubuntu is recently installed as a virtual machine using a virtual box. And please note that this method requires a root password if asked so.
If the user privileges are blocked or limited by the administrator root user then such command executions right can be given by himself only. As a low-privilege user without root access, you cant modify the sudoers file.