Linux – su Command

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.



Tutorials dojo strip

Initial Example

To switch to another user, you simply use:

su username




SU Parameters

ParameterDescription
-c, --command=commandPass the specified command to the shell with the -c option.
-f, --fastPass -f to the shell, which may affect shell behavior depending on the shell used.
-g, --group=groupSpecify the primary group. Available only to the root user.
-G, --supp-group=groupSpecify a supplementary group. Available only to the root user. Acts as primary group if --group is not specified.
-, -l, --loginStart the shell as a login shell, simulating a real login.
-m, -p, --preserve-environmentPreserve the entire environment, including variables like HOME, SHELL, USER, and LOGNAME, unless --login is specified.
-P, --ptyCreate a pseudo-terminal for the session, enhancing security by isolating the terminal.
-s, --shell=shellRun the specified shell instead of the default.
--session-command=commandPass the specified command without creating a new session (not recommended).
-T, --no-ptyDo not create a pseudo-terminal, opposite of --pty.
-w, --whitelist-environment=listPreserve the specified environment variables when using --login.
-h, --helpDisplay help text and exit.
-V, --versionPrint 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




Linux Playground

Scroll to Top