runuser Command Examples in Linux

runuser is a command-line utility in Linux that allows users to run commands or shell scripts as a specific user and group without prompting for a password. The utility requires root privileges to run and can be used to execute commands with the security context of a specified user and group.

By default, when a user executes a command or runs a shell script, the process runs with the privileges of the user who initiated the command. However, sometimes it is necessary to execute a command or script with the privileges of another user. For example, a system administrator may need to run a script as a specific user to perform certain tasks that require that user’s permissions.

runuser Command Examples

1. Run command as a different user:

# runuser user -c 'command'

2. Run command as a different user and group:

# runuser user -g group -c 'command'

3. Start a login shell as a specific user:

# runuser user -l

4. Specify a shell for running instead of the default shell (also works for login):

# runuser user -s /bin/sh

5. Preserve the entire environment of root (only if `–login` is not specified):

# runuser user --preserve-environment -c 'command'

Summary

Note that runuser does not require a password to execute the command or script as the specified user and group, so it is important to use the utility with caution. Also, runuser is only available on Linux systems and is not supported on other operating systems.

For more information about runuser, including additional options and examples, you can refer to the runuser manual page, which can be accessed using the man runuser command or by visiting https://manned.org/runuser.

Related Post