The Linux Newbie Guide  ⇒    Fundamentals     Advanced     Supplement   Command Index   ENG⇒中
All rights reserved, please indicate the source when citing
  

su - Switch User







The su command, short for super user, primarily allows regular users to temporarily switch to the superuser (root) or any other specified user account.

The basic syntax of the su command is:
su [OPTIONS] [USERNAME] .

If the "USERNAME" is omitted, it defaults to switching to the root user.

Some common options for the su command include:

When you use the su command to switch to another user, several environment variables typically change:

It's important to note that if you use the su command without any options, it won't simulate a complete login session, and some environment variables may not change. To simulate a full login session, you can use the "-" or "--login" option, which causes the su command to act as if you are logging in as the new user, including reading their relevant configuration files.

Example:
$ su root ←Switch to the "root" user without the `-` option (environment remains unchanged)
Password: ←Enter the root user's password
# exit ←Return to the original user
$
$ su -c 'rm fileA' - l john ←Switch to the "john" user and run the command 'rm fileA' as "john"