How to use su command on Ubuntu Linux with examples

|

|

This tutorial provides a guide on using the ‘su’ command on Ubuntu 18.04 LTS, enabling the user to temporarily switch roles for the login session. Ubuntu Linux OS is recommended for beginners, and ‘su’ is suitable for desktops, laptops, servers, and other devices. The command can be tricky when switching to root privileges; therefore, sudo…

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

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

The su command on Ubuntu Linux is a powerful tool that enables the current user to act as another user during the login session. It is especially useful for system administrators who need to perform tasks as a superuser or as another user with elevated privileges.

Learning how to use the su command on Ubuntu Linux can help users gain more control over their system and perform tasks that require elevated privileges.

Additionally, it is a fundamental concept in Linux administration and can serve as a stepping stone for learning more advanced Linux concepts.

About su command:

The su command, short for substitute user or switches user, enables the current user to act as another user during the login session. It changes the current user ID to that of a superuser or another user 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 determine 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 for 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, –loginUse the or -l or –login to make the shell a login shell. This provides an environment similar to the user session
-s, –shell SHELLUse the -s or –shell to specify a SHELL instead of the default in passwd
-h, –helpUse the -h or –help to display the help message
-m, -p,
–preserve-environment
Use the -m or -p  or –preserve-environment to not reset environment variables, and
keep the same shell
–helpDisplay 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 runs 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 become root.

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

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

sudo passwd root

When you do that, you’ll be prompted for your password. Then, continue 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 can now use the su command, which temporarily 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

Using su with -c or –command option 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, the sudo command allows users to execute programs with root privileges.

On the other hand, the su command gives a temporary root shell to users. But users must know the root password. This could be dangerous when sharing root passwords 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.


Discover more from Geek Rewind

Subscribe to get the latest posts to your email.

Like this:



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Geek Rewind

Subscribe now to keep reading and get access to the full archive.

Continue reading