Switch Users on Linux with the su Command
Last Updated :
19 Jan, 2024
Switching users on a Linux system is a fundamental aspect of system administration, allowing users to perform tasks with different privileges. The 'su' command, short for "switch user" or "substitute user," is a powerful tool that facilitates this transition. This article will delve into the intricacies of the 'su' command, exploring its various options and providing practical examples to demonstrate its usage.
Understanding the su Command
In Unix-like operating systems, the 'su' command, short for "substitute user," offers a way to temporarily switch to a different user account within your current login session. This means you can adopt the privileges and permissions of the target user to perform specific tasks or access restricted files.
When you execute 'su,' it effectively replaces your current user ID with the ID of the specified account. By default, if you run 'su' without any arguments, it assumes you want to switch to the superuser account, also known as the "root" account. The root account holds the highest level of system privileges, granting nearly unrestricted access. However, 'su' can be used to switch to any user account on the system, provided you have the required permissions.
To successfully switch to another account using 'su,' you'll typically need to provide the password for that account. This authentication process ensures authorized access and maintains security.
Basic Syntax of su Command
The basic syntax of the 'su' command is as follows:
su [OPTION] [USER]
Here,
[OPTION] represents various command-line options.
[USER] is the username to which you want to switch. Let's explore some practical examples to understand how 'su' works in different scenarios.
Options Available in su Command
|
Simulate a full login shell for the target user.
|
Specify a command to be executed with the new user.
|
Define the shell to be used for the command.
|
Preserve the environment when switching users.
|
Run a command in a new session.
|
Preserve user environment variables.
|
Synonym for --login.
|
Display help information about the command.
|
Display version information.
|
How to Switch to the Root User by Using su Command
To switch to the root user, simply type:
sudo su

You will be prompted to enter the root user's password. Once authenticated, you will have superuser privileges until you exit the session.
How to Switch to the Specific User by Using su Command
To switch to a different user, specify the username as an argument:
su username
Replace 'username' with the actual username you want to switch to. You'll be prompted to enter the password for that user.
For example: If we want to switch to a user name "shivansh260" we will use the following command
su shivansh260

How to Execute a Command as Another User by Using su Command
You can use 'su' to execute a single command as another user without switching to their environment:
sudo su -c "command" -s /bin/bash username
Replace 'command' with the desired command and 'username' with the target user. The '-s /bin/bash' option specifies the shell to be used.
For example: If we want to print "hello" on the screen , and this command has to be run by a username "shivansh" we will use the following command
sudo su -c "echo hello" -s /bin/bash shivansh

How to Preserve Environment Variables while Switching User
When switching users, you might want to preserve the environment variables from the original user. Use the '-' option to achieve this:
sudo su -

How to Simulate a Login Shell
To simulate a full login shell for another user, use the '-l' or '--login' option:
su -l username
This ensures that the target user's environment is fully loaded, just like during a regular login.

This command switches to the root user while preserving the environment.
Switch Users with su Command - FAQs
How do I switch to the root user using the su command in Linux?
To switch to the root user, simply type su in the terminal and press Enter. You will be prompted to enter the root password. Once authenticated, you will have superuser privileges.
Can I switch to a specific user account other than root with su?
Yes, you can switch to a specific user by using the su command followed by the username. For example, to switch to the user 'john', type su john and enter the password when prompted.
What is the difference between su and su - in Linux?
The su - command (or su --login) simulates a full login shell for the target user, loading their environment variables. Without the dash, su retains the current environment. For example, su - username loads the user's environment, while su username retains the current environment.
How can I run a single command as another user without switching to their account?
You can use the -c option with su to execute a single command as another user. For instance, su -c "command" -s /bin/bash username will run the specified command as the user 'username'.
Is it possible to switch users in Linux and preserve the current environment variables?
Yes, you can preserve the environment variables when switching users by using the -m or -p option with su. For example, su - or su -m switches users while preserving the environment, ensuring a similar environment to the original user.
Conclusion
In this article we discussed the 'su' command in Linux which is essential for effective system administration, enabling users to seamlessly switch between accounts with varying privileges. This article has provided a comprehensive guide to the 'su' command, exploring its intricacies, syntax, and a range of options. From switching to the root user for system-wide tasks to executing specific commands as different users, the versatility of 'su' has been demonstrated through practical examples. Users have also been introduced to preserving environments, simulating login shells, and frequently asked questions to enhance their understanding. By following the outlined techniques and leveraging the various options available, Linux users can confidently navigate user switching scenarios for efficient system management.
Similar Reads
Switch Users on Linux with the su Command
Switching users on a Linux system is a fundamental aspect of system administration, allowing users to perform tasks with different privileges. The 'su' command, short for "switch user" or "substitute user," is a powerful tool that facilitates this transition. This article will delve into the intrica
6 min read
users command in Linux with Examples
users command in Linux system is used to show the user names of users currently logged in to the current host. It will display who is currently logged in according to FILE. If the FILE is not specified, use "/var/run/utmp". "/var/log/wtmp" as FILE is common. Syntaxusers [OPTION]... [FILE]where, OPT
2 min read
How to Use Linux Commands in Windows with Cygwin
Cygwin is a collection of tools that provide a Linux Operating System's terminal look, feel, and some of its basic functionality for users of Windows OS who want to have a go over the basic commands of Linux without worrying about installing a Linux OS. To use this tool, first of all, we need to dow
3 min read
stty command in Linux with Examples
stty command in Linux is used to change and print terminal line settings. This command shows or changes terminal characteristics. Syntaxstty [-F DEVICE | --file=DEVICE] [SETTING]...stty [-F DEVICE | --file=DEVICE] [-a|--all]stty [-F DEVICE | --file=DEVICE] [-g|--save]where, -F DEVICE or --file=DEVIC
2 min read
How to Update sudo Version on Linux
In the Linux Operating System, we use different administrative commands to control and manage the system. To perform the administrative work on the system, we use sudo as the main primary command. sudo in Linux is the command utility that allows us to execute the commands with superuser privileges.
6 min read
Reboot Linux System Command with Examples
Rebooting a Linux system is a fundamental administrative task necessary for applying updates, troubleshooting, or system maintenance. Various commands are available to reboot a Linux system, each with specific options and use cases. This guide explores essential reboot commands such as `reboot`, `sh
11 min read
usermod command in Linux with Examples
usermod command or modify user is a command in Linux that is used to change the properties of a user in Linux through the command line. After creating a user we have to sometimes change their attributes like password or login directory etc. so in order to do that we use the Usermod command. The info
4 min read
write command in Linux with Examples
The `write` command in Linux facilitates the users in sending the messages directly to other logged-in users' terminals. For using this command, try on to type `write username` and then type your message. After typing your message, press `Ctrl+D` to end the session. This command is useful for quick
6 min read
username Command in Linux With Examples
Linux as an operating system holds the capabilities of handling multiple users. So it is important to keep a check on the users and their related information in order to maintain the integrity and security of the system. Whenever a user is added its information is stored in the "/etc/passwd" file. I
3 min read
setsid command in Linux with Examples
setsid command in Linux system is used to run a program in a new session. The command will call the fork(2) if already a process group leader. Else, it will execute a program in the current process. The main advantage of using 'setsid' is that it allows programs to run independently of the terminal
3 min read