1

I'm able to delete linux users directly from the shell using the command ./del_user.sh

del_user.sh is as below

#!/bin/sh
userdel username

When I run this script from a PHP page, it doesn't work.

echo shell_exec('./del_user.sh');

Both the files are in the same directory.

What could be the problem?

5
  • 1
    does www-data user have permission to run the script? Commented May 1, 2013 at 13:17
  • 3
    Its permissions, you may be able to delete a user when you are root but the webserver will not have and indeed should not have permissions to do so Commented May 1, 2013 at 13:17
  • where should i set the permission? Commented May 1, 2013 at 13:22
  • Try google.com it has the answer for how to create and delete linux users from php. That said, you will have to be so careful with your code as one slip up and you can find the root user or web user deleted and then you have a whole world of hurt. Commented May 1, 2013 at 13:29
  • Thanks to Waygood and Anigel.. it is the permission issue. Commented May 2, 2013 at 5:53

1 Answer 1

1
  1. del_user.sh does not take an argument so you're running the script on whatever hardcoded user is in the script. This won't work unless you modify the script to allow a command line argument.
  2. While this task can be accomplished by PHP, this is certainly not a primary function and should probably be delegated to a more suitable application.

As an aside, you haven't stated what your goal is or what this script is supposed to do. Is this part of some user management application or is it simply a one-off for some small task?

Answer: To enable this, you'd have to give Apache the ability to sudo so it can gain temporarily raised priveleges. You may have to do some googling on how to do this depending on your OS but this link provides some direction on how to do it in Ubuntu: Ubuntu Forums

I would also recommend against using a bash script as its not really necessary to do this. You could use a PHP script that accepts a command line argument. So in your main script you could have something like this: shell_exec('php /path/to/del_user.php username');

Then in del_user.php you'd have something like shell_exec('userdel '.$argv[1]);. More info on commandline arguments can be found here: $argv

Lastly, you could put it directly into your main script instead of using shell_exec twice. In other words, just use shell_exec('userdel '.$username); instead of calling a script. Since Apache will be able to sudo, it should be able to execute this directly.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it is part of user management application. The user should be able to Add, Edit and Delete linux users from the web interface. can you please send me more info on how this could be achieved?

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.