After logging into my Linux VPS via SSH, I ran a sudo command and got this error:
‘username’ is not in the sudoers file. This incident will be reported.
I understand this means the user lacks admin privileges, but I’m not sure how to fix it. Since I’m locked out of using sudo, what’s the correct way to grant my user access without reinstalling or wiping the server?
Any guidance on resolving the is not in the sudoers file issue—maybe via root or recovery access—would be appreciated.
If you have SSH access with another user that does have sudo rights (like root or a preconfigured admin), fixing the ‘is not in the sudoers file’ error is simple. Just switch to that user and run:
usermod -aG sudo yourusername
Or on some distros:
gpasswd -a yourusername sudo
Then log out and log back in with your original user. That should remove the error once you’re added to the sudo group. But, be careful not to typo the username, I’ve seen people lock themselves out that way!
Now, if no one has sudo access and root login is disabled (which is common for security), the ‘is not in the sudoers file’ problem can be a bit trickier. But don’t worry, your hosting provider likely has a recovery console or ‘rescue mode’ option. Here’s what to do:
- Boot into rescue mode via the hosting panel.
- Mount your root partition.
- Chroot into the root filesystem.
- Use
visudo
to edit the /etc/sudoers
file directly or manually add your user to the sudo group:
yourusername ALL=(ALL:ALL) ALL
Save the file, reboot, and you’re back in business. It’s a longer process, but this trick has saved me countless times!
Some distros, like CentOS or even Ubuntu cloud images, don’t give sudo rights to users created via cloud-init or manually. If that’s the case, you can still fix the ‘is not in the sudoers file’ issue by logging in as root (if password login is enabled).
SSH as root and open the sudoers file with:
visudo
Then insert your user into the appropriate spot. Make sure to use visudo
instead of editing the file directly, it checks for syntax errors to avoid accidentally breaking sudo entirely. This issue often crops up after provisioning a fresh VPS where the defaults don’t match your expectations.