【问题标题】:How to add user in linux pc using shell script in php如何在 php 中使用 shell 脚本在 linux pc 中添加用户
【发布时间】:2016-02-02 07:38:04
【问题描述】:

我正在尝试将用户添加到我的 LAN pc(ubuntu 14.04) 但失败了。 比如说:

mypc : 192.168.0.11 serverpc : 192.168.0.100

我可以使用 shell 脚本将用户创建到“mypc”中。但是当我尝试连接并将用户添加到“serverpc”时:它返回“添加用户失败!”。我正在使用 PHP:

这是我的方法:

public function test_ldap(){

    $username = escapeshellarg('different');
    $password = escapeshellarg('different');
    #$udomain = 'edutechsolutionsbd.com';
    $u_domain = '192.168.0.100';
    $u_user = 'etsb';

    $output = exec("sudo shell_script/add_user.sh $username $password $u_domain $u_user 2>&1");
    #$output = shell_exec("/usr/bin/php -v");
    print_r($output);
    exit;

}

shell脚本::

#!/bin/bash
# Script to add a user to Linux system
# -------------------------------------------------------------------------
# Copyright (c) 2007 nixCraft project <http://bash.cyberciti.biz/>
# This script is licensed under GNU GPL version 2.0 or above
# Comment/suggestion: <vivek at nixCraft DOT com>
# -------------------------------------------------------------------------
# See url for more info:
# http://www.cyberciti.biz/tips/howto-write-shell-script-to-add-user.html
# -------------------------------------------------------------------------
if [ $(id -u) -eq 0 ]; then

username=$1
password=$2
u_domain=$3
u_user=$4

egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
    echo "$username exists!"
    exit 1
else
    pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
    #useradd -m -p $pass $username
    sudo ssh $u_user@$u_domain "useradd -m -p $pass $username"

    [ $? -eq 0 ] && echo "User is Created Successfully!" || echo     "Failed to add a user!"
fi
else
echo "Only root may add a user to the system"
exit 2
 fi

我的问题:如何在 ubuntu 中将用户创建到我的另一台电脑中?提前致谢。

【问题讨论】:

    标签: php linux shell


    【解决方案1】:

    根据我的经验,PHP 的exec() 函数不支持运行带有sudo 提升权限标志的任务。我添加了www-data 用户,这是Web 服务器应该在其下运行的用户,添加到您使用命令sudo visudo -f /etc/sudoers 在机器上打开的/etc/sudoers 文件中,然后在该行下方includedir /etc/sudoers.d user_name ALL=(ALL)。

    然后我添加了以下行...

    www-data ALL=(ALL) NOPASSWD: ALL
    

    基本上你所做的是将 www-data 用户添加到 sudoers 列表中,并允许它访问 sudo 命令并在提升的权限下运行。

    但是我应该指出,这是一个非常危险的更改,因为这意味着任何人都可以使用 Web 服务器的用户访问提升的权限,如果这是一个面向公众的服务器,这会带来巨大的风险,所以要极端使用它小心。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-02
      • 2019-12-29
      • 1970-01-01
      • 2016-11-13
      • 2014-09-17
      • 1970-01-01
      • 2022-10-17
      • 1970-01-01
      相关资源
      最近更新 更多