today-i-learnt

Run command as another user that has no shell

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

$ 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

$ su - user -c 'echo "I am $USER, with uid $UID"'

runuser

$ runuser -l user -c 'echo "I am $USER"'

pkexec

Useful for GUI apps

$ pkexec --user user /path/to/command arg1