Add a user to Ubuntu server from PHP script
I am trying to write a PHP script which executes a shell command (via system()
?) to add a new user to the server. I am thinking about using useradd
or adduser
but don't know how I will get it to work. What flags are necessary to execute useradd
in a script? And how would I 开发者_如何学Goset the permissions on the script so it executes properly? This doesn't work:
<?
$user = $_GET['user'];
system("sudo useradd -m -p 4dk/kBWvKaP52/POJYOZGLam8qZnCkQtdw== $user; echo $user");
?>
You'll need to add a line to /etc/sudoers
like
www-data ALL=(root) NOPASSWD: /usr/sbin/useradd
This allows www-data to run the useradd command.
You may also need to comment out this line, if it exists in /etc/sudoers
:
Defaults requiretty
By the way, it's a good idea to escape your inputs with escapeshellarg():
$user = escapeshellarg($_GET['user']);
system("sudo useradd -m -p 4dk/kBWvKaP52/POJYOZGLam8qZnCkQtdw== $user; echo $user");
精彩评论