How to Use the SU Command in Linux

The su command is often used in conjunction with other commands like sudo for temporary elevated access, passwd for changing passwords, and whoami to check which user you’re currently logged in as. Understanding su is crucial for system administration tasks and for users who need to operate in a multi-user environment.

So, let’s get started and learn how to effectively use the su command to switch user accounts and elevate permissions.

General syntax for su command:

$ su [OPTIONS] [USER [ARGUMENT...]]
1. 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 1: Switching to Root User

If you run su without any username, it defaults to the root user. You’ll be prompted to enter the root password.

$ su
Password: [Enter root password here]
# [You are now in a shell as the root user]

Example 2: Switching to a Specific User

To switch to a specific user, you can specify the username as an argument. For example, to switch to a user named john, you would do:

$ su john
Password: [Enter john's password here]
$ [You are now in a shell as john]

Example 3: Running a Command as Another User

You can also run a single command as another user without entering their shell. For example, to run whoami as john:

$ su -c "whoami" john
Password: [Enter john's password here]
john
2. 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 1: Running a Command as Root

Let’s say you want to run the ls /root command to list the contents of the /root directory, which is usually only accessible by the root user. You can use:

su root -c "ls /root"

After executing this command, you’ll be prompted to enter the root password. Once authenticated, the ls /root command will be executed, and you’ll see the contents of the /root directory.

Example 2: Running a Command as Another User

Suppose you have another user on your system named john, and you want to run a Python script located at /home/john/script.py as that user. You can use:

su john -c "python3 /home/john/script.py"

Again, you’ll be prompted to enter the password for the john account. Once authenticated, the Python script will be executed as john.

Example 3: Running Multiple Commands

You can also run multiple commands in sequence. For example, to change to a directory and then list its contents as user john, you can do:

su john -c "cd /home/john/documents && ls"

This will switch to the john user, navigate to /home/john/documents, and then list the contents of that directory.

3. Specify the shell
su -s /bin/[shell]

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

Example:

Let’s say you are currently logged in as the user john, and you want to switch to the user alice using the bash shell. You would execute the following command:

su -s /bin/bash alice

After running this command, you’ll be prompted to enter the password for alice. If the password is correct, you’ll be switched to alice’s user account and the shell will be bash.

4. Preserve user account environment
su –p [another_user]

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

Example:

Let’s say you are currently logged in as the user john, and you want to switch to the user alice but preserve john’s environment variables.

By typing in the following command:

su -p alice

You will be prompted to enter the password for alice.

After successfully entering the password, you will be switched to the user alice, but your environment will still contain the settings from john.

More Linux commands:
Directory Operations rmdir · cd · pwd · exa · ls
File Operations cat · cp · dd · less · touch · ln · rename · more · head
File System Operations chown · mkfs · locate
Networking ping · curl · wget · iptables · mtr
Search and Text Processing find · grep · sed · whatis · ripgrep · fd · tldr
System Information and Management env · history · top · who · htop · glances · lsof
User and Session Management screen · su · sudo · open
WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail