1

I'm trying to get a sudo command to execute from within PHP. For simplicity sake, I'm using passthru wrapped in pre tags. Long story short, I want to get some exim stats.

I'm on a CentOS box.

After logging in via shell, I added the following line via visudo.

myuser      ALL = NOPASSWD: /usr/sbin/exiwhat

I can successfully execute the exiwhat command using either of the following commands when shelled in.

sudo -u myuser -H /usr/sbin/exiwhat
sudo -u myuser -H exiwhat

I've even tried becoming the the user via su and am still successful.

su myuser
sudo /usr/sbin/exiwhat
-or-
sudo exiwhat

My php script looks a little something like this.

<?php
echo '<pre>';
passthru('whoami');
echo "\n1)";
passthru('sudo exiwhat');
echo "\n2)";
passthru('sudo /usr/sbin/exiwhat');
echo "\n3)";
passthru('exiwhat');
echo "\n4)";
passthru('/usr/sbin/exiwhat');
echo "\n";
echo '</pre>';

All I get back is:

myuser

1)
2)
3)
4)No exim process data

I've tried the same with

exec('exiwhat',$output);

but output returns as an empty array.

I can get simple things like "ps aux" or "dir" to work just fine. Please help.

Possible Duplicate: How to call shell script from php that requires SUDO?

Note: I can run exiwhat via command line and get a good chunk of output. We have several hundred emails in the queue at any point in time.

UPDATE: Per another thread, I added " 2>&1" to each command and got the following

myuser

1)sudo: sorry, you must have a tty to run sudo

2)sudo: sorry, you must have a tty to run sudo

3)sh: exiwhat: command not found

4)/bin/rm: cannot remove `/var/spool/exim/exim-process.info': Permission denied
exim(770): Operation not permitted
exim(8016): Operation not permitted
exim(15618): Operation not permitted
exim(15626): Operation not permitted
exim(16751): Operation not permitted
exim(16765): Operation not permitted
exim(32207): Operation not permitted
exim: no process killed
No exim process data

UPDATE: I added the following line via visudo and my sudo commands now work

Defaults:myuser !requiretty

Is that save to leave like that though?

1
  • Further note: Running mod_suphp, so each process should run as its respective user, hopefully as indicated by the whoami command. Commented Apr 4, 2014 at 18:26

1 Answer 1

2

Edit your /etc/sudoers file and remove the line about Default requiretty.

**Update I guess this options enabled by default so add something like the following instead:

Defaults:myuser        !requiretty
Sign up to request clarification or add additional context in comments.

1 Comment

Found that over at the following link and got it in just before you posted. serverfault.com/questions/111064/…

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.