简体   繁体   English

编辑 .desktop 文件以以 root 身份运行可执行文件?

[英]Editing .desktop file to run executable as root?

I have compiled ac program into an executable that I would now like to integrate into the applications menu in Debian 7.4 XFCE.我已将 ac 程序编译为可执行文件,现在我想将其集成到 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 文件并将它放在 /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.如果我导航到 /usr/bin(我放置了我的可执行文件)并输入“sudo myprogram”,程序会按预期启动。

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.也许 /usr/bin 不是放置它的正确位置,或者我的 Exec 命令不正确。 I greatly appreciate the help.我非常感谢帮助。

I ended up using (after installing gksu)我最终使用(安装 gksu 后)

Exec = gksu myprogram

this launches a graphical sudo prompt, which is sufficient for my needs.这会启动一个图形 sudo 提示符,这足以满足我的需要。

This is what the setuid bit in the permissions is for.这就是权限中的setuid位的用途。 It makes executables run with permissions of the file owner.它使可执行文件以文件所有者的权限运行。 This only works on actual executables, not on shell scripts!这仅适用于实际的可执行文件,不适用于 shell 脚本!

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.使用它时请小心,因为无论谁执行它,它都会以 root 身份执行该程序。 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

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. Terminal=true打开一个新的终端窗口,运行sudo -i来询问密码。 Then, using sh to run the program, the Terminal is closed and myprogram runs in the background because it has a & at the end.然后,使用sh运行程序,终端关闭, myprogram在后台运行,因为它的末尾有一个&

[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许本图

尝试将此添加到.desktop

Path=/path/to/myprogram

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM