简体   繁体   English

如何使用nodejs在Linux中创建新用户

[英]How to create new user in Linux using nodejs

Below is the code i'm using to run linux command it run but, doesn't show anything in terminal to set new password, group nothing.下面是我用来运行它运行的 linux 命令的代码,但是在终端中没有显示任何内容来设置新密码,没有任何分组。 terminal just runs forever and doesn't exit.终端永远运行并且不会退出。

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

The command is not adduser, but useradd.该命令不是adduser,而是useradd。 For instance, i want to add new user named kbr.例如,我想添加名为 kbr 的新用户。 The command will be like this-命令将是这样的-

sudo useradd kbr

Your program should look like this-你的程序应该是这样的——

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

And about the output.关于 output。 useradd shows output in RHEL, but it doesn't happen in other distribution. useradd 在 RHEL 中显示 output,但在其他发行版中不会发生。 Please refer to this answer for this.请参阅此答案

About the output, you can cat the auth.log file to show the output.关于output,可以cat auth.log文件显示output。 For instance, cat /var/log/auth.log | tail -1例如, cat /var/log/auth.log | tail -1 cat /var/log/auth.log | tail -1 would show, something like this, cat /var/log/auth.log | tail -1会显示,像这样,

Jan 27 19:04:16 useradd[32328]: failed adding user 'kbr', data deleted

So, the program should look like this-所以,程序应该是这样的——

 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); } });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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