3. The su (short for substitute or switch user) utility allows you to run commands with the privileges of
another user, by default the root user.
Using su is the simplest way to switch to the administrative account in the current login session.
This is especially handy when the root user is not allowed to log in to the system through ssh or
using the GUI display manager.
In this tutorial, we will explain how to use the su command.
4. How to Use the su Command
The general syntax for the su command is as follows:
su [OPTIONS] [USER [ARGUMENT...]]
When invoked without any option, the default behavior of su is to run an interactive shell as root:
$ su
You will be prompted to enter the root password, and if authenticated the user running the command temporarily becomes
root.
5. 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.
To confirm that the user is changed, use the whoami command:
whoami
The command will print the name of the user running the current shell session:
root
6. The most commonly used option when invoking su is -, -l, --login. This makes the shell a login shell with an
environment very similar to a real login and changes the current directory:
su -
If you want to run another shell instead of the one defined in the passwd file, use the -s, --shell option. For
example, to switch to root and to run the zsh shell you would type:
su -s /usr/bin/zsh
To preserve the entire environment (HOME, SHELL, USER, and LOGNAME) of the calling user use the -p, --preserve-
environment option.
7. On some Linux distributions like Ubuntu, the root user account is disabled by default for security reasons.
This means that no password is set for root 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 -
The sudo command allows you to run programs as another user, by default the root user.
If the user is granted with sudo assess 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.
8. When used with the -i option, sudo run an interactive login shell with the root user’s environment:
sudo -i
sudo -i is basically the same as running su -.
The advantage of using sudo over su is that the root password doesn’t need to be shared among
multiple administrative user accounts.
Also, with sudo you can allow users to run only specific programs with root privileges.
9. meenu101@meenu101-VirtualBox: - $ useradd user1
Useradd: Permission Denied
Useradd : cannot lock /etc/passwd: try again later;
meenu101@meenu101-VirtualBox:- $ sudo -s
[sudo] password for meenu101:
root@meenu101- VirtualBox : - # passwd
Enter new LINUX Password :
Retype new LINUX Password:
passwd: password updated successfully
root@meenu101- VirtualBox : - # exit
Exit
meenu101@meenu101-VirtualBox: - $ su
Password:
root@meenu101- VirtualBox : /home/meenu101# exit
Exit
meenu101@meenu101-VirtualBox: - $ sudo - i
root@meenu101- VirtualBox : /home/meenu101#
root@meenu101- VirtualBox : /home/meenu101# exit
Logout
meenu101@meenu101-VirtualBox: - $ sudo su
root@meenu101- VirtualBox : /home/meenu101#
root@meenu101- VirtualBox : /home/meenu101# exit
Exit
10. meenu101@meenu101-VirtualBox: - $ sudo passwd root
Enter new LINUX Password :
Retype new LINUX Password:
passwd: password updated successfully
meenu101@meenu101-VirtualBox: - $ su
Password:
root@meenu101- VirtualBox : /home/meenu101#
root@meenu101- VirtualBox : /home/meenu101# exit
Exit
meenu101@meenu101-VirtualBox: - $ sudo -k
meenu101@meenu101-VirtualBox: - $ sudo user
[sudo] password for meenu101:
root@user- VirtualBox : - # exit
Exit
meenu101@meenu101-VirtualBox: - $ cd /etc
meenu101@meenu101-VirtualBox: /etc $ Vim sudoers
meenu101@meenu101-VirtualBox:
11. Conclusion
su is a command-line utility that allows you to temporarily become another user and
execute commands with the substitute user.