How to Use the SU Command in Linux

Stands for substitute user. This command is used to switch user and lets exceuting command with the privilage of that user. It simplifies, typically for administrator, to manage users. Instead of logging to shell for separate users and sessions, admin can login with their own account and switch to sepcific user with their username.

General syntax for su command:

$ su [OPTIONS] [USER [ARGUMENT...]]
Login with a different user account
su [username]

Pass the username to the su command, and it will provide a login session when the password has been verified.

Example: Login with a different username newone.

login diff account
Login with a different user account + run a command
su [username] -c [command]

The -c option allows you to run a particular command under a different user account and display the results in the current login session.

Example: Running the ls command under another user account newone and displaying it in the ubuntu login session.

login and run command
Specify the shell
su -s /bin/[shell]

Use the -s option to change an existing default shell to a different one.

Example: Using the command to open the root user account in the Z shell (zsh).

specify shell
Preserve user account environment
su –p [another_user]

The -p option allows you to keep the environment of the current user account.

Example: Switching username user to user newone. And displaying the home directory to show that it remains the same.

preserve user acc env
WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail