Skip to main content

The su Command in Linux: A Comprehensive Guide

Linux's power and flexibility lie in its commands, and the su command is one of those indispensable ones. Standing for "Switch User," su allows you to change your identity to another user without logging out and back in again. This is useful for system administrators for system maintenance, for developers for running processes as different users, and for users for switching between accounts. This article will give you a comprehensive look at the su command in Linux, including its syntax, options, and practical examples.

Syntax

The general syntax of the su command is:

su [options] [username [arguments]]

Here, options are the flags you can use with the command, username is the username you want to switch to, and arguments are passed to the login shell.

Options and Their Descriptions

Here's a table detailing the options that you can use with the su command.

OptionShorthandDescription
--help-hDisplay help message and exit.
--command-cSpecify a command that will be executed by the shell.
--fast-fPass the -f option to the shell, usually preventing reading of .profile or .cshrc
--login-lStart a login shell, loading the target user's environment.
--shell-sSpecify the shell to use, overriding the shell defined for the target user.
--preserve-environment-pDo not reset environment variables, keep the same environment.

Creating Users for Testing

Before jumping into examples, let's first create some users to play with:

sudo adduser user1
sudo adduser user2

Now, you have two new users, user1 and user2, which we'll use for demonstration.

Basic Usage

Switching to another user:

su user1

This will prompt you for user1's password. After authentication, you will be switched to user1's environment.

Using Options

Running a Command as Another User

You can run a specific command as another user using the -c option:

su -c "whoami" user1

Starting a Login Shell

By using the -l option, you can start a login shell:

su -l user1

Or simply,

su - user1

Specifying a Shell

If you want to specify a shell while switching user, you can do so using the -s option:

su -s /bin/zsh user1

Combining Options

You can also combine multiple options:

su -l -s /bin/zsh -c "whoami && echo $SHELL" user1

This will log in as user1, start the Zsh shell, and then execute the commands whoami and echo $SHELL.

Conclusion

Understanding how to use su effectively is essential for managing users and performing tasks that require different user roles or permissions. The command is powerful but straightforward once you get the hang of it. Always remember to exit the shell when you are done using another user's account to return to your original session.

That's a wrap on the su command in Linux. Hopefully, you now have a strong understanding of what it does, its options, and how to use it.

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle