Salandora / octoprint-customControl

This plugin makes Custom Controls editable through the OctoPrint WebUI
GNU Affero General Public License v3.0
26 stars 15 forks source link

Running command as root #27

Closed Jademalo closed 5 years ago

Jademalo commented 5 years ago

Is there any way to run a script or command as root?

I want to add a button that restarts the webcam service, but that requires running it as root. Sudo also doesn't work since it requires a password input to function.

Thanks!

Uinden commented 5 years ago

@Jademalo Can't tell if this is best way to do it but I did something like this using setuid bit.

Create restart.c file with

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
    setuid( 0 );   // you can set it at run time also
    system( "service webcamd restart" );
    return 0;
 }

Compile gcc restart.c -o restart (you need to have build-essential installed apt-get install build-essential) Set owner and bits chown pi:pi ./restart (I am not sure if user pi runs this, set it to user who will run it) chmod 4755 ./restart After that user pi should be able to run ./restart and it will execute service webcamd restart with root account.

I did not tested this! But if something will not work you can google how to use setuid bits.

Salandora commented 5 years ago

@Jademalo well sudo is exactly what you're looking for. You can configure sudo to not need a password for specific commands just google for it.

Salandora commented 5 years ago

btw this might be the wrong repository, I guess you wanted to open the issue at the systemCommandEditor plugin instead?