Special offer
popup-offers
English
+370 5 205 5502 sales@monovm.com

How to Switch Users in Linux

Su (switch user) is a Linux command that lets you run a command as a separate user. In the latest logged-in session, this is the simplest way to migrate to the administrative account. Any Linux distributions, such as Ubuntu, disable the root user account by default, rendering the code safer. However, the user would be unable to execute complex instructions as a result of this restriction. You can get around this limitation by briefly acting as a root user with su. If you want to learn more about the su and sudo command, then read this article completely. In this article, we will consider everything about the sudo command and ways to use it in Linux. 

31 May, 21 by Antoniy Yushkevych 4 min Read

A Detailed Breakdown of the Linux SU Command

 

Command Syntax for su

To use the su command, type the following into the command prompt:

su [options] [username [arguments]]

su defaults to the superuser if no username is specified (root). Locate the required user and include it in the su command syntax.

 

su Command Options

Use -h to see a list of commands options in su:

su –h

You can use the su command with the following options:

  • Username – Change your username with the username you'd like to use to log in. Any consumer, not just root, can do this.
  • –command or –c [cmd] – Executes a command as the user-defined.
  • –login or –l [username] – Changes the username by running a login script. For that person, you are required to enter a password.
  • –s or –shell [shell] – Allows you to execute the command in a separate shell context.
  • –h or –help – Displays the su command's help file.
  • –p or ––preserve-environment – Protect the shell environment (HOME, SHELL, USER, LOGNAME).

 

Examples of su Commands

Changing to an Another User

Enter the following to change the logged-in user in this terminal window:

su –l [other_user_name]

A password would be needed. When you type it in, the username will be changed to that account.

As a Different User, Execute a Specific Command

Using the –c alternative to execute a particular command as a separate user:

su –c [command] [other_user_name]

The system would then prompt you for your user name. When you execute this command, the system will run the ls (list the directory contents) command for the defined account.

Switching to a Different Shell Environment

Enter the following to use a new shell or operating environment:

su –s /usr/bin/zsh

In Z shell, this command creates a root user account.

In the Same Environment, Use a Different User

With the –p alternative, you can hold the current user account's environment:

su –p [other_user_name]

[another user] can be replaced with the real username you choose to use. The user account will change, but your home directory will remain the same. If you need to execute a command as a new user but need access to the current user's files, this is helpful.

Use the echo $HOME command to show the directory you're operating in to ensure you're still in the same home environment.

 

su vs. sudo Command Comparison

su Command

Su allows you to change the existing user to some other user. Use the –l [username] method to define a user account if you need to execute a command as someone other than root. Switching between shell interpreters on one go is also possible using the su command. Su is a more powerful but older command. It can mimic sudo's features by passing a single command to the shell with the –c option.

Sudo Command

The sudo command provides temporary or one-time privilege access to the root. The sudo command is typically used to easily run an administrative command before returning to the user account's normal permissions. The consumer must be connected to the sudo community to gain sudo access.

 

Conclusion

You now know how to use the su command in Linux to shift users and run commands briefly. Also, su and sudo have their differences. Follow the guide and start using su command as per your requirement. We hope that the above information will help you use the su commands while using the Linux terminal in your system. 

Antoniy Yushkevych

Master of word when it comes to technology, internet and privacy. I'm also your usual guy that always aims for the best result and takes a skateboard to work. If you need me, you will find me at the office's Counter-Strike championships on Fridays or at a.yushkevych@monovm.com