Unable to delete Linux user using PHP and bash script

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

del_user.sh is as below

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?


  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.

Need Your Help

AVPlayer crashes after multiple playbacks-

objective-c ios avplayer

I'm trying to create an app which plays videos from a file using AVFoundation. The videos are shown in a view accessed by tapping on a row in a parent tableview. The real app will have a video fo...

Sharing dynamically allocated arrays between C++ source files

c++ arrays

I am trying to compile a code with two (more actually, but that is not important) source codes. In one of them I declared a variable size array like this: