runuser

systemLinux
The runuser command is one of the most frequently used commands in Linux/Unix-like operating systems. runuser Run a command with substitute user and group ID

Quick Reference

Command Name:

runuser

Category:

system

Platform:

Linux

Basic Usage:

runuser [options] [arguments]

Common Use Cases

    Syntax

    runuser [options] -u user [--] [command [args...]]

    Options

    Option Description
    -c, --command=COMMAND Pass a single command to the shell with -c
    -f, --fast Pass -f to the shell (for csh or tcsh)
    -g, --group=GROUP Specify the primary group
    -G, --supp-group=GROUP Specify a supplemental group
    -l, --login Make the shell a login shell
    -m, -p, --preserve-environment Preserve environment variables
    -s, --shell=SHELL Run the specified shell instead of the default
    -u, --user=USER The user to run as (required)
    -w, --whitelist-environment=LIST Pass only specified variables to the new environment
    --session-command=COMMAND Pass a single command to the shell with -c and don't create a new session
    -h, --help Display help information and exit
    -V, --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the runuser command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    Basic Examples:

    sudo runuser -u username -- whoami
    Run the command 'whoami' as the specified user.
    sudo runuser -u postgres -- psql -l
    Run PostgreSQL client as the postgres user to list databases.

    Advanced Examples:

    sudo runuser -u apache -- bash -c 'cd /var/www/html && ls -la'
    Run multiple commands as the apache user in a specific directory.
    sudo runuser -u mysql -g mysql -- mysqld_safe Run the MySQL daemon as the mysql user and group. sudo runuser -u username -l -- bash Start a login shell as the specified user. sudo runuser -u jenkins -p -- java -jar jenkins.war Preserve the current environment and run Java as the jenkins user. sudo runuser -u www-data -- bash -c 'echo $PATH' See the PATH environment variable as it would be for www-data user. sudo runuser -u nobody -s /bin/sh -- -c 'id' Run the 'id' command as nobody using the /bin/sh shell. sudo runuser -u postgres -- bash -c 'cd ~ && pwd' Navigate to postgres user's home directory and print the path. sudo runuser -u git -g git -- git status Run git command as the git user and group.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `runuser` command is a utility provided by the util-linux package that allows a user (typically root) to run commands with substitute user and group IDs. It's similar to the more commonly used `su` command but with some key differences that make it more suitable for certain administrative tasks and scripting scenarios. Unlike `su`, which prompts for a password when the user executing it is not root, `runuser` can only be run by the root user (or via sudo) and does not require the target user's password. This makes it ideal for system scripts that need to execute commands as specific users without interactive password prompts. Key features and characteristics of the `runuser` command include: 1. Security-Focused: It's designed with security in mind and doesn't require or use the target user's password. 2. Limited Environment: By default, it provides a clean environment rather than preserving the calling user's environment variables (though this can be changed with options). 3. No PAM Authentication: Unlike `su`, it doesn't use PAM (Pluggable Authentication Modules) for authentication, making it more lightweight. 4. Root Only: It can only be executed by the root user or via sudo, limiting its use to authorized administrators. 5. System Automation: It's particularly useful in system scripts, cron jobs, and automation tasks that need to run processes as specific users. Common use cases for `runuser` include: - Running database maintenance tasks as the database user - Starting application servers as their dedicated service users - Performing file operations with appropriate ownership and permissions - Executing scheduled tasks that need to run as specific users - Testing applications in the context of their intended user - System initialization scripts that need to launch processes as non-root users It's important to note that while `runuser` and `su` have similar functionality, `runuser` is generally preferred in scripts and automated tasks because it has a more predictable behavior and doesn't rely on password authentication. The `sudo` command is another alternative, with its own set of features and configuration options that allow for more fine-grained control over who can run what commands as which users. When using `runuser`, the double dash (`--`) is often used to separate the options for `runuser` itself from the command to be executed, preventing any ambiguity in option parsing.

    Related Commands

    These commands are frequently used alongside runuser or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the runuser command works in different scenarios.

    $ runuser
    View All Commands