How to Use the su Command in Linux with Examples

January 7, 2020

Introduction

In Linux, the su command (switch user) is used to run a command as a different user.

In this guide, you will learn how to use the su command, with practical examples.

A tutorial on how to use the su command in Linux ubuntu or centos

Prerequisites

  • A system running Linux
  • Access to a terminal window/command-line (Ctrl-Alt-T, Ctrl-Alt-F2)

How the su Command Works

The su command is used to run a function as a different user. It is the easiest way to switch or change to the administrative account in the current logged in session.

Some versions of Linux, like Ubuntu, disable the root user account by default making the system more secure. But, this also restricts the user from running specific commands.

Using su to temporarily act as a root user allows you to bypass this restriction and perform different tasks with different users.

Note: A root account is a master administrator account with full access and permissions in the system. Because of the severity of changes this account can make, and because of the risk of it being compromised, most Linux versions use limited user accounts for normal use.

su Command Syntax

To use the su command, enter it into a command-line as follows:

su [options] [username [arguments]]

If a username is specified, su defaults to the superuser (root). Simply find the user you need and add it to the su command syntax.

su Command Options

To display a list of commands, enter the following:

su –h
Display a list of su commands in Linux.

Here are some common options to use with the su command:

  • Username – Replace username with the actual username you want to log in with. This can be any user, not just root.
  • –c or –command [command] – Runs a specific command as the specified user.
  •  or –l or –login [username] – Runs a login script to change to a specific username.  You’ll need to enter a password for that user.
  • –s or –shell [shell] – Allows you to specify a different shell environment to run in.
  • –h or –help – Show the help file for the su command.
  • –p or ––preserve–environment – Preserve the shell environment (HOME, SHELL, USER, LOGNAME).

su Command Examples

Switch to a Different User

To switch the logged-in user in this terminal window, enter the following:

su –l [other_user]

You’ll be asked for a password. Enter it, and the login will change to that user.

An example how to switch to a different user in Linux using the -su command

If you omit a username, it will default to the root account. Now, the logged-in user can run all system commands. This will also change the home directory and path to executable files.

Use the whoami command to verify you switched to a different user.

Note: If you are having issues with authentication, you can change the root or sudo password in a couple of simple steps.

Run Specific Command as a Different User

To run a specific command as a different user, use the –c option:

su –c [command] [other_user]

The system will respond by asking you for the user password.

When you enter this example, the system will use the specified account to run the ls (list directory contents) command.

Run a command in terminal window as a different user.

Use a Different Shell

To use a different shell, or operating environment, enter the following:

su –s /usr/bin/zsh

This command opens a root user account in Z shell.

Use a Different User in the Same Environment

You can keep the environment of the current user account with the –p option:

su –p [other_user]

Replace [other_user] with the actual username you want to switch to.

The user account will switch, but you’ll keep the same home directory. This is useful if you need to run a command as a different user, but you need access to the current user’s data.

The su command to use a different user in the same environment

To verify you remained in the same home environment, use the echo $HOME command that will display the directory you are working in.

Command Comparison: su vs sudo

sudo Command

The sudo command grants a one-time or limited-time access to root functionality. Typically, the sudo command is used to quickly run an administrative command, then return to the user account’s regular permissions.

To provide sudo access, the user has to be added to the sudo group.

Note: By default, some versions of Linux (such as Ubuntu) disable the root account. That means there’s no password assigned to the root user. However you can switch to root by running the following command and entering the currently logged-in user’s password:

sudo su -

su Command

The su command lets you switch the current user to any other user. If you need to run a command as a different (non-root) user, use the –l [username] option to specify the user account. Additionally,su can also be used to change to a different shell interpreter on the fly.

su is an older but more fully-featured command. It can duplicate the functionality of sudo by use of the –c option to pass a single command to the shell.

Conclusion

You now know how to use the su command to temporarily change users and execute commands in Linux. Use the examples provided to get started.

Next, consider checking out our Linux Command Cheat Sheet Tutorial With Examples.

Was this article helpful?
YesNo
Dejan Tucakov
Dejan is the Head of Content at phoenixNAP with over 8 years of experience in Web publishing and technical writing. Prior to joining PNAP, he was Chief Editor of several websites striving to advocate for emerging technologies. He is dedicated to simplifying complex notions and providing meaningful insight into data center and cloud technology.
Next you should read
How to Create a Sudo User on Debian
April 22, 2019

Sudo stands for superuser do. Sudo is a command used in Unix-like systems to allow a regular user to execute...
Read more
How to use apt Package Manager on Ubuntu Linux
January 7, 2019

In Linux, special tools were developed for managing applications. Application software for Linux typically...
Read more
How To Add User to Sudoers & Add User to Sudo Group on CentOS 7
December 5, 2018

This guide will walk you through the steps to create or add a sudo user on CentOS 7. The "sudo" command...
Read more
How to Reset or Change the Root Password in Linux
October 22, 2018

In Linux, root privileges (or root access) refers to a user account that has full access to all files...
Read more