Open In App

Switch Users on Linux with the su Command

Last Updated : 19 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Switching users on a Linux system is a fundamental aspect of system administration, allowing users to perform tasks with different privileges. The ‘su’ command, short for “switch user” or “substitute user,” is a powerful tool that facilitates this transition. This article will delve into the intricacies of the ‘su’ command, exploring its various options and providing practical examples to demonstrate its usage.

Understanding the su Command

In Unix-like operating systems, the ‘su’ command, short for “substitute user,” offers a way to temporarily switch to a different user account within your current login session. This means you can adopt the privileges and permissions of the target user to perform specific tasks or access restricted files.

When you execute ‘su,’ it effectively replaces your current user ID with the ID of the specified account. By default, if you run ‘su’ without any arguments, it assumes you want to switch to the superuser account, also known as the “root” account. The root account holds the highest level of system privileges, granting nearly unrestricted access. However, ‘su’ can be used to switch to any user account on the system, provided you have the required permissions.

To successfully switch to another account using ‘su,’ you’ll typically need to provide the password for that account. This authentication process ensures authorized access and maintains security.

Basic Syntax of su Command

The basic syntax of the ‘su’ command is as follows:

su [OPTION] [USER]

Here,

[OPTION] represents various command-line options.

[USER] is the username to which you want to switch. Let’s explore some practical examples to understand how ‘su’ works in different scenarios.

Options Available in su Command

Options

Description

– or –login

Simulate a full login shell for the target user.

-c, –command

Specify a command to be executed with the new user.

-s, –shell

Define the shell to be used for the command.

-m, -p

Preserve the environment when switching users.

–session-command=

Run a command in a new session.

–preserve-environment

Preserve user environment variables.

-l

Synonym for –login.

–help

Display help information about the command.

–version

Display version information.

How to Switch to the Root User by Using su Command

To switch to the root user, simply type:

sudo su

2024-01-12_13-47

You will be prompted to enter the root user’s password. Once authenticated, you will have superuser privileges until you exit the session.

How to Switch to the Specific User by Using su Command

To switch to a different user, specify the username as an argument:

su username

Replace ‘username’ with the actual username you want to switch to. You’ll be prompted to enter the password for that user.

For example: If we want to switch to a user name “shivansh260” we will use the following command

su shivansh260

2024-01-12_13-48

How to Execute a Command as Another User by Using su Command

You can use ‘su’ to execute a single command as another user without switching to their environment:

sudo su -c "command" -s /bin/bash username

Replace ‘command’ with the desired command and ‘username’ with the target user. The ‘-s /bin/bash’ option specifies the shell to be used.

For example: If we want to print “hello” on the screen , and this command has to be run by a username “shivansh” we will use the following command

sudo su -c "echo hello" -s /bin/bash shivansh

2024-01-12_13-50

How to Preserve Environment Variables while Switching User

When switching users, you might want to preserve the environment variables from the original user. Use the ‘-‘ option to achieve this:

sudo su -

2024-01-12_13-53

How to Simulate a Login Shell

To simulate a full login shell for another user, use the ‘-l’ or ‘–login’ option:

su -l username

This ensures that the target user’s environment is fully loaded, just like during a regular login.

2024-01-12_13-53_1

This command switches to the root user while preserving the environment.

Switch Users with su Command – FAQs

How do I switch to the root user using the su command in Linux?

To switch to the root user, simply type su in the terminal and press Enter. You will be prompted to enter the root password. Once authenticated, you will have superuser privileges.

Can I switch to a specific user account other than root with su?

Yes, you can switch to a specific user by using the su command followed by the username. For example, to switch to the user ‘john’, type su john and enter the password when prompted.

What is the difference between su and su – in Linux?

The su – command (or su –login) simulates a full login shell for the target user, loading their environment variables. Without the dash, su retains the current environment. For example, su – username loads the user’s environment, while su username retains the current environment.

How can I run a single command as another user without switching to their account?

You can use the -c option with su to execute a single command as another user. For instance, su -c “command” -s /bin/bash username will run the specified command as the user ‘username’.

Is it possible to switch users in Linux and preserve the current environment variables?

Yes, you can preserve the environment variables when switching users by using the -m or -p option with su. For example, su – or su -m switches users while preserving the environment, ensuring a similar environment to the original user.

Conclusion

In this article we discussed the ‘su’ command in Linux which is essential for effective system administration, enabling users to seamlessly switch between accounts with varying privileges. This article has provided a comprehensive guide to the ‘su’ command, exploring its intricacies, syntax, and a range of options. From switching to the root user for system-wide tasks to executing specific commands as different users, the versatility of ‘su’ has been demonstrated through practical examples. Users have also been introduced to preserving environments, simulating login shells, and frequently asked questions to enhance their understanding. By following the outlined techniques and leveraging the various options available, Linux users can confidently navigate user switching scenarios for efficient system management.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads