What is the difference between the su command and the sudo command in Linux?

  The su command and the sudo command are commonly used commands in Linux. sudo executes commands as the root user, and su is used to change the identity of other users to execute commands. So what is the difference between the su command and the sudo command in Linux? Let's explain it in detail through this article.

  1. About the password

  The biggest difference between the two commands is that the sudo command requires the password of the current user, and the su command requires the password of the root user. Obviously, the sudo command is better when it comes to security. For example, considering a multi-user host that requires root access, using the su command means sharing the root password with other users, which obviously reduces security.

  Also, if you want to revoke superuser/root access for a specific user, the only way to do that is to change the root password before telling all other users about the new root password.

  The use of the sudo command can handle the above two situations well. Since the sudo command requires the other user's own password, there is no need to share the root password. At the same time, if you want to prevent specific users from accessing root privileges, you only need to adjust the corresponding configuration of the sudoers file.

  2. Default behavior

  Another difference between the two commands is their default behavior. The sudo command only allows a single command to be run with elevated privileges. The su command starts a new shell while allowing as many commands as root to run until explicitly logged out.

  Therefore, the default behavior of the su command is risky, since users may forget that they are working as root, and inadvertently make some irreversible changes.

  3. Logging

  Although sudo commands execute commands as the target user, they use the username configured by sudoer to record who executed the command. The su command cannot directly track and record what operations the user performs after switching to the root user.

  4. Flexibility

  The sudo command is much more flexible than the su command. It can limit which commands sudo users can access. Users can only access the commands they need to work through the sudo command, while the su command allows users to have permission to do anything.

  5、sudo su

  Presumably because it is risky to use the su command or log in directly as the root user, some Linux distributions disable the root user account by default. However, it is still possible to successfully execute the su command without entering the password of the root user by running the following command: sudo su

  Since you run the command with sudo, you only need to enter the password of the current user. So, once it's done, the su command will run as root, which means it won't ask for any password again.

  If you want to enable the root user account in the system, you must manually set the root user password, you can use the following command: sudo passwd root.

Guess you like

Origin blog.csdn.net/oldboyedu1/article/details/132496369