List of content you will read in this article:
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.
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:
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 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.
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.
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.