[Fixed] add user to sudoers file

In this article, we will fix a common error that new Linux users encounter username is not in sudoers file. The problem is related to user permissions and can be simply resolved with a single command to add user to sudoers file.

If you have recently created a new user on your Linux distribution, you may see the error “username is not in soders file” when using sudo. The error occurs because the logged-in user lacks the ability to execute commands as sudo.

If you’re in a rush and need to fix the issue without knowing the context of the error, here’s how. Please use any of the following methods to grant the user sudo access, depending on the Linux distribution you’re using.

Add user to sudoers file or sudo group

For Ubuntu or its derivatives

sudo usermod -aG sudo username

For CentOS or other RHEL’s derivatives

To have sudo privileges in CentOS and other Red Hat Enterprise Linux derivatives, the user must be assigned to the wheel group –

sudo usermod -aG wheel username

To grant sudo privileges to a user, you can directly modify the sudoers file and add the following line. This method is equally applicable to any other Linux distribution.

visudo

Add the following line at the end of the file –

username  ALL=(ALL) NOPASSWD:ALL

For Arch Linux and derivatives –

If you installed Arch Linux from the base image, sudo is most likely not pre-installed with the default system. To begin, install sudo

pacman -S sudo

Now, add the user to the wheel group and grant it sudo access.

usermod -aG wheel username

An extra step Arch necessitates is granting sudo access to users from all wheel group, which we accomplish by changing the sudoers file. So, using visudo, open the sudoers file.

visudo

Now find and uncomment the line “#%wheel ALL=(ALL) ALL". Just remove the # to uncomment it.

## Uncomment to allow members of group wheel to execute any command
%wheel ALL=(ALL) ALL

That’s how you give any user the ability to run commands with sudo. If you’re still reading, you’re probably asking why my specific Linux distribution doesn’t address the problem. The solution, of course, is in the context which I avoided explaining above. But now it’s here.

In Linux, sudo access is not granted by default to newly created users. It’s for your own safety. Yes, you guessed correctly. sudo can be used by a less trusted or untrusted user to edit system files. Only trusted users should be in the sudoers file, or the sudo and wheel group since sudo is a powerful command.

What is visudo?

The sudoers file can be found at /etc/sudoer. The visudo command is a tiny utility for editing the sudoers file. Although we can change the sudoers file directly, it is highly advised that we use visudo.

Before saving the file, visudo will examine the syntax of each line. If we save the sudoers file with the incorrect syntax, we may lose all sudo access. If there are mistakes, visudo will not allow anyone to save the file.

Conclusion

So that’s how to fix “username is not in sudoers file” error. We add user to sudoers file, and that’s about it. Finally, before concluding the post, I wanted to add a minor piece of information. Any user in any group can be added using the usermod command.