sudo in php exec()

sudo in php exec()

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

sudo in php exec()

When a command works flawlessly in your local terminal but vanishes into silence when invoked via PHP's exec() or system(), it is almost always an environment mismatch rather than a PHP bug. The specific error message you are encountering—no tty present and no askpass program specified—is the smoking gun. It indicates that the underlying process lacks a controlling terminal (TTY), which breaks interactive authentication flows like password prompts for sudo.

This isn't simply a matter of "missing the password." While configuring /etc/sudoers with %admin ALL=(ALL) NOPASSWD: ALL is a valid strategy to remove the password requirement, it does not fix the TTY issue. The error message explicitly states that no TTY is present. When you run sudo osascript myscript.scpt in a terminal session, your user session provides the necessary TTY environment variables and context. When PHP spawns a process via exec(), it runs as a detached daemon with no associated terminal. Consequently, even if you grant passwordless sudo access, the system refuses to execute the command because the security policy for sudo requires a valid TTY unless specific flags are used or the environment is simulated.

The most robust solution involves bypassing the interactive nature of sudo entirely by using the -S (stdin) flag combined with a non-interactive authentication method, or better yet, running the command as the target user without invoking sudo if policy permits. However, since your requirement dictates that sudo is necessary to invoke the specific system response for AppleScript, you must address the TTY requirement programmatically.

One standard approach in PHP is to pipe the password into stdin using the -S flag. This requires storing the sudo password in your application, which introduces a significant security risk if not handled with extreme care (e.g., encrypting the password or using a dedicated vault). A more secure architectural pattern involves creating a specific sudoers rule that allows execution of the command without a password but still respects the TTY constraint by forcing the creation of a pseudo-TTY.

To achieve this in an environment where sudo is strictly required, you can configure /etc/sudoers to allow your application user (or group) to run the specific binary with a forced TTY allocation:

# Allow user 'app' to run sudo commands with a pseudo-TTY
%app ALL=(ALL) NOPASSWD: /usr/bin/sudo -E -H osascript %s

Note the -E (preserve environment) and -H (run as home directory) flags. However, simply adding these flags might not be enough if the underlying sudo binary strictly rejects execution without a TTY unless invoked via -p or specific PAM configurations.

If modifying system sudoers is not an option, you can try using proc_open() instead of exec(). Unlike exec(), which spawns a process and detaches it from the current environment, proc_open() allows you to explicitly define input/output streams. You could theoretically pipe credentials in or simulate a TTY environment by redirecting file descriptors, though this adds significant complexity to your codebase.

The most pragmatic fix often lies in ensuring the PHP process itself is running within an environment that mimics a terminal session. If you can install sudo with the tty module compiled in (which is default on modern Linux distributions), and configure the sudoers file to allow NOPASSWD for the specific command, you may still need to ensure the env_keep or env_reset policies do not strip necessary variables.

In summary, the disconnect between your terminal and PHP execution stems from the absence of a controlling terminal. Simply adding a password does not work because sudo checks for TTY presence before prompting. The correct path forward is either to restructure the sudoers rules to allow the command without a password while ensuring the security context matches your requirements, or to utilize proc_open() with explicit stream handling if piping credentials is acceptable for your specific security model.

Our network