Linux Command - su

su - Switch shell to another user.

root is a Linux user that has permission to do anything on the system. You can switch to root user using su command.

Example

Switch to root

You can use su command to switch to the root user. You will need to enter the root password if password is set. If root haven’t had a password yet, then use passwd command to set the password.

1
2
3
4
5
$ su
# passwd
Enter new UNIX password:
Retry new UNIX password:
passwd: password updated successfully

After you set the password, exit and execute su or su root command to switch to root user.
When you run su without specifying the username, su will switch to root user.

1
2
3
4
$ su
password:
# whoami
root

Note that if you don’t have root password, you can still execute commands using sudo if you are in sudo group. You can even become root by running sudo -i:

1
2
3
4
$ sudo -i
[sudo] password for jimmy:
# whoami
root

su with dash

When you run su with a dash, you switch to root user and use new environment settings. If you don’t include the dash, the shell preserves the current user shell environment settings. Shell environment includes environment variables and working directory.

1
2
3
4
$ su -
Password:
# pwd
/root

switch to other user

use su username to switch to other user. You will need to enter the user’s password

For example, switch to user jimmy by running su jimmy and enter jimmy’s password.

1
su jimmy

Reference