WebMasterCampus
WEB DEVELOPER Resources

Linux su Command

Learn Linux su Command with examples


Linux su Command

In Linux, we can use “su” command allows you to run commands with another user’s privileges, by default the root user.

Using su is the simplest way to switch to the administrative account in the current login session.

su Command Syntax

>> su [OPTIONS] [USER [ARGUMENT...]]

su Command Example

The default behavior of su is to run an interactive shell as root.

You will be prompted to enter the root password, and if authenticated, the user running the command temporarily becomes root.

>> su

su -l Command

su command with -, -l, –login makes the shell a login shell with an environment very similar to a real login and changes the current directory.

>> su -

>> su -l

>> su --login

su -s Command

If you want to run another shell instead of the one defined in the passwd file, use the -s, –shell option.

Let’s switch to root and to run the ksh Korn shell.

>> su -s /usr/bin/ksh

su -p Command

To preserve the entire environment (HOME, SHELL, USER, and LOGNAME) of the calling user, invoke the command with the -p, –preserve-environment option.

>> su -p

su -c Command

If you want to run a command as the substitute user without starting an interactive shell, use the -c, –command option.

Let’s invoke the ps command as root.

>> su -c ps

Important Note: 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.

Created with love and passion.