Jump to content

[SOLVED] Struggling with exec() and sudo


LuAn

Recommended Posts

I want to run a couple of linux commands from within PHP but they need root access to run them.

 

The following code works and tells me that I am 'apache':

 

[pre]$output = exec('/usr/bin/whoami');[/pre]

 

The following code does not work and $output is empty:

 

[pre]$output = exec('/usr/bin/sudo /usr/bin/whoami');[/pre]

 

I have added the following line to /etc/sudoers

 

[pre]apache ALL = (root) NOPASSWD: /usr/bin/whoami[/pre]

 

I'm on Fedora Core 6 but selinux is set to permissive so it's not that.  I've spent the last 6 hours scouring the web for an answer but everything that I'm seeing tells me that what I've done is the way to do it.  Any ideas what I'm doing wrong?

Link to comment
Share on other sites

I just nailed this.  The breakthough came when I tried:

 

[PRE]$output = exec('/usr/bin/sudo /usr/bin/whoami 2>&1');[/PRE]

 

The result should be 'root' but what I got, courtesy of the bit on the end, was this: 'sudo: sorry, you must have a tty to run sudo'

 

A search on that told me that the issue related to a setting in /etc/sudoers which is used in Fedora Core 6 namely:

 

[PRE]Defaults    requiretty[/PRE]

 

Apparenly FC6 is the only OS that has that setting (which explains why I was struggling to find any information about what was causing the problem).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.