Linux su Command
The Linux su (full spelling in English: switch user) command is used to change to the identity of another user, except for root, the password of that user needs to be entered.
Usage permission: All users.
Syntax
su [-fmp] [-c command] [-s shell] [--help] [--version] [-] [USER [ARG]]
Parameter Description:
-f or --fast Do not read startup files (such as csh.cshrc, etc.), only for csh or tcsh
-m -p or --preserve-environment Do not change the environment variables when executing su
-c command or --command=command Change to the user account USER and execute the command (command) before changing back to the original user
-s shell or --shell=shell Specify the shell to execute (bash csh tcsh, etc.), the default value is the user's (USER) shell in /etc/passwd
--help Display help documentation
--version Display version information
- -l or --login With this parameter, it is as if you are logging in again as that user, most environment variables (HOME SHELL USER, etc.) are based on that user (USER), and the working directory will also change. If USER is not specified, the default is root
USER The user account to change to
ARG Pass new shell parameters
Examples
Change the account to root and execute the ls command before exiting and switching back to the original usersu -c ls root
Change the account to root and pass the -f parameter to the newly executed shell
su root -f
Change the account to clsung and change the working directory to clsung's home directory
su - clsung
Switch user
[email protected]:~$ whoami //Display current user
hnlinux
[email protected]:~$ pwd //Display current directory
/home/hnlinux
[email protected]:~$ su root //Switch to root user
Password:
[email protected]:/home/hnlinux# whoami
root
[email protected]:/home/hnlinux# pwd
/home/hnlinux
Switch user, change environment variables
[email protected]:~$ whoami //Display current user
hnlinux
[email protected]:~$ pwd //Display current directory
/home/hnlinux
[email protected]:~$ su - root //Switch to root user
Password:
[email protected]:/home/hnlinux# whoami
root
[email protected]:/home/hnlinux# pwd //Display current directory
/root