How to make an application ask for root credentials?

Hello there,
I’m writing an application and I would like to make it ask for “root” password when launched from a normal user, just as Yast does. How can it be done?

I’m developing with Qt on KDE.

Thank you for any information.
faina

When one calls a program such as Nautilus in KDE, the command is

kdesu nautilus

I don’t code, so I don’t know if that works inside a program, but it works in the shortcut/symbolic link.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Typically easiest to just have it check its UID and then drop out if that
is not 0 (zero) with a message stating to start it again with
su|sudo|gnomesu|kdesu.

Good luck.

faina wrote:
> Hello there,
> I’m writing an application and I would like to make it ask for “root”
> password when launched from a normal user, just as Yast does. How can it
> be done?
>
> I’m developing with Qt on KDE.
>
> Thank you for any information.
> faina
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJLbBUlAAoJEF+XTK08PnB5FkMP/jpU1tlPj6dAV0KDkFQNe/4p
3IHIsTgz4tpvDqvJ5M+T0OI3fp9pDSEbzS5LZ5YJDkCpRYwyoAsPAXy2Xkfp2xlr
SjCZLGDr/bIXbGEgPRBHloTcYiXX+UHNGbP6bI21Y9Z4LjVTIbYamJceEC++aORo
QkaAj6B+LG3woX4mpIZzLNr0+h/lfe8ycKclPQ2rRWD9dBcUDp0vTgfzDyUTdO+I
0YGX9Igb3g8b3hgeUJqP2CMHtaThgFmI5wP2KSBfCoGcrZ5H2an1foxZZAYDvaQQ
LBxdABs720I8ttcl7e9UiCAW1rKtTBwxHN5ZPtUcV9pDK+ywdqXMzv8JPQPPcoRL
DYJNNwbOewX1Zq6xQQOs2rO2ume0hRN0qaK2bK9SoVWXTAUIycVWIDMG34pqB5wz
LAcil3lbo2+Pw3bo/uVt3pHOi9hMupF6BBnREe+6DkgRBoxLe2HfEI7zxMkISnfm
QOS7zGypJPT3HL+69riu0NeBRX/vQ2l9Wp3TprjsRHXxSePeuLeeXNTLH+Ukql5E
5YLsIk1EjvY9hHS9vxZ1tB9ch2EI/vKQC84SJN5GnhLgEoSaQyLfCz7OLhhkORxK
N25nGlDg6r6IEcxFitSHjcLqmc4b5Y22L92cuzYt/s1tErMg2wpysGE4X7FwiuTG
o9xN2Pis1a+nR0k0AwoH
=7ePZ
-----END PGP SIGNATURE-----

The most portable way is to use the following in your .desktop entry.


Exec=/usr/bin/xdg-su -c <program>

This should use the appropriate dialog for the desktop environment (otherwise if you use gnomesu, kde users will get an unfamiliar dialog and vice versa).

It looks like there are other solutions, have a look at /usr/share/applications/wireshark.desktop which uses X-KDE-SubstituteUID=true but thats not portable https://features.opensuse.org/307769

Thank you everybody!
Max

Nice, I was not aware of the xdg-utils until now.

I wasn’t either. I am going to play with that when I get home.:wink:

This is very useful. In fact, I wanted such a thing in one of my installation program.