1. Preface

This article is mainly about explanation and demonstration Linux In the system su Command usage and common usage .

Linux su Use case of command

Linux su Use case of command

su Commands are also called switch user, Switch users . This command is used to switch the user's identity to another user during the login session . When no switch target user name is specified ,su Default switch to root user . Normally ,su The command is used to switch the currently logged in user to another user without exiting the system .

This is a Linux Commands often used by end users . This tutorial will help you understand through use cases Linux su command reference .

grammar :

su [OPTIONS] [USER] [OPTIONAL ARGS...]

2. `su` Command usage example

Let's start with a simple demonstration . I use zcwyou The user login Linux System , Use whoami The command verifies the identity . And then use su Command switch to root user . Verified the identity again .

whoami

zcwyou

Switch to root user

su - root

Re execution whoami

whoami

Output results :

root

Linux su Use case of command

su Command usage example

Supplementary knowledge :

  • If you don't specify a user name ,su By default, it switches to root.
  • Use hyphens (-) Switch environment variables .

sudo Privileged users can also be in sudo with su command . Such as :

sudo su - root

Linux su Use case of command

su Command switch user

occasionally , You may just need to switch users to run one or more commands . under these circumstances ,su The order provides -c Options , You can call another user to run the command , Without actually switching shell.

Try this command :
Execute the following command on a regular account :

su - root -c "whoami"

Input root After the password , Output the following results

root

su Command line options :
Linux su Command options are few but useful . These options are in use shell Scripts are very useful for automating tasks .

  • 1. -c: To execute a command as a user .
  • 2. - -l, --login: Login user account , Use the new user's environment variables .
  • 3. -s, --shell: Appoint shell, Do not press /etc/passwd The default... Specified in the file shell perform .
  • 4. -m, -p, --preserve-environment: Keep existing environment variables , This is useful for temporarily borrowing other users to execute commands , Very useful .

Linux su Use case of command

su Command common options

3. Conclusion

Through this tutorial , You should know that Linux su The use of command and common usage ? Actually su The command function is simple , It's used to switch user identities . If you have any questions , Please leave a message .

If you like this article , Welcome to forward . This article has been synchronized to the blog site , Respect for the original , Please attach the following link to the text when reprinting :
https://www.linuxrumen.com/cyml/1877.html