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.
If you encounter the below error while running the command runuser:
runuser: command not found
you may try installing the below package as per your choice of distribution:
Distribution | Command |
---|---|
Debian | apt-get install util-linux |
Ubuntu | apt-get install util-linux |
Alpine | apk add util-linux |
Arch Linux | pacman -S util-linux |
Kali Linux | apt-get install util-linux |
CentOS | yum install util-linux |
Fedora | dnf install util-linux |
OS X | dnf install util-linux |
Raspbian | apt-get install util-linux |
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.