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:
- -c: Execute the specified command and return to the original user's identity.
- -f: Do not read the system-wide startup files and the user's personal shell initialization files.
- -m: Preserve the environment variables of the original user.
- -s: Run the specified shell.
When you use the su command to switch to another user, several environment variables typically change:
- HOME environment variable: This is set to the new user's home directory, meaning your current working directory becomes the new user's home directory.
- USER or LOGNAME environment variables: These are changed to the new user's name.
- SHELL environment variable: This usually remains unchanged unless you use the "-" or "--login" option to simulate a full login session, in which case the new user's default shell is used.
- PATH environment variable: The PATH variable may change based on the new user's configuration, potentially altering the search path for executable commands.
- Other environment variables: Other user-specific environment variables may change based on the user's configuration files (e.g., ".bashrc"," .profile", etc.).
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"
|