Explain the error problem of “… Is not in the sudoers file” in Linux

Time:2022-6-20

Sudo is a command that allows a specific user group to run with the privileges of another user (typically root). Sudo has detailed logging functions and provides fine-grained control over which commands users can run through sudo.
Sudo vs. Su

The Su command also provides the same privilege promotion function. The difference between them is the granularity of their authentication process and privilege changes. Su allows you to switch from your login session to another user’s session, and then you can run any program with the user’s privileges at will, but you need to know the password of the target user to switch this user. Sudo, on the other hand, works on a single command basis, allowing you to run a single command with root privileges. You don’t have to know the root password to use sudo, but you should enter your password when prompted to enter the sudo password.
Add users to sudoers list

As a new user, if you try to run the sudo command, you will encounter the following error. This means that you are not in the sudoers list of a user group that has been authenticated to use sudo privileges.

   

Copy code

The codes are as follows:

[my-user-id] is not in the sudoers file. This incident will be reported.

There are two ways to add you to the sudoers list.
Method 1

The first method is to add you to the Linux user group named sudo. This particular Linux user group is preconfigured to use sudo. Therefore, once you are in this group, you can run the sudo command.

The following command will add you to the sudo group of Linux. You need to run this command under the root user.

   

Copy code

The codes are as follows:

#AddUser < user name > sudo

Now let’s confirm whether your group membership has been updated. Use the groups command to see which group you currently belong to in the list. This list must contain sudo groups.

   

Copy code

The codes are as follows:

$ groups </p>
<p> alice adm cdrom sudo dip plugdev fuse lpadmin netdev sambashare davfs2 libvirtd docker promiscuous

Membership changes (and sudo access) will take effect after you log out and log in again.
Method II

The second way you can use sudo is to add yourself directly to the /etc/sudoers configuration file.

To modify the /etc/sudoers file, you can use a special sudo editor command called visudo. Simply invoke the following command as root.

   

Copy code

The codes are as follows:

# visudo

This command can open and edit the /etc/sudoers file, add the following line to the end of the file, and press ctrl+x. When prompted, save your changes and exit.

   

Copy code

The codes are as follows:

<username> ALL=(ALL) ALL

This change will take effect immediately, and you can use sudo immediately.
201569160819831.jpg (800×456)