If you're seeing the error username is not in the sudoers file, it means that your user doesn't have permission to use sudo. Here's what to do:
### Method 1: Using visudo (recommended)
1. Log in as root
Since you can't use sudo yet, switch to root: su -
2. Edit the sudoers file
Use visudo to safely edit the sudoers file: visudo
3. Grant sudo access to your user
Add the following line at the end of the file (replace your_username with your actual username):
your_username ALL=(ALL:ALL) ALL
4. Save and exit
- Press Ctrl + X to exit.
- Press Y to confirm saving.
- Press Enter to save the changes.
5. Exit root session
Type exit to leave the root session.
6. Test sudo access
Now, try using sudo to test:
sudo ls
### Method 2: Adding User to the sudo Group
1. Log in as root
If you're still logged in as root, continue, or use su to become root again:
su -
2. Add your user to the sudo group
Run this command (replace your_username with your actual username):
usermod -aG sudo your_username
3. Exit root session
Type exit to return to your regular user session.
4. Refresh your session
Instead of restarting, you can apply the new group membership by running:
newgrp sudo
5. Test sudo access
Try running a command with sudo to verify the change:
sudo ls