title: How to create a ubuntu user from within node
body: <p>I'm trying to add users to the server running my node application. Right now I am using: </p>
  
  <pre><code>exec("useradd -mp {password-hash}")
  </code></pre>
  
  <p>with a hash that gets created by </p>
  
  <pre><code>exec("mkpasswd -m des {password}")
  </code></pre>
  
  <p>This leaves the password visible in the process list, and that should be avoided at all costs.</p>
  
  <p>That's why I am wondering if there is a module on npm or an easier way to add a linux user to the system programmatically (this is necessary because the app I am building creates a user account on the system when a user registers on the web). Can this be done without evoking system commands like I am currently doing? I have found neither npm module nor any information of using node to interact with ubuntu/unix user management.</p>
  
  <h1>Solution</h1>
  
  <p>Using Mustafa's hints from the top answer, I implemented a version of the <code>mkpasswd</code> command using <code>spawn</code>. In CoffeeScript, it would look like this:
  </p>
  
  <pre><code>{spawn} = require 'child_process'
  
  child = spawn 'mkpasswd', ['-m','des','--stdin']
  
  child.stdout.on 'data', (data)-&gt;
    console.log 'password hash arrived: ', data.toString()
  
  child.stdin.write 'password' 
  </code></pre>
  
  <p>Make sure you add the proper error handling event handlers to <code>child.stderr</code> and <code>child.stdout</code> while debugging your code.</p>