Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking for a clean and secure way to run specific Linux system commands from a web interface.

Let's say I would like to sudo -u differentuser ping The IP address is supplied by user in a HTTP request and I'd like to print the result on a web page. What would be the best way to do this?

I was thinking about using PHP's exec() command :

echo exec('sudo -u differentuser ping ' . $_POST['ip']);

But this seems very dirty and unsecure and I'd have to add the apache user to the sudoers file.

What language would you use and how would you achieve this?

Thanks for your help.

Edit : the sudo is important here, because I need to run those commands as a specific user different from the one used to host the web interface.

share|improve this question
I wouldn't. That is a security nightmare right there. If I did need to run a very specific set of commands, I would have the commands themselves hardcoded, and I certainly would not pass the parameters straight through without checking them first. (Also, I would never use PHP since that language itself contains enough security holes to sail a moderately large container ship through.) – Jenny D Jul 17 '13 at 9:14

Usualy is not safe. It doesn't matter if you let users send commands or any other kind of interactivity. Even if your script runs alone, exploits can be invented to make use of it in one form or another and maybe alter it's actions.

But, this applies only if you want to have insane security rules on your server. In real world, the chance is minimal that you can compromise your server security.

I still have some suggestions for you :

make sure yo

  1. Make sure your script does not accept any input from outside, it does not read a database or a file. Everything must be enclosed inside the script.

  2. Try to put the script somewhere outside the documentRoot so it won't be accesible by users.

  3. Put some special permissions on the script so that it's actions are limited to the user it runs as. Even if someone breaks it somehow, the OS will not let him do something else than running just that particular command in a particular environment.

This of course may be completed with more rules, but this is just what comes in mind now.

share|improve this answer

Put your commands in a shell script and do something like this:

chown root:wheel
chmod +xs

And then run that script in your php script by exec()

This should work :-)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.