I'm writing a program that requires root access. I'm hoping to use polkit for privilege escalation instead of just erroring out when the program is not run from root or via sudo.

Is polkit used on all major linux distributions? If not, is there some other generic way to ask for privilege escalation on linux?

Is polkit the only way do do this?:

closed as primarily opinion-based by Thomas Dickey, Archemar, slm Jun 19 '16 at 7:08

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    You mean like sudo? – terdon Jun 18 '16 at 23:50
  • 1
    Yes, I was thinking of something like [ $(id -u) = 1 ] && command || sudo command. It depends on how much of your program needs elevated privileges. If all of it, you could just have a wrapper script that runs [ $(id -u) = 1 ] && your_program || sudo your_program. On the other hand, I know nothing about polkit and it may very well be the better solution (hence this comment instead of an answer). – terdon Jun 18 '16 at 23:59
  • 2
    @BryceGuinta: Since it's your program and you apparently want it to run as root all the time, why don't you setuid it to root? – Julie Pelletier Jun 19 '16 at 0:35
  • 1
    making allowances for users too lazy or forgetful to use sudo (or whatever) when appropriate is an even bigger can of worms. just check for euid != 0 and exit with "root privs required" error message. – cas Jun 19 '16 at 5:35
  • 1
    @BryceGuinta: In your program, add setuid(0); near the beginning and once compiled, chown root program; chmod 4755 program; – Julie Pelletier Jun 19 '16 at 16:23