Using the `runuser` Command (with examples)
- Linux
- November 5, 2023
The runuser
command allows a user with root privileges to run commands as a different user and group without asking for a password. This command is useful in various scenarios where you need to execute a command or start a shell session as another user.
Use Case 1: Run Command as a Different User
To run a command as a different user, you can use the following syntax:
runuser user -c 'command'
user
is the username of the user you want to run the command as.command
is the command you want to run as the specified user.
Motivation: This use case can be helpful when you need to execute a command that requires specific user permissions. For example, suppose you want to run a script that interacts with a service running as a particular user. By using runuser
, you can easily switch to that user and execute the script.
Example:
runuser john -c 'ls /home/john'
This command executes the ls
command as the user john
, listing the contents of the /home/john
directory.
Use Case 2: Run Command as a Different User and Group
To run a command as a different user and group, you can use the following syntax:
runuser user -g group -c 'command'
user
is the username of the user you want to run the command as.group
is the group name you want the command to be executed with.command
is the command you want to run as the specified user and group.
Motivation: This use case is useful when you need to execute a command with both specific user and group permissions. For instance, if you have a command that requires access to a directory owned by a specific user and group, you can use this syntax to execute the command with the appropriate permissions.
Example:
runuser tom -g developers -c 'git pull'
This command runs the git pull
command as the user tom
with the group developers
. It allows tom
to update the code repository associated with the developers
group.
Use Case 3: Start a Login Shell as a Specific User
To start a login shell as a specific user, you can use the following syntax:
runuser user -l
user
is the username of the user you want to start the login shell as.
Motivation: Running a login shell as a specific user is useful when you want to have an interactive session as that user. It loads the user’s environment variables, sets up the user’s default shell and workspace, and allows the user to perform actions as if they had logged in directly.
Example:
runuser alice -l
This command starts a login shell as the user alice
, providing an interactive session with her environment and default shell settings.
Use Case 4: Specify a Shell for Running Instead of the Default Shell
To specify a shell for running instead of the default shell, you can use the following syntax:
runuser user -s /bin/sh
user
is the username of the user you want to run the shell as./bin/sh
is the path to the shell executable you want to use.
Motivation: The default shell specified in the user’s account settings is typically used when starting a shell session. However, there may be situations where you want to override the default shell and use a specific one. This use case allows you to achieve that.
Example:
runuser robert -s /bin/zsh
This command starts a shell session as the user robert
using the Zsh shell (/bin/zsh
) instead of the user’s default shell.
Use Case 5: Preserve the Entire Environment of Root
To preserve the entire environment of the root user (only if --login
is not specified), you can use the following syntax:
runuser user --preserve-environment -c 'command'
user
is the username of the user you want to run the command as.command
is the command you want to run as the specified user.
Motivation: By default, when running a command as a different user using runuser
, the environment variables of the original user are not preserved. However, in some cases, you may need to keep the environment variables of the root user intact. This use case allows you to achieve that.
Example:
runuser sarah --preserve-environment -c 'echo $HOME'
This command runs the echo $HOME
command as the user sarah
, preserving the HOME
environment variable value of the root user.