How to Run Linux Commands as Another User?

Sometimes, there is a need to run a Linux command from another user account, and to do so, the conventional approach is to switch to that user and then run the command. However, Linux distributions also support a feature to run a command as another user without logging out of the current user.

This blog will explore different approaches to run the Linux commands as another user.

Method 1: Using the “runuser” Command

The “runuser” command allows running the command as the substitute user in Linux distributions. The general syntax of using the “runuser” command to run Linux commands as another user:

Syntax

$ sudo runuser -[options] [username] [command]

According to the above general usage of the runuser command, we can use this command as the root user or with super-user privilege by using the “sudo”.

Then, we can use the different options of the runuser, which can be explored with the command:

$ runuser --help

The options and their application will be displayed on the screen.

Coming back to the general syntax of the “runuser” command, after specifying options, you have specified the user name to which you want to use and then the command.

Example 1: Switch the User and then Run Commands

For example, we will log in to another user, “john,” by using the runuser command’s option “-l”:

$ sudo runuser -l john

Now, we can run the multiple commands in the user john and then exit the shell by using the command:

$ exit

The shell of john user has been logout.

Example 2: Run the Linux Commands as Another User from the Same Shell

If you want to run the command as John user from the same shell:

$ sudo runuser -l john -c “ls”

The contents of the john user directory have been displayed.

Method 2: Using the su Command

Next is switching the specified user account using the “su” command. The general syntax of using the “su” command:

$ su [options] - [username] [arguments]

The above general syntax of switching to another account is simple to understand. Use the su command with the “-” sign and specify the username. It will ask you for the user account’s password and log in to another user account.

Example 1: Switch the User and then Run Commands

To understand it more clearly, we will use the su command to log in to the “john” user account:

After asking for the password, it is logged in to the john user shell. Run the ls command in the new shell:

$ ls

To go back to the “itslinux” user, again use the su command:

$ su - itslinux

We are back to the shell of our user account.

Example 2: Run the Linux Commands as Another User from the Same Shell

To run the command without logging into another user shell, use the command:

$ su - john -c “ls”

First, it asks for a password, and then the result of the command is printed.

Method 3: Using the “pkexec” command

The “pkexec” command can also be used to run Linux commands by another user. The usage of the pkexec command is visualized in the following syntax:

Syntax

$ sudo pkexec --user [username] [command]

Example

Following the above general syntax, we will log in to the john account and run the command:

The command has been executed in the “john” user account.

Conclusion

To run Linux commands as another user, we can use the “runuser” command and “pkexec” command and can log in to another user with the “su” command. All these commands switch to another user (either while execution or before execution) to run the command from a specific user’s account. This post has briefly explained the methods to run Linux commands as another user.