pkexecを使用してGUIアプリケーションをrootとして実行するには?

pkexecを使用してGUIアプリケーションをrootとして実行するには?

使っています水滴GNU/Linux-Libreが付属しています。Gnome3フラッシュバックデスクトップ環境。

sudo&を介してGUIアプリケーションをrootとして実行できることを知っていますが、gksudo次のコマンドを使用してGUIアプリケーションをrootとして実行する方法を知りたいです。pkexec

gedit(または次のような他のアプリケーション)を実行しようnautilusとすると、pkexec gedit認証するパスワードの入力を求められます。

スクリーンショット

パスワードを入力するとエラーが発生して終了します。 -

$ pkexec gedit
error: XDG_RUNTIME_DIR not set in the environment.

(gedit:6135): Gtk-WARNING **: cannot open display:

だからディスプレイ環境に何か問題があるようです。

私も試しましたが、うまくDISPLAY=:0 pkexec geditいきません。


以下の情報はman pkexec以下で提供されます。

PROGRAM が実行される環境は、LD_LIBRARY_PATH または同様のメカニズムによるコードの挿入を避けるために、既知の最小の安全環境に設定されます。また、PKEXEC_UID 環境変数は、pkexec を呼び出すプロセスのユーザー ID に設定されます。したがって、$ DISPLAYおよび$ XAUTHORITY環境変数が設定されていないため、pkexecは他のユーザーとしてX11アプリケーションを実行できません。これら 2 つの変数は、ジョブの org.freedesktop.policykit.exec.allow_gui コメントが null 以外の値に設定されている場合に保持されます。ただし、これは推奨されず、従来のプログラムでのみ使用してください。

今、私はこれを達成するために何をすべきかわかりません。

それでは、どのように通過するかを理解するのに役立ちますpkexec可能ですか?


ところで、gparted-pkexec命令からインスピレーションを受けて見事に動作します。gparted使用する方法pkexec?

ベストアンサー1

これは、ポリシーパッケージにカスタムアクションを追加することで実現できます。 geditをrootとして実行するには、新しいpkexecファイルを作成する必要があります/usr/share/polkit-1/actions/org.freedesktop.policykit.gedit.policy。例:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
 "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">
<policyconfig>
    <action id="org.freedesktop.policykit.pkexec.gedit">
    <description>Run gedit program</description>
    <message>Authentication is required to run the gedit</message>
    <icon_name>accessories-text-editor</icon_name>
    <defaults>
        <allow_any>auth_admin</allow_any>
        <allow_inactive>auth_admin</allow_inactive>
        <allow_active>auth_admin</allow_active>
    </defaults>
    <annotate key="org.freedesktop.policykit.exec.path">/usr/bin/gedit</annotate>
    <annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
    </action>
</policyconfig>

ついにpkexec gedit期待どおりに動作するはずです。


マンページにアクセスするか、参考書以下の例を説明します。

$ man pkexec | grep -i ^Example -A 60
EXAMPLE
       To specify what kind of authorization is needed to execute the program /usr/bin/pk-example-frobnicate as
       another user, simply write an action definition file like this

           <?xml version="1.0" encoding="UTF-8"?>
           <!DOCTYPE policyconfig PUBLIC
            "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
            "http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">
           <policyconfig>

             <vendor>Examples for the PolicyKit Project</vendor>
             <vendor_url>http://hal.freedesktop.org/docs/PolicyKit/</vendor_url>

             <action id="org.freedesktop.policykit.example.pkexec.run-frobnicate">
               <description>Run the PolicyKit example program Frobnicate</description>
               <description xml:lang="da">Kør PolicyKit eksemplet Frobnicate</description>
               <message>Authentication is required to run the PolicyKit example program Frobnicate (user=$(user), program=$(program), command_line=$(command_line))</message>
               <message xml:lang="da">Autorisering er påkrævet for at afvikle PolicyKit eksemplet Frobnicate (user=$(user), program=$(program), command_line=$(command_line))</message>
               <icon_name>audio-x-generic</icon_name>
               <defaults>
                 <allow_any>no</allow_any>
                 <allow_inactive>no</allow_inactive>
                 <allow_active>auth_self_keep</allow_active>
               </defaults>
               <annotate key="org.freedesktop.policykit.exec.path">/usr/bin/pk-example-frobnicate</annotate>
             </action>

           </policyconfig>

       and drop it in the /usr/share/polkit-1/actions directory under a suitable name (e.g. matching the namespace of
       the action). Note that in addition to specifying the program, the authentication message, description, icon
       and defaults can be specified. Note that occurences of the strings $(user), $(program) and $(command_line) in
       the message will be replaced with respectively the user (of the form "Real Name (username)" or just "username"
       if there is no real name for the username), the binary to execute (a fully-qualified path, e.g.
       "/usr/bin/pk-example-frobnicate") and the command-line, e.g. "pk-example-frobnicate foo bar". For example, for
       the action defined above, the following authentication dialog will be shown:

           [IMAGE][2]

               +----------------------------------------------------------+
               |                     Authenticate                     [X] |
               +----------------------------------------------------------+
               |                                                          |
               |  [Icon]  Authentication is required to run the PolicyKit |
               |          example program Frobnicate                      |
               |                                                          |
               |          An application is attempting to perform an      |
               |          action that requires privileges. Authentication |
               |          is required to perform this action.             |
               |                                                          |
               |          Password: [__________________________________]  |
               |                                                          |
               | [V] Details:                                             |
               |  Command: /usr/bin/pk-example-frobnicate                 |
               |  Run As:  Super User (root)                              |
               |  Action:  org.fd.pk.example.pkexec.run-frobnicate        |
               |  Vendor:  Examples for the PolicyKit Project             |
               |                                                          |
               |                                  [Cancel] [Authenticate] |
               +----------------------------------------------------------+

おすすめ記事