通过 pkexec 在 .desktop 中运行 sudo Exec:Qt 错误

通过 pkexec 在 .desktop 中运行 sudo Exec:Qt 错误

我在 virtualbox 中有一个配置,要求我以 sudo 权限运行应用程序。我一直在编辑.desktop文件以使用 sudo 运行(请注意,即使对 sudoers 文件进行了适当的更改,简单地将变量更改ExecExec=sudo virtualbox也不起作用):

$ cat /usr/share/applications/virtualbox.desktop 
[Desktop Entry]
Name=VirtualBox
GenericName=PC virtualization solution

Type=Application
Exec=virtualbox %U
TryExec=VirtualBox
Keywords=virtualization;
Keywords[de]=Virtualisierung;
Keywords[ru]=виртуализация;
MimeType=application/x-virtualbox-vbox;application/x-virtualbox-vbox-extpack;application/x-virtualbox-ovf;application/x-virtualbox-ova;
Icon=virtualbox
Categories=Emulator;System;Utility;
Actions=RootManager;Manager
Comment=Run several virtual systems on a single host computer
Comment[de]=Mehrere virtuelle Maschinen auf einem einzigen Rechner ausführen
Comment[it]=Esegui più macchine virtuali su un singolo computer
Comment[ko]=가상 머신
Comment[pl]=Uruchamianie wielu systemów wirtualnych na jednym komputerze gospodarza
Comment[ru]=Запуск нескольких виртуальных машин на одном компьютере
Comment[sv]=Kör flera virtuella system på en enda värddator

[Desktop Action Manager]
Exec=virtualbox
Name=Open Oracle VM Manager
Name[de]=Oracle VM Manager öffnen

[Desktop Action RootManager]
Exec=gnome-terminal -e "bash -c 'sudo -i /usr/bin/virtualbox;'"
Name=Open Manager as Admin

上面的方法有效,目前是一个不错的解决方法,但请注意身份验证发生在 gnome 终端中。我想用pkexec(eg Exec=pkexec virtualbox) 绕过这个问题,但出现以下错误:

 ~$ pkexec virtualbox
Qt WARNING: could not connect to display 
Qt FATAL: This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, xcb.

Aborted (core dumped)

我已经确定了一些可能的罪魁祸首/解决方案:

我有一种感觉,我的问题与第一个链接没有太大关系,因为应用程序在不使用pkexec.第二个链接将自定义操作添加到 freedesktop 策略包中,这看起来非常复杂,但可能正是我所需要的。

有什么见解吗?

答案1

第二个链接有效:

$ cat /usr/share/polkit-1/actions/org.freedesktop.policykit.virtualbox.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.virtualbox">
    <description>Run VirtualBox as Admin</description>
    <message>Authentication is required to run VirtualBox as administrator</message>
    <icon_name>virtualbox</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/virtualbox</annotate>
    <annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
    </action>
</policyconfig>

$ cat /usr/share/applications/virtualbox.desktop 
[Desktop Entry]
Name=VirtualBox
GenericName=PC virtualization solution

Type=Application
Exec=pkexec virtualbox %U
TryExec=VirtualBox
Keywords=virtualization;
Keywords[de]=Virtualisierung;
Keywords[ru]=виртуализация;
MimeType=application/x-virtualbox-vbox;application/x-virtualbox-vbox-extpack;application/x-virtualbox-ovf;application/x-virtualbox-ova;
Icon=virtualbox
Categories=Emulator;System;Utility;
Actions=RootManager;Manager
Comment=Run several virtual systems on a single host computer
Comment[de]=Mehrere virtuelle Maschinen auf einem einzigen Rechner ausführen
Comment[it]=Esegui più macchine virtuali su un singolo computer
Comment[ko]=가상 머신
Comment[pl]=Uruchamianie wielu systemów wirtualnych na jednym komputerze gospodarza
Comment[ru]=Запуск нескольких виртуальных машин на одном компьютере
Comment[sv]=Kör flera virtuella system på en enda värddator

[Desktop Action Manager]
Exec=virtualbox
Name=Open Oracle VM Manager
Name[de]=Oracle VM Manager öffnen

[Desktop Action RootManager]
Exec=pkexec virtualbox
Name=Open Oracle VM Manager (admin)

相关内容