Chat Zalo Chat Messenger Phone Number Đăng nhập
How to Use the su Command in Linux with Examples - phoenixNAP

How to Use the su Command in Linux with Examples – phoenixNAP

introduction

On Linux, the

su (switch user) command is used to execute a command as a different user.

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

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

How the su command works

The command is used to execute a function as a different user. This is the easiest way to switch or switch to the administrative account in the current logged-on session.

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

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

su command

syntax

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

[options] [user name [arguments]]

If a user name is specified, its default value is superuser (root). Just find the user you need and add it to the su command syntax.

su command options

To display a list of commands, type the following

: su -h

Here are some common options to use with the su command

: User name: Replace the user name with

  • the actual user name you want to log in with. This can be any user, not just root.
  • -c or -command [command]: Executes a specific command as the specified user.
  • – or –

  • l or –login – Runs a login script to change to a specific username. You will need to enter a password for that user.
  • -s or -shell—Specifies a different shell environment to run.
  • -h or -help Displays the help file for the su command.
  • -p or –preserve-environment: Preserves the shell environment (HOME, SHELL, USER, LOGNAME).

Examples of commands

su

Switch to a different user

To change the logged in user in this terminal window, type the following:

your -l [other_user]

You will be prompted for a password. Enter it and the login will change to that user.

If you omit a user name, the default value is the root account. The logged-on user can now execute all system commands. This will also change the home directory and path to executable files.

Use the whoami command to verify that you have changed to a different user.

Run a 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 for the user password.

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

Use a different shell To use a different shell,

or operating environment, type the following

: su -s /usr/bin/zsh

This command opens a root user account in the Z shell

.

Use a different user in

the same environment

You can maintain the environment of the current user account with the -p option: your -p

[other_user]

Replace [other_user] with the actual user name you want to change to

.

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

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

Command comparison: su command vs

sudo

sudo The sudo

command grants a single or time-limited access to root functionality. Typically, the sudo command is used to quickly run an administrative command and then return to the normal permissions of the user account.

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

su command

The su command

allows you to change the current user to any other user. If you need to run a command as a different user (not root), use the -l [user name] option to specify the user account. In addition, su can also be used to switch to a different shell interpreter on the fly.

Su is an older command but with more functions. You can duplicate sudo functionality by using the -c option to pass a single command to the shell.

Conclusion

Now you know how to use the su command to temporarily switch users and execute commands on Linux. Use the examples provided to get started.

Next, consider checking out our Linux Command Cheat Sheet Tutorial with examples.

Contact US