X
X
USD

Knowledge Base

HomepageKnowledge BaseHow the su Command Works

How the su Command Works

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.

 

Article From Phoenixnap

Can't find the information you are looking for?

Create a Support Ticket
Did you find it useful?
(212 times viewed / 3 people found it helpful)

Top