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

In Linux, suand  sudoare two commonly used commands for switching user identities and executing privileged commands. There are the following differences between them:

  1. suOrder:

    • suIt is the abbreviation of "Switch User", which is used to switch to the identity of other users.
    • By default, commands without any arguments  suswitch to  rootthe user.
    • To use  suthe command, you need to enter the password of the target user.
    • After switching, the environment variables, working directory and permissions will inherit the settings of the target user.
    • Switching to other users can be done by adding parameters, eg  su username.
  2. sudoOrder:

    • sudois an acronym for "Superuser Do" and is used to execute privileged commands as a superuser or other authorized user.
    • sudoAllows normal users to gain privileges without knowing the superuser password.
    • sudoWhen executing the command, the password of the current user is required.
    • sudoCommands allow administrators to grant specific users permission to execute privileged commands based on a configuration file.
    • sudoExecution of commands can be specified on the command line, eg  sudo command.

Main difference:

  • suWhen switching to other users, you need to know the password of the target user, and  sudowhen executing privileged commands, you need the password of the current user.
  • suAfter switching users, the entire session is conducted under the target user, and  sudoprivileges are only acquired when executing privileged commands.
  • sudoCommands can perform more fine-grained permission control on users, and administrators can flexibly configure which commands and which users can perform privileged operations.

It should be noted that sudothe commands need to be properly configured in the system to be used, and only users with the necessary permissions are granted to perform privileged operations.

Guess you like

Origin blog.csdn.net/tiansyun/article/details/131298389