English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Linux su command

Linux Command Manual

The Linux su command (full English name: switch user) is used to change the identity to another user, and in addition to root, the password of the user must be entered.

Permission to use: All users.

Syntax

su [-fmp] [-c command] [-s shell] [--help] [--version] [-] [USER [ARG]]

Parameter Description:

  • -f or --fast does not read the startup file (such as csh.cshrc, etc.), used only for csh or tcsh
  • -m -p or --preserve-environment does not change environment variables when executing su
  • -c command or --command=command changes the account to USER and executes the command (command) before returning to the original user
  • -s shell or --shell=shell specifies the shell to be executed (bash, csh, tcsh, etc.), the default value is /etc/passwd The shell of the user (USER) inside
  • --help Display Description File
  • --version Display Version Information
  • - -l or --The login parameter added makes it as if you are logging in as the user again, most environment variables (HOME, SHELL, USER, etc.) are based on the user (USER), and the working directory will also change. If USER is not specified, it defaults to root
  • USER The User Account to Change
  • ARG Pass New Shell Parameters

Online Examples

Change account to root and exit after executing ls command to return to the original user

su -c ls root

Change account to root and pass -f Parameter for the New Executed Shell

su root -f

Change account to clsung and change working directory to clsung's home directory (home dir)

su - clsung

Switch User

[email protected]:~$ whoami //Show Current User
hnlinux
[email protected]:~$ pwd //Show 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 //Show Current User
hnlinux
[email protected]:~$ pwd //Show Current Directory
/home/hnlinux
[email protected]:~$ su - root //Switch to root User
Password: 
[email protected]:/home/hnlinux# whoami 
root
[email protected]:/home/hnlinux# pwd //Show Current Directory
/root

Linux Command Manual