In Linux, su
and sudo
are two commonly used commands for switching user identities and executing privileged commands. There are the following differences between them:
-
su
Order:su
It is the abbreviation of "Switch User", which is used to switch to the identity of other users.- By default, commands without any arguments
su
switch toroot
the user. - To use
su
the 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
.
-
sudo
Order:sudo
is an acronym for "Superuser Do" and is used to execute privileged commands as a superuser or other authorized user.sudo
Allows normal users to gain privileges without knowing the superuser password.sudo
When executing the command, the password of the current user is required.sudo
Commands allow administrators to grant specific users permission to execute privileged commands based on a configuration file.sudo
Execution of commands can be specified on the command line, egsudo command
.
Main difference:
su
When switching to other users, you need to know the password of the target user, andsudo
when executing privileged commands, you need the password of the current user.su
After switching users, the entire session is conducted under the target user, andsudo
privileges are only acquired when executing privileged commands.sudo
Commands 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 sudo
the 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.