Site icon DesignLinux

Su Command in Linux (Switch User)

Su Command in Linux (Switch User)

The 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 su Command

The following is the basic syntax for the su command:

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

When the su command is run without any arguments it will run as interactive shell as root:

su

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.

Execute the whoami command to confirm that user is changed:

whoami

It will print the name of the user running the current shell session:

root

With su the commonly used options are -, -l and --login. By using this shell converted to a login shell with environment same as real login and changed to the current directory.

su -

Use the -s, --shell option to run another shell session instead of the one defined in the passwd file.

su -s /usr/bin/zsh

You can use the -p, --preserve-environment option to preserve the entire environment (HOME, SHELL, USER, and LOGNAME) of the calling user.

su -p

When the - option is used -p is ignored.

Use the -c, --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:

su tecnstuff

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 -

Using 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

sudo -i is basically the same as running su -.

The root password doesn’t need to be shared among multiple administrative user accounts while using sudo, this is the main advantage over su.

With sudo, you also can allow users to run only specific programs with root privileges.

Conclusion

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.

Exit mobile version