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

Community
  • 1
  • 1
  • 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` – totallyNotLizards May 23 '14 at 12:19
  • 1
    actually it depends on how the system is configured. you can make sudo work without asking a password – Lorenzo Marcon May 23 '14 at 12:20
  • 1
    post your entry in /etc/sudoers – Gerald Schneider May 23 '14 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: http://www.sudo.ws/sudo/sudo.man.html – totallyNotLizards May 23 '14 at 12:22
  • although the security implications of actually doing that are monstrous. please don't, atleast in a production environment. – totallyNotLizards May 23 '14 at 12:23
  • @jammypeach that's wrong, you can pipe username and password to sudo with `proc_open()`. – Daniel W. May 23 '14 at 12:29
  • @DanFromGermany which part is wrong? my first comment is but is the second wrong also? I didn't know you could use `proc_open()`, thanks for that. – totallyNotLizards May 23 '14 at 12:48

1 Answers1

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

Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149