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.
Prerequisites
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.
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.
To display a list of commands, enter the following:
su –h
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).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.
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