How to use su command on Ubuntu Linux with examples

This brief tutorial shows students and new users how to use the su command on Ubuntu 18.04 LTS.

The su command, which is short for substitute user or switch user, enables the current user to act as another user during the current login session.

If you’re a student or new user looking for a Linux system to start learning on, the easiest place to start is Ubuntu Linux OS. It’s a great Linux operating system for beginners.

Ubuntu is an open-source Linux operating systems that runs on desktops, laptops, server and other devices.

Both Ubuntu and Windows systems allow you to be productive, easy to use, reliable and enable you to install and run thousands of programs from gaming to productivity suite software for individuals and businesses.

About su command:

The su command, which is short for substitute user or switch user, enables the current user to act as another user during the current login session. It changes the current user ID to that of a superuser or another user that you specified.

Syntax:

The syntax is the rule and format of how the su command can be used. These syntax options can be reordered, but a straight format must be followed.,.

Below is an example syntax of how to use the su command.

su [options] [LOGIN]

Options:

The command line options are switches or flags that determined how the commands are executed or controlled. they modify the behavior of the commands. they are separated by spaces and followed after the commands.

Below are some options of the su command:

   LOGIN. Replace LOGIN.. with the username or login name you want to switch to.
-c, –command COMMAND
Use the -c or –command to pass COMMAND to the invoked shell
-, -l, –login Use the or -l or –login to make the shell a login shell. This provides an environment similar to the user session
-s, –shell SHELL Use the -s or –shell to specify a SHELL instead of the default in passwd
-h, –help Use the -h or –help to display help message
-m, -p,
–preserve-environment
Use the -m or -p  or –preserve-environment to not reset environment variables, and
keep the same shell
–help Display a help message and exit.

Examples:

Below are some examples of how to run and use the su on Ubuntu Linux.

Simply run the su command to invoke it.

If you run the su command but don’t specify a login name, the command automatically switches to the superuser or root and run its interactive shell.

su

When you run the su command above, you’ll be prompted for the root password. If the root password is authenticated and validated, the user running the command will automatically becomes root.

By default, authentication will fail when you want to change to root since the root account doesn’t have a password created with you install Ubuntu.

To add a password to the root account so you can authenticate, run the commands below

sudo passwd root

When you that, you’ll be prompted for your password. then continue with asking to create and re-type a new password for the root account.

Output:
[sudo] password for richard:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

You’ll now be able to use the su command and temporary becomes root.

You can also run the su command with or -l or –login. This makes the mimic the shell environment similar to a real login, in this case the root.

su -

To run another SHELL instead of the one defined by default in the passwd file, use the -s or –shell option . The below example will use the /usr/bin/zsh shell.

su -s /usr/bin/zsh

When you use su with -c or –command option, it causes the next argument to be treated as a command by most command interpreters.

su -c ls

The su command acts almost similar to the sudo command. However, sudo command allows user to execute programs with root privileges.

On the other hand, the su command give temporary root shell to users. But users must know the root password. This could be dangerous when sharing root password among users to gain root privileges.

In most cases, sudo is used.

When you run su with the –help option, you’ll see the help text below:

Usage: su [options] [LOGIN]

Options:
  -c, --command COMMAND         pass COMMAND to the invoked shell
  -h, --help                    display this help message and exit
  -, -l, --login                make the shell a login shell
  -m, -p,
  --preserve-environment        do not reset environment variables, and
                                keep the same shell
  -s, --shell SHELL             use SHELL instead of the default in passwd

That’s it!

Congratulations! You’ve learned how to use the su command on Ubuntu.