14

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

1
  • pkexec needs a policy file to work, you would need to create one, some info here - freedesktop.org/software/polkit/docs/0.105/pkexec.1.html or use existing ones to see how done, - /usr/share/polkit-1/actions. Do note that policies are sometimes better off when part of an app install, (ie. work without issue), if you get a 'Cannot open display' error then there are workarounds such as ubuntuforums.org/…
    – doug
    Jun 27, 2015 at 13:45

4 Answers 4

11

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" "$@"
3
  • It works! The only thing I modified is on the Exec command: I wrote Exec=pkexec /home/natasha/FlashTool/FlashTool like before, because flashtool-pkexec doesn't work on the .desktop file. Thanks to the policy file it works like a charm. Thank you!
    – np_3ka
    Jun 28, 2015 at 11:57
  • @nplezka To run a shell script from a .desktop file, even with #!, you need to prefix the Exec command with /bin/sh.
    – TheSola10
    May 14, 2017 at 15:17
  • Had to run chmod +x to allow my script executing first, but it worked!
    – Gus
    Nov 27, 2020 at 12:42
2

You may also change

Exec=APP_COMMAND

to

Exec=sh -c "pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY APP_COMMAND"

That way you will get asked about the password and the program starts as usual.

(See How to launch application as root from Unity Launcher? for more information and reasoning. TL;DR: sh -c starts it in a shell, where the follow-up code worked, as the op confirmed. pkexec then starts the "APP_COMMAND" as root but needs the environment variables to actually be able to start a GUI application.)

0

sudo -H is enough to start a graphical application preventing changes to the user's configuration files in ~/, since it sets the running environment's home directory to root's home directory:

Exec=sudo -H /home/natasha/FlashTool/FlashTool
4
  • In a desktop file?
    – A.B.
    Jun 27, 2015 at 13:13
  • @A.B. Yes, why not?
    – kos
    Jun 27, 2015 at 13:18
  • @A.B. It's the same as running it in a whatever sh instance (probably with some environment variables set ad hoc, but here's not a problem).
    – kos
    Jun 27, 2015 at 13:33
  • I don't think it's going to ask for your password and launch the application with sudo if you have Terminal=false line in the .desktop launcher (as OP has here). However most probably it would work with Terminal=true instead.
    – pomsky
    Jan 26, 2019 at 13:35
0

The content of the desktop file (/usr/share/applications/Flashtool.desktop or ~/.local/share/applications/Flashtool.desktop).

[Desktop Entry]
Name=Flashtool
GenericName=Flashtool
Comment=Tool for Xperia Devices
Type=Application
Categories=Qt;FileTransfer;Chat
Terminal=false
Icon=/usr/share/icons/Numix-Circle-Light/48x48/apps/nitrotasks.svg
Exec=/home/natasha/FlashTool/FlashTool
Actions=new-root-window

[Desktop Action new-root-window]
Name=New Root Window
Exec=sh -c "pkexec --disable-internal-agent env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY /home/natasha/FlashTool/FlashTool"
Terminal=true

Right-click on the Desktop icon and select New Root Window.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .