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?:

  • 1
    You mean like sudo?
    – terdon
    Jun 18, 2016 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, 2016 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? Jun 19, 2016 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, 2016 at 5:35
  • 1
    @BryceGuinta: In your program, add setuid(0); near the beginning and once compiled, chown root program; chmod 4755 program; Jun 19, 2016 at 16:23


Browse other questions tagged .