su command is used to run commands as a another user. It is simplest way to switch to the administrative account in the current login session. It is very useful when the root user is not allowed to log in to the system through ssh. For example, in Ubuntu, root user is disabled by default to make more secure. This tutorial explains how to use su command.
How to Use the
The following is the basic syntax for the su command:
su [OPTIONS] [USER [ARGUMENT...]]
su command is run without any arguments it will run as interactive shell as
It will prompt you to enter root password, and if successfully authenticated then it will currently logged in user becomes temporarily.
The session shell (
SHELL) and home (
HOME) environment variables are set from substitute user’s
/etc/passwd entry, and the current directory is not changed.
whoami command to confirm that user is changed:
It will print the name of the user running the current shell session:
su the commonly used options are
--login. By using this shell converted to a login shell with environment same as real login and changed to the current directory.
--shell option to run another shell session instead of the one defined in the
su -s /usr/bin/zsh
You can use the
--preserve-environment option to preserve the entire environment (
LOGNAME) of the calling user.
- option is used
-p is ignored.
--command option, to run a command as substitute user without starting an interactive shell. For instance, to invoke the
ps command as root you would type:
su -c ps
You can switch to another user by passing username as an arguments to
su. For example, to switch to the user
tecnstuff you would type:
Sudo vs Su
As we seen above that Linux systems like Ubuntu, the root user account is disabled by default to increase the security. So no password is set for root user and you cannot use
su to switch to root.
One option to change to root would be to prepend the
su command with
sudo and enter the currently logged in user password:
sudo su -
sudo you can run the programs as another user and default is the root user.
If the user is granted with
sudo access the
su command will be invoked as root. Running
sudo su - and using the user password is the same as running
su - using the root password.
If the sudo used with the
-i option, it will run an interactive login shell with the root user’s environment:
sudo -i is basically the same as running
The root password doesn’t need to be shared among multiple administrative user accounts while using
sudo, this is the main advantage over
With sudo, you also can allow users to run only specific programs with root privileges.
The su is a command-line utility and used to run commands as a another user.
If you have any questions or feedback, please leave a comment below.