The su command in Linux, which stands for “substitute user” or “switch user,” is a powerful utility that allows you to switch to another user’s account. The su command is essential for performing administrative tasks that require elevated privileges, such as installing software or modifying system configurations.

In this guide, we will explore how to effectively use the su command with practical examples.

Understanding the su Command

The su command allows you to switch from your current user account to another user account. It’s commonly used to gain superuser privileges by switching to the root user.

Here is a basic syntax:

su [options] [username]

Options:

  • -: Load the target user’s environment as a login shell.
  • -c: Execute a command as the specified user.

Difference Between su and sudo

  • su: Switches the user, requiring the target user’s password (usually root). Once switched, you stay as that user until you exit.
  • sudo: Runs a single command with elevated privileges, requiring the current user’s password. It’s more controlled, as you don’t completely switch users.

Switching to Root User

The most common use of su is to switch to the root user, allowing you to perform administrative tasks.

Example 1: Basic Command to Switch to the Root User

To switch to the root user, simply enter:

su

You will be prompted for the root password. After entering the correct password, you will have root privileges.

Password:
[root@hostname ~]#

Switching to Another User

You can also use su to switch to any other user account.

Example 1: Switching to a Different User Account

To switch to a different user account, specify the username:

su - username

You will be asked to provide your user’s password:

Password:
[username@hostname ~]$

Running Commands as Another User

You don’t always need to switch to an interactive shell; instead, you can run a single command as another user.

Example 1: Running a Single Command as Root

To execute a command as root without switching to a root shell:

su -c "command_to_run"

Example: To create a directory named example_dir in the /root directory:

su -c "mkdir /root/example_dir"

After entering the root password, the command will be executed, and the directory will be created.

Example 2: Running a Command as Another User

You can also run a command as a specific user:

su - username -c "command_to_run"

Example: To list the files in another user’s home directory:

su - john -c "ls /home/john"

This allows you to perform specific tasks for another user without fully switching to their account.

Common Errors and Troubleshooting

1. “Authentication Failure”

If you enter an incorrect password, you will see:

su: Authentication failure

Make sure you are using the correct password for the target user. For root, this means knowing the root password, which may be different from your current user password.

2. Permission Issues

If you attempt to use su to switch to a restricted account, you may encounter permission issues:

su: cannot set user id: Permission denied

You need to ensure that you have the proper permissions to switch to the specified user. Only users with sufficient privileges can switch to certain accounts, like root.

Conclusion

The su command is an essential tool for managing Linux systems, allowing you to switch users, run commands as other users, and perform administrative tasks effectively. You can now easily switch between multiple users on dedicated server hosting from Atlantic.Net!