node.js - 如何使用 nodejs 在 Linux 中创建新用户

标签 node.js linux ubuntu child-process

下面是我用来运行它运行的 linux 命令的代码,但是在终端中没有显示任何内容来设置新密码,没有分组。终端永远运行并且不会退出。

   const child = execFile('sudo',['adduser', 'gku5'], (error, stdout, stderr) => {
    if (error) {
      throw error;
    }
   console.log(stdout);
   }); ```

最佳答案

该命令不是adduser,而是useradd。例如,我想添加名为 kbr 的新用户。命令将是这样的-

sudo useradd kbr
你的程序应该是这样的——

   const child = execFile('sudo',['useradd', 'gku5'], (error, stdout, stderr) => {
    if (error) {
      throw error;
    }
   console.log(stdout);
   });

关于输出。 useradd 在 RHEL 中显示输出,但在其他发行版中不会发生。请引用this answer为了这。
关于输出,您可以 cat auth.log 文件以显示输出。
例如,cat /var/log/auth.log | tail -1 会显示,像这样的东西,
Jan 27 19:04:16 useradd[32328]: failed adding user 'kbr', data deleted
所以,程序应该是这样的——

child = exec('sudo useradd kbr | cat /var/log/auth.log | tail -1',
    function (error, stdout, stderr) {
        console.log('output: ' + stdout);
        if (error !== null) {
             console.log('exec error: ' + error);
        }
    });

关于node.js - 如何使用 nodejs 在 Linux 中创建新用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65919315/

相关文章:

linux - 在 bash 中使用列

linux - 通配符 TDM400P REV H 数字卡配置

javascript - 主干 .save() 上的 404 错误

node.js - Sequelize : how to count objects and update the rank?

javascript - Node 服务器 : Loading module was blocked because of a disallowed MIME type (“text/html” )

linux - 通过 weblogic 中的 linux 脚本更新部署

c++ - node.js 内部 : How can I find out where `process.binding(' eval')` gets defined?

linux - Debian:更新软件包时/var/lib/<package> 会发生什么?

linux - 无法安装 Apache2

ubuntu - 尝试将 LibreOffice (soffice) 作为 systemctl 服务运行失败