This is the fifth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Linux system operation, as one of the technologies that must be mastered in back-end development, has become more and more a part of our work. Entering the workplace for the first time, contact with Linux, in the face of full screen operation commands, who will not miss the Windows interface, but slowly will find that the command line is still pretty cool, after all, we can “kaka kaka” to prove our full workload! Today I would like to share with you a small problem encountered by oneself knocking command, I hope you will not encounter. Linux, yyds!

Problem description

When you log in to the Linux operating system as a common user, you need the root permission to perform some operations. When you run the sudo su command to upgrade the user permission to root, an error message is displayed when you enter the password of a common user: “… The user is not in the sudoers file, it will be reported.”

The reasons causing

First, let’s look at the results of su and sudo commands:

  • sudo ..: sudo followed by the command indicates that the current user has the permission to run the command as root. The password of the current user needs to be verified. After the command is executed, the user goes back to the current user.The sudo command is executed only when the user has permissions in sudoers
  • sudo su: use sudo to override the su command.
  • su root: Switch to user root and verify the password of user root without changing the directory or environment variables
  • su: equivalent tosu root(By default, the user switches to the root user.)
  • su - root: Switch to user root, go to the corresponding directory of user root, and change environment variables
  • su xxx: switch to user XXX. You do not need to enter the password to switch to user root

Therefore, we can analyze the sudo su command. Sudo indicates that we need to upgrade the permission of the current user to the permission of the root user, and use the permission of the root user to execute the su command. We find that to execute sudo command, we need to write the permission of the current common user in the sudoers file. If the file does not have permissions for the current user at the time of execution, the message “… The user is not in the sudoers file and this will be reported.

The solution

Now that I have found the cause of the problem, I can take measures to solve it. As the saying goes, as long as my mind does not slide, there are more ways than problems. If the current user does not have permission to execute sudo, let’s add this permission to the user. The sudoers configuration file is stored in the /etc/sudoers directory on the Linux operating system.

  1. We start with commandssu rootTo switch to user root, you need to verify the password of user root
  2. And then we look at/etc/sudoersWhether the file contains the permission of the current user
  3. If no, run the commandvim /etc/sudoersEdit the file to add sudo permission for user Shone
    User root exists by default. Root ALL=(ALL:ALL) ALL ## Specifies the permission of the sudo command for user shone ALL=(ALL:ALL) ALLCopy the code
  4. Pay attention to/etc/sudoersThe file is a 440 read-only file. After editing the file content, the file cannot be saved and exit
    • chmod u+w /etc/sudoersTo add the write permission
    • chmod 740 /etc/sudoersAdd full permissions to user root (4+2+1=7)
    • After editing, run the command:wq!Force save changes
  5. Finally we use commandssu shoneSwitch to a common user and retest the sudo command. The permission of the sudo command has been added successfully

As a novice, in the usual work and study, we should keep curiosity, encounter problems not only to learning, also want to try to know the why, only thus can we accumulate at work, learning, and constantly progress, eventually one day we can really enter the field, rather than as a forever in wandering ghosts in the introduction.