开发者

Add a user to Ubuntu server from PHP script

开发者 https://www.devze.com 2023-02-22 05:24 出处:网络
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

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");
0

精彩评论

暂无评论...
验证码 换一张
取 消