Runuser: how to run a Linux command with another user?

runuser Linux - Run as a command

Under Linux, how do you run a command as another user? That's what we're going to find out in this tutorial, with the runuser command!

You probably know the " sudo "This is used to run a command with elevated privileges as a standard user. This avoids the need to use the root account for administration tasks. What if I want to run a command with another specific user?

1. Run as... with runuser

Under Linuxto perform a Run as... "by putting yourself in another user's shoes, it is advisable touse the runuser command.

There are several possible syntaxes for using this command. For example:

runuser -l UserName -c '/path/to/command argument1 argument2' -c '/path/to/command argument1 argument2
runuser -u UserName -- command argument1 argument2

To use runuserit is necessary to specify target user name (which we want to use to execute the command) and the command to be executedwith arguments, if any. The " -l "is used to specify the connection identifier, similar to " -u "But it's how runuser interacts with the account that will change. Next, the " -c "This is used to specify the command to be executed.

Let's take the following example: run a PHP script as user "www-data which is the user associated with the Apache2 Web server on various distributions, including Debian and Ubuntu. Let's take the example of the script "/var/www/monsite/script-synchro.php" that we want to run with this user.

runuser -u www-data -- php /var/www/monsite/script-synchro.php

With the www-data account, it is essential to use this syntax. Otherwise, the error " This account is currently not available. "will be displayed. With another account, associated with another system user for example, you can use the first syntax mentioned at the beginning of this article.

For example:

runuser -l tutobox -c 'ls /var/www/monsite'

It's up to you to use either of these two syntaxes, depending on your needs. If one method doesn't work, use the second.

Finally, to use runuser with a user other than root, you must add the prefix "sudo" before the command. The result is simply :

sudo runuser -l tutobox -c 'ls /var/www/monsite'

Last but not least, there's also the option ofuse the "su" command instead of "runuser. Taking the previous example, this gives the following command:

su - tutobox -c "ls /var/www/monsite"

2. Conclusion

After reading this article from the tutorial box, you will be able torun a command "as" on a Linux machine. Here, it's runuser was used, but you can also use su and even sudo to elevate its privileges.

Resources :

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *