# man > pkexec(1)

> **TLDR:** Execute commands as another user.
>
- Run command as root:
  `pkexec {{command}}`
- Switch user to root:
  `pkexec`
- Run command as a specific user:
  `pkexec --user {{username}} {{command}}`

*Source: tldr-pages*

---

[_PKEXEC_(1)](https://www.chedong.com/phpMan.php/man/PKEXEC/1/markdown)                                      pkexec                                      [_PKEXEC_(1)](https://www.chedong.com/phpMan.php/man/PKEXEC/1/markdown)

## NAME
       pkexec - Execute a command as another user

## SYNOPSIS


       **pkexec **[**--version**] [**--disable-internal-agent**] [**--help**]

       **pkexec **[**--keep-cwd**] [**--user _**username_] _PROGRAM_ [_ARGUMENTS_...]

## DESCRIPTION
       **pkexec **allows an authorized user to execute _PROGRAM_ as another user. If _PROGRAM_ is not
       specified, the default shell will be run. If _username_ is not specified, then the program will
       be executed as the administrative super user, _root_.

## RETURN VALUE
       Upon successful completion, the return value is the return value of _PROGRAM_. If the calling
       process is not authorized or an authorization could not be obtained through authentication or
       an error occured, **pkexec **exits with a return value of 127. If the authorization could not be
       obtained because the user dismissed the authentication dialog, **pkexec **exits with a return
       value of 126.

## AUTHENTICATION AGENT
       **pkexec**, like any other polkit application, will use the authentication agent registered for
       the calling process or session. However, if no authentication agent is available, then **pkexec**
       will register its own textual authentication agent. This behavior can be turned off by
       passing the **--disable-internal-agent **option.

## SECURITY NOTES
       Executing a program as another user is a privileged operation. By default the action to check
       for (see the section called “ACTION AND AUTHORIZATIONS”) requires administrator
       authentication. In addition, the authentication dialog presented to the user will display the
       full path to the program to be executed so the user is aware of what will happen.

       The environment that _PROGRAM_ will run it, will be set to a minimal known and safe environment
       in order to avoid injecting code through LD_LIBRARY_PATH or similar mechanisms. In addition
       the PKEXEC_UID environment variable is set to the user id of the process invoking **pkexec**. As
       a result, **pkexec **will not by default allow you to run X11 applications as another user since
       the $DISPLAY and $XAUTHORITY environment variables are not set. These two variables will be
       retained if the _org.freedesktop.policykit.exec.allow_gui_ annotation on an action is set to a
       nonempty value; this is discouraged, though, and should only be used for legacy programs.

       **pkexec **will run _PROGRAM_ in _username_'s home directory, unless **--keep-cwd **is used to override
       this behavior

       Note that **pkexec **does no validation of the _ARGUMENTS_ passed to _PROGRAM_. In the normal case
       (where administrator authentication is required every time **pkexec **is used), this is not a
       problem since if the user is an administrator he might as well just run **pkexec bash **to get
       root.

       However, if an action is used for which the user can retain authorization (or if the user is
       implicitly authorized) this could be a security hole. Therefore, as a rule of thumb, programs
       for which the default required authorization is changed, should **never **implicitly trust user
       input (e.g. like any other well-written _suid_ program).

## ACTION AND AUTHORIZATIONS
       By default, the _org.freedesktop.policykit.exec_ action is used. To use another action, use the
       _org.freedesktop.policykit.exec.path_ annotation on an action with the value set to the full
       path of the program. In addition to specifying the program, the authentication message,
       description, icon and defaults can be specified. If the _org.freedesktop.policykit.exec.argv1_
       annotation is present, the action will only be picked if the first argument to the program
       matches the value of the annotation.

       Note that authentication messages may reference variables (see the section called
       “VARIABLES”), for example $(user) will be expanded to the value of the user variable.

## WRAPPER USAGE
       To avoid modifying existing software to prefix their command-line invocations with **pkexec**,
       it's possible to use **pkexec **in a [34m**she-bang wrapper**[1] like this:

           #!/usr/bin/pkexec /usr/bin/python

           import os
           import sys

           print "Hello, I'm running as uid %d"%(os.getuid())

           for n in range(len(sys.argv)):
               print "arg[%d]=`%s'"%(n, sys.argv[n])

       If this script is installed into /usr/bin/my-pk-test, then the following annotations

             [...]
             <annotate key="org.freedesktop.policykit.exec.path">/usr/bin/python</annotate>
             <annotate key="org.freedesktop.policykit.exec.argv1">/usr/bin/my-pk-test</annotate>
             [...]

       can be used to select the appropriate polkit action. Be careful to get the latter annotation
       right, otherwise it will match any **pkexec **invocation of /usr/bin/python scripts.

## VARIABLES
       The following variables are set by **pkexec**. They can be used in authorization rules and
       messages shown in authentication dialogs:

       _program_
           Fully qualified path to the program to be executed. Example: “/bin/cat”

       _command_line_
           The requested command-line (do not use this for any security checks, it is not secure).
           Example: “cat /srv/xyz/foobar”

       _user_
           The user name of the user to execute the program as. Example: “davidz”

       _user.gecos_
           The full name of the user to execute the program as. Example: “David Zeuthen”

       _user.display_
           A representation of the user to execute the program as that is suitable for display in an
           authentication dialog. Is typically set to a combination of the user name and the full
           name. Example: “David Zeuthen (davidz)”

## AUTHOR
       Written by David Zeuthen <<davidz@redhat.com>> with a lot of help from many others.

## BUGS
       Please send bug reports to either the distribution or the polkit-devel mailing list, see the
       link [34m**<https://gitlab.freedesktop.org/polkit/polkit/-/issues/> **on how to subscribe.

## SEE ALSO
       [**polkit**(8)](https://www.chedong.com/phpMan.php/man/polkit/8/markdown), [**polkitd**(8)](https://www.chedong.com/phpMan.php/man/polkitd/8/markdown), [**pkaction**(1)](https://www.chedong.com/phpMan.php/man/pkaction/1/markdown), [**pkcheck**(1)](https://www.chedong.com/phpMan.php/man/pkcheck/1/markdown), [**pkttyagent**(1)](https://www.chedong.com/phpMan.php/man/pkttyagent/1/markdown)

## NOTES
        1. she-bang wrapper
           <http://en.wikipedia.org/wiki/Shebang_>(Unix)

polkit                                        May 2009                                     [_PKEXEC_(1)](https://www.chedong.com/phpMan.php/man/PKEXEC/1/markdown)
