I can’t count the amount of times Google’s answered my questions by mining old forum posts and solved my problem. Today yet another one…

One of my scripts needs to take ownership of a file to get access to it so I have the apache user set up in /etc/sudoers to be able to use chown with no password so that it can take ownership of the file and do what it needs to do. The problem was when I switched servers the new server being CentOS has a line in the sudoers file which makes sudo only work on a terminal session.
Defaults requiretty

Unfortunately if your command is run using PHP’s exec() function then you don’t get a response back from sudo because it comes back as an error so it’s not until you run the function with the error catching on…
<?php exec("/usr/sbin/sudo /bin/chown username /file/to/own 2>&1“, $output); ?>
that the output will actually have a helpful error message in it telling you just that; “sudo: sorry, you must have a tty to run sudo” which obviously can’t happen from the web without removing the above offending line from /etc/sudoers

Here’s my helpful forum post from 2007

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>