Editing .desktop file to run executable as root?
Asked Answered
D

5

5

I have compiled a c program into an executable that I would now like to integrate into the applications menu in Debian 7.4 XFCE. In order to run the application under normal circumstances, I am required to type

sudo myprogram

Now I have created my .desktop file and placed it in /usr/share/applications

[Desktop Entry]
Type=Application
Encoding=UTF-8
Name=myprogram
Comment=configuration loader
Exec=sudo loader
Icon=/usr/share/icons/hicolor/48x48/apps/myprogram.png
Terminal=false
Categories=Development;IDE

The item is added to my applications menu as expected, and the icon shows up properly. The problem, however, is that double clicking the menu item to launch the application does nothing.

If I navigate to /usr/bin (where I have placed my executable) and type "sudo myprogram", the program launches as expected.

What can I do to fix this issue and get the program to launch from the menu? Perhaps /usr/bin is not the correct place to put it, or I have the incorrect Exec command. I greatly appreciate the help.

Debera answered 6/5, 2014 at 15:56 Comment(0)
D
6

I ended up using (after installing gksu)

Exec = gksu myprogram

this launches a graphical sudo prompt, which is sufficient for my needs.

Debera answered 6/5, 2014 at 16:50 Comment(1)
On Ubuntu 18.04 gksu doesn't seem to be available (either by default or in a repo). The above solution by @noraj using pkexec is preferable.Retrochoir
J
4

The pkexec solution from askubuntu:

Exec=pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY APP_COMMAND
Jehiah answered 11/11, 2022 at 16:43 Comment(0)
M
3

This approach does not need additional installation of packages.

Terminal=true opens a new terminal window which runs sudo -i to ask for the password. Then, using sh to run the program, the Terminal is closed and myprogram runs in the background because it has a & at the end.

[Desktop Entry]
Type=Application
Name=...
Exec=sudo -i sh -c "myprogram &"
Terminal=true

Request: Please report if it works under your OS. Tested under:

  • Xubuntu
Mclyman answered 30/6, 2022 at 12:38 Comment(2)
Works in Raspberry Pi OS/Debian 12 (bookworm) on Pi 4bShibboleth
Works on Ubuntu 22.04.4 on waylandDamage
G
1

This is what the setuid bit in the permissions is for. It makes executables run with permissions of the file owner. This only works on actual executables, not on shell scripts!

sudo chmod u+s myprogram
sudo chown root myprogram
./myprogram # now runs as root

Please be careful when using this as it will always execute that program as root no matter who executes it. You can limit access by setting it to your usergroup and deny all execute.

chgrp "${USER}" myprogram # provided you have individual groups set up
chmod a-x myprogram       # deny all execute
Gangling answered 6/5, 2014 at 16:30 Comment(0)
P
-3

Try adding this to .desktop

Path=/path/to/myprogram

Pluck answered 6/5, 2014 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.