Command-Line – Using pkexec Command in a .desktop File

command linedesktoppkexec

I made a .desktop file for Androxyde's Flashtool (utility for Sony Xperia devices that I have to open with an executable file in its folder) that requires root privileges to use fastboot utilities.
I used to make it work with gksu, but I'm on Ubuntu 15.04 and gksu is now old.

I tried to modify the exec line from

Exec=gksu /home/natasha/FlashTool/FlashTool

to

Exec=pkexec /home/natasha/FlashTool/FlashTool

Then, I read about pkexec doesn't allow to run X11 applications and so I override in this way:

enter image description here

Link to full image on Imgur.com

The problem now is: It asks me the password but the Flashtool's GUI doesn't start.
BUT if I execute that command in terminal, the program starts without problems. What can I do?

enter image description here

Link to full image on Imgur.com

Best Answer

Create a new file in /usr/share/polkit-1/actions/

sudo nano /usr/share/polkit-1/actions/FlashTool.policy

and add the lines below:

<?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.run-FlashTool">
    <description>Run FlashTool</description>
    <message>Authentication is required to run FlashTool</message>
    <defaults>
      <allow_any>no</allow_any>
      <allow_inactive>no</allow_inactive>
      <allow_active>auth_admin_keep</allow_active>
    </defaults>
    <annotate key="org.freedesktop.policykit.exec.path">/home/natasha/FlashTool/FlashTool</annotate>
    <annotate key="org.freedesktop.policykit.exec.allow_gui">TRUE</annotate>
  </action>

</policyconfig>

Then create a new file /home/natasha/FlashTool/

nano /home/natasha/FlashTool/flashtool-pkexec

and add the lines below:

#!/bin/sh
pkexec "/home/natasha/FlashTool/FlashTool" "$@"

Use the line below for Exec in your desktop file:

Exec=/home/natasha/FlashTool/flashtool-pkexec

Tested on my system Ubuntu 15.04 GNOME with the following files:


$ cat /usr/share/applications/gedit.root.desktop 
[Desktop Entry]
Name=Gedit as root
GenericName=Text Editor
X-GNOME-FullName=
Comment=
Exec=gedit-pkexec
Icon=gedit
Terminal=false
Type=Application
Categories=GNOME;System;Filesystem;Settings;
StartupNotify=true
X-Ubuntu-Gettext-Domain=gedit

$ cat /usr/share/polkit-1/actions/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.run-FlashTool">
    <description>Run FlashTool</description>
    <message>Authentication is required to run FlashTool</message>
    <defaults>
      <allow_any>no</allow_any>
      <allow_inactive>no</allow_inactive>
      <allow_active>auth_admin_keep</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>

$ cat /usr/bin/gedit-pkexec 
#!/bin/sh
pkexec "gedit" "$@"