Employing the su command in Linux, you can seamlessly run commands as a different user. This command-line utility is one of the easiest ways to change the administrative account associated with the current logged-in session.
The su command or the switch user command refers to a utility that allows users to switch and temporarily act as root users. It eventually helps in bypassing all the usual restrictions and performing different tasks as different users easily.
How is the sudo Command different from the su Command?
While the sudo command in Linux is all about granting one-time access to the root privileges, the su command helps switch the accounts altogether.
While the former is used to run some of the administrative commands quickly, the latter helps non-root users assign desired usernames and run commands as them. The su commands can also change different shell interpreters.
Although the origin is older as compared to sudo, the su command is more functional. The element that makes it more amazing is the ability to duplicate the features of sudo by simply using the
-c flag and passing commands to the shell.
Using su Command in Linux
With the help of the su command in Linux, you can become a temporary user by switching the current administrative account. Before going into the deep analysis, let’s walk through the working syntax of the su command.
su Command in Linux: Syntax
The basic syntax of the su command in Linux looks like this:
su [OPTIONS] [USER [ARGUMENT…]]
When the command is executed without providing any option, it usually runs an interactive shell as the temporary root. After running the command, you’ll come across a password prompt for further authentication.
In other words, when anyone runs
$ su, they will need to authenticate the same with the system password and become a temporary root. It is crucial to keep in mind that the variables
HOME are defined from the entry made by the user in
Before you start using the su command in Linux, make sure to verify if the user is changed. Doing this is pretty simple, just run the
$ whoami command and check the user’s name in the output.
su Command in Linux: OPTIONS
From the long lists of options that can be used with the su command in Linux, here is what the most common ones intend to do. To access the full list, use the
$ su -h command.
Username: This option alters the username with the actual username that you want to log in with. The username under focus can be any user, root, or non-root.
–command [command]: The option helps specify any desired command as the designated user.
–login [username]: With this option, you can easily execute a login script and bring changes to any specific username. Don’t forget, inputting a password for that user is also necessary.
–s or –
shell [shell]: Using this option helps you specify a different shell environment to run your commands in.
–help: The option displays the help file for the su command in Linux.
–p or ––preserve–environment: Those who’re looking to preserve the shell environment like the
USER can use this option.
Making Login Shell’s Environment Similar to Real Login
su Command in Linux, when used with the
-l flag or
--login flag, transforms the login shell environment to something more familiar to the actual login scenario alongside bringing alterations in the current directory.
Run the command in the following format:
$ su -l [other_user]
Enter the password to authenticate.
Using su Command in Linux to Run any Other Command as a Different User
Employing su command together with the
-c flag helps run any command as a different user.
Run the command as:
$ su -c [command] [other_user]
Move forward by inputting the system password.
su Command in Linux for Using a Different Shell
In case you’re willing to use a different shell altogether or an operating environment that is not similar to the real login, run the following command:
$ su -s /usr/bin/zsh
Keeping the Environment Same for Different User
You can use the su commands in Linux with the
-p option and save the working environment of the current user. What this will do is keep the environment the same while assigning a different user.
The environment we’re talking about refers to the home directory. This practice is beneficial when you need to run any command as a different user while having access to the current user’s data simultaneously.
$ su -p [other_user]
Verifying if the directory has remained the same or not is very simple. All that you need to do is run the following command:
$ echo $HOME
This is how su commands in Linux can be used. The utility is essentially practiced for changing user accounts on a momentary basis, and the article walks you through everything in detail.
The article also talks about the factors that differentiate su from the sudo command line. In other words, reading along will help you get more idea about employing su functions and aid place su and sudo commands on distinct boxes.
If you still have any doubts, feel free to leave a comment below.
If this guide helped you, please share it.