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
https://forums.phpfreaks.com/topic/57958-solved-struggling-with-exec-and-sudo/
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).

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.