I need to run a sudo command from a PHP script in CentOS 7, but I am getting an error:

sudo: no tty present and no askpass program specified

I have edited my sudoers file with the following lines, as others have advised:

  1. don't require tty:

    Defaults:apache !requiretty

  2. Set no password for apache user:

    apache  ALL=(root) NOPASSWD: /path/to/app, /path/to/app2

(I have double-checked that these paths are correct). My errors persist. Any thoughts?

  • 1
    Is that the only sudoers line for apache? Could it be hitting another rule?
    – Jeff Schaller
    Sep 25 '18 at 20:17
  • It is the only line, yes Sep 26 '18 at 8:46
  • You would better find another way of doing that. Apache has another user for security reasons, giving sudo to Apache is a very bad idea. Sep 26 '18 at 11:39
  • apache = NOPASSWD: /path/2/app, /path/2/app2 could you try this? Sep 26 '18 at 12:17

This line does not give sudo permissions to app and app2:

apache  ALL=(root) NOPASSWD: /path/to/app /path/to/app2

What the above line does is give sudo permissions to run /path/to/app, only if you also pass a single argument to it of /path/to/app2. To specify multiple commands, they need to be separated by a comma:

apache  ALL=(root) NOPASSWD: /path/to/app, /path/to/app2
  • ah, that was just a typo in my post, which I've now changed Sep 26 '18 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.