简体   繁体   中英

Create User (Bash Script) through Php form

Can anyone help me on this simple thing? I have a php registration form that communicates directly with MySQL database and adds users when someone fulfils the form. The problem starts when I want to also create a home directory for each user (with a bash script) that makes this registration.

What I've done: I created a bash script

#!/bin/bash

sudo useradd -m /home/$1

Whatever parameter I made on this simple script until now didn't work in order to create the user's home directory.

Problems:

  1. Explicit home directory: Your command line syntax is wrong. Try the following instead:

    sudo useradd -m username

    or

    sudo useradd -d /home/username -m username

  2. Is PHP a sudoer? Only certain accounts are able to execute sudo. Check that the account PHP runs under is listed in the sudoers file. You can usually easily the file with

    visudo

  3. Is PHP chroot -ed? If so, you may not be able to create new linux user accounts from scripts with any reasonable code.

A working test

The following worked for me under Debian (without sudo).

newuser.sh

#!/bin/bash
useradd -d /home/$1 -m $1

newuser.php

<?php
  exec('/bin/bash /var/www-scripts/newuser.sh joe');
?>

test invocation: php newuser.php

If you can perform the same tests (but add sudo as needed), then the only reason it would not work when invoked by your web server is a security problem. That could either be a chroot environment, or lacking sudoer privilege.

Additional resources

You can see how other people have solved this problem by reviewing the following:

  1. http://www.weberdev.com/get_example.php3?ExampleID=3766
  2. http://www.linuxforums.org/forum/programming-scripting/176031-solved-crafting-php-script-create-unix-user.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM