Running a system command from php

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
Amul Adagale
Forum Newbie
Posts: 2
Joined: Thu Jun 25, 2009 10:05 am

Running a system command from php

Post by Amul Adagale »

Hi,
I am new to php and web development. I wanted to run a system command from php script. I am using shell_exec() to run the command, but i need root privileges to execute it. I dont want to make 'Apache' a 'sudo user' nor do I want to change the default 'apache user' to root.

Please can someone suggest me a way in which I will be able to execute the command with root privileges.

Thanks in Advance,
Amul
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Running a system command from php

Post by VladSun »

Amul Adagale wrote:... the command, but i need root privileges to execute it...
What is the command?
Amul Adagale wrote:... I don't want to make 'Apache' a 'sudo user' ...
Well, it is the best way to do it - sudo has a powerful wrapper.
Amul Adagale wrote:... nor do I want to change the default 'apache user' to root...
I doubt, one is even able to do it ;)

You may make your executable file SUID-ed (this means that any user on the system can execute it with root privileges) ...
Or you can write a wrapper script (arguments validation, user ID check, etc.) and SUID it - i.e. to "write" a small sudo-like tool ;)
There are 10 types of people in this world, those who understand binary and those who don't
Amul Adagale
Forum Newbie
Posts: 2
Joined: Thu Jun 25, 2009 10:05 am

Re: Running a system command from php

Post by Amul Adagale »

Hi,

Thanks for your reply.
The command actually is my executable in which I am calling 'mkdir' and some other file handling commands. I have been trying to setuid for the script and the executable but it didnt seem to work.
I have found a solution to the problem though. I am writing a wrapper to my executable which is owned by a sudo user. In this wrapper I am calling the executable with a sudo in it. It seems to be doing the job. :)

Thanks
Amul
Post Reply