PHP: execute a PHP script with root privileges

sudoers is a list of which users may execute what. The sudoers file should always be edited by the visudo command which locks the file and does grammatical checking. It is imperative that sudoers be free of syntax errors since sudo will not run with a syntactically incorrect sudoers file.

If you want a script to be executed (either by command line or by the webserver) with root privileges add the following line to /etc/sudoers:

daemon ALL=NOPASSWD: /usr/bin/svnadmin

Also comment out the following line otherwise you’d get “sudo: sorry, you must have a tty to run sudo”:

  #Defaults    requiretty

Be sure and secure your script with filters and such to prevent people from adding in extra commands. For more information check the Ubuntu documentation.

Leave a Reply