0

I'm trying to run a script as root user. here is my code

echo shell_exec("sudo bash.sh 2>&1");

It's giving error

sudo: no tty present and no askpass program specified
www-data is not in the sudoers file.  This incident will be reported.

I've followed these methods as well but end up with no permission error

7
  • sudo is interactive, and you're running it from a script, that's why it fails, there is no way for the script to supply the password. Is it possible for you to run PHP with sudo, in which case the script would have root permissions anyway? eg sudo php script.php Commented May 23, 2014 at 12:19
  • 1
    actually it depends on how the system is configured. you can make sudo work without asking a password Commented May 23, 2014 at 12:20
  • 1
    post your entry in /etc/sudoers Commented May 23, 2014 at 12:20
  • 1
    @LorenzoMarcon yes youre right, just reading the man page now. You could use the -S option (I think) to pass in the password via stdin, so you could do this in a one-liner. Have a look at the manual: sudo.ws/sudo/sudo.man.html Commented May 23, 2014 at 12:22
  • although the security implications of actually doing that are monstrous. please don't, atleast in a production environment. Commented May 23, 2014 at 12:23

1 Answer 1

1

There are a couple of issues you might encounter:

  • The user that is running the php process must have sudo rights (check with visudoers command)
  • There is no environment set, so the $PATH variable does not include the path to the sudo command
  • sudo might require a password. Either change the sudoers file, adding NOPASSWORD, which would be hugely unsafe. Or you have to use pipes (proc_open), and pass the password through the stdin pipe

I've managed to find a way to do so, but after some help from people on this site:

load .profile with proc_open()
proc_open interaction

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.