The su
(short for “substitute user” or “switch user”) command allows you to switch from your current user account to another user account without logging out. This command is commonly used to switch to the root account to perform administrative tasks that require elevated privileges.
Initial Example
To switch to another user, you simply use:
su username
SU Parameters
Parameter | Description |
---|---|
-c, --command=command | Pass the specified command to the shell with the -c option. |
-f, --fast | Pass -f to the shell, which may affect shell behavior depending on the shell used. |
-g, --group=group | Specify the primary group. Available only to the root user. |
-G, --supp-group=group | Specify a supplementary group. Available only to the root user. Acts as primary group if --group is not specified. |
-, -l, --login | Start the shell as a login shell, simulating a real login. |
-m, -p, --preserve-environment | Preserve the entire environment, including variables like HOME, SHELL, USER, and LOGNAME, unless --login is specified. |
-P, --pty | Create a pseudo-terminal for the session, enhancing security by isolating the terminal. |
-s, --shell=shell | Run the specified shell instead of the default. |
--session-command=command | Pass the specified command without creating a new session (not recommended). |
-T, --no-pty | Do not create a pseudo-terminal, opposite of --pty . |
-w, --whitelist-environment=list | Preserve the specified environment variables when using --login . |
-h, --help | Display help text and exit. |
-V, --version | Print version information and exit. |
Examples
1. Switching to the Root User
To switch to the root user, just use su
and provide the root password:
su
2. Switching to Another User
To switch to a different user, use su
followed by the username and provide the password for that user:
su username
3. Running a Command as Another User
You can also use su
to run a specific command as another user. The -c
option allows you to pass a command that should be executed by the specified user:
su -c "command_to_run" username
For example, to run ls
as user john
:
su -c "ls" john
4. Preserving the Environment
By default, su
does not change the current environment variables. To simulate a full login and apply the target user’s environment settings, use the -l
or --login
option:
su -l username
5. Using su
with Shells
You can specify the shell to use when switching users. For example, to use the Bash shell:
su -s /bin/bash username
6. Exiting su
Session
To return to your original user after using su
, simply type exit
or press Ctrl+D
:
exit