Linux user account management - switching between users

Linux user account management - switching between users

In the Linux system, managing user accounts is very important. Among them, switching between users is a frequently used operation, which can switch to the identity of another account to perform related tasks. This article will introduce the method of switching between users in the Linux system, including syntax, practical operation, and the differences between various switching methods between users.

switch between users

In the Linux system, there are two commands for switching between users: su and sudo.

su command

The su command is the most basic user switching command, which can switch to other user accounts without exiting the current user. Usually, the su command will use the permissions of the root account or other privileged users to execute commands. The syntax of the su command is as follows:

su [选项] 用户名

Among them, 选项it represents the operating parameters of the su command, and the commonly used options are:

  • -: Switch to the specified user, and switch together with the user's working environment (including PATH variable, MAIL variable, etc.).
  • -lOr --login: switch the working environment completely while switching the user identity.
  • -pOr --preserve-environment: Indicates to switch to the identity of the specified user, but does not change the current working environment (the configuration file of the switched user is not used).
  • -c 命令: Only switch users to execute the command once, and automatically switch back after execution.

sudo command

The sudo command is used to temporarily authorize ordinary users to execute specific system commands, and can perform privileged operations without exiting the current user. The syntax of the sudo command is as follows:

sudo [选项] 命令

Among them, 选项it represents the operating parameters of the sudo command, and the commonly used options are:

  • -u 用户名: Specifies which user to switch to to execute the command.
  • -s: Indicates that the command is executed with the authority of the root user.
  • -iOr --login: Switch the environment variables completely while switching the user identity.

Switch between users

Next, we demonstrate the use of su and sudo commands through practical operations.

su command practice

  1. Use the su command to switch to the root account, and switch with the user's working environment:

    $ su -
    密码:
    
  2. Use the su command to switch to other accounts (such as lamp):

    $ su - lamp
    密码:
    
  3. Use the su command to automatically switch back after executing a command:

    $ su -c "ls /root"
    密码:
    

The difference between the options in the su command

Order illustrate
su user Execute commands as the specified user
are - Switch to root user and open a new terminal
su - user Switch to the specified user and open a new terminal

Note: Entering the su command without the user parameter in the terminal means switching to the root user identity.

su command syntax demonstration

su [options] [user]

Among them, optionsit represents the operating parameters of the su command, and the commonly used options are:

  • -: Switch to the specified user, and switch together with the user's working environment (including PATH variable, MAIL variable, etc.).
  • -lOr --login: switch the working environment completely while switching the user identity.
  • -c 命令: Only switch users to execute the command once, and automatically switch back after execution.

Note: When not specified in the command user, it will switch to the root account by default.

Practical examples of using the su command

Suppose the current user is user1, we need to switch to user2the account and execute a command, you can use the following command:

su -c "command" user2

Among them, commandindicates the command to be executed. user2In this way , the command can be executed as the user without exiting the current user .

Note: If the current user does not have sudopermission, you need to know the password of the target user to switch.

sudo command operation

  1. Use the sudo command to execute a command that requires privileges:

    $ sudo apt-get install nginx
    
  2. Use the sudo command to switch to the root account and completely switch the environment variables:

    $ sudo -i
    

The difference between switching methods between users

When using su and sudo commands, you need to pay attention to the difference between them.

The difference between su and sudo

  • The su command needs to know the password of the account to be switched, and the sudo command usually needs to enter the password of the current user.
  • The su command can realize user switching, and can also directly switch to the root user; while the sudo command can only be executed with the authorization of a privileged user.

The difference between su and su -

Order illustrate
su user Execute commands as the specified user
are - Switch to root user and open a new terminal
su - user Switch to the specified user and open a new terminal

Note: Entering the su command without the user parameter in the terminal means switching to the root user identity.

in conclusion

In daily Linux system management, switching between users is a very important operation. This article introduces two commands for switching between users in the Linux system: su and sudo, as well as the differences between them and the option parameters of each command. Master these commands and options proficiently, you can manage user accounts more efficiently in Linux system management.

Guess you like

Origin blog.csdn.net/m0_67268191/article/details/130790534