
su Command in Linux
The su command in Linux is a key tool that lets people change between different user accounts on a computer system. The term "su" stands for "substitute user" or "switch user." It is useful for changing which user is active during the time you are logged in.
By using the "su" command, users can manage resources, configure settings, and troubleshoot problems effectively.
Table of Contents
Here is a comprehensive guide to the options available with the su command −
What is su Command in Linux
The su command lets you change from your current user account to a different one. For instance, you can move from your personal user account to the root account or any other account on the computer system.
When you run the su command without options, it will ask for the password of the account you want to access before allowing the change. This ability to switch users is important when you need entry to permissions and resources that are only available to specific accounts, like the root user account, which has higher access rights.
Syntax of su Command
The basic syntax of the su command is −
su [OPTIONS] [USERNAME]
Here −
- OPTIONS − Flags to customize the behavior of the su command.
- USERNAME − The account you want to switch to. If no username is provided, it defaults to the root user.
su Command Options
The following are a few different options that can be used with the su command −
Option | Description |
---|---|
-m, -p, --preserve-environment | Allows you to keep the current user’s environment variables when switching to a different user. |
-w, --whitelist-environment <list> | Specifies a list of environment variables that should not be reset when switching users, even if the rest of the environment is changed. |
-g, --group <group> | Allows you to specify the primary group that should be used when switching to another user. |
-G, --supp-group <group> | Specifies additional groups that the switched user should belong to during the session. |
-, -l, --login | Starts a complete login shell session for the target user, loading all their environment variables, shell configurations, and login scripts. |
-c, --command <command> | Runs a single command as the target user without starting an interactive shell session. |
--session-command <command> | Similar to -c, but does not create a new session when running the command, keeping the current session context intact. |
-f, --fast | Passes the -f option to the shell, which speeds up the initialization process for csh or tcsh by skipping certain startup configurations. |
-s, --shell <shell> | Runs the specified shell instead of the user’s default shell, provided it is listed in /etc/shells. |
-P, --pty | Opens a new pseudo-terminal for the session, enhancing compatibility with certain applications. |
-h, --help | Displays a help menu with explanations of all available options for the su command. |
>Examples of su Command in Linux
Let's explore a few practical scenarios of the su command on Linux system −
- Switching to the Root User
- Switching to Another User
- Running Commands as a Specific User
- Loading a Fresh User Environment
- Switching Shells Changing Users
Switching to the Root User
The su command is commonly used to switch to the root account, allowing you to perform administrative tasks.
su
When you run this command, the system prompts you for the root password. Once authenticated, your session switches to the root user, granting you full administrative privileges.
Now, you can execute critical tasks such as installing software, modifying system files, and changing configurations.

Switching to Another User
To switch to a different user account, specify the username as an argument.
su linux
The system will ask for linuxâs password. After successful authentication, the session switches to linux, meaning you now have access to their files, environment, and permissions.

Running Commands as a Specific User
Instead of switching entirely, you can execute a command as another user without fully changing accounts.
su -c "ls /home/linux" linux
This temporarily switches to user linux just to execute the ls /home/linux command. The session ends after running the command, and you return to your original user.

Loading a Fresh User Environment
To fully load the new user's environment, use the - option.
su - linux
This creates a fresh shell session for linux, loading their environment variables, configurations, and shell settings. It ensures you're working within their intended setup, which may differ from yours.

Switching Shells When Changing Users
If a user has a different default shell, you can specify another shell while switching.
su -s /bin/bash linux
Normally, su switches users using their default shell. The -s option forces a specific shell (in this case, /bin/bash) while operating as Linux. This is useful if the user's default shell is something different, like /bin/sh or /usr/bin/zsh.

Conclusion
The su command is an important tool in Linux systems. It helps you switch users and gain administrative access. You can do many things with it, like perform system tasks, test user settings, or run commands as another user. The su command gives you the ability to control and manage the system effectively.
By learning the different options and practicing with examples, you can use the su command well in various situations. Knowing how to use this command is very important for system administrators and experienced users. They often need to manage permissions, test access rights, or solve problems in systems used by many people.