| Category | linux |
| Added on | 2018-07-18 |
| Source | source |
You need to be superuser to run commands as another user (or get SU privileges in some other way)
$ sudo -H -u user bash -c 'echo "I am $USER, with uid $UID"'
-H The -H (HOME) option requests that the security policy set
the HOME environment variable to the home directory of the
target user (root by default) as specified by the password
database. Depending on the policy, this may be the default
behavior.
$ su - user -c 'echo "I am $USER, with uid $UID"'
$ runuser -l user -c 'echo "I am $USER"'
Useful for GUI apps
$ pkexec --user user /path/to/command arg1