setuid

process.setuid(id)

Sets the user identity of the process. (See setuid(2).) This accepts either a numerical ID or a username string. If a username is specified, this method blocks while resolving it to a numerical ID.

console.log('Current uid: ' + process.getuid());
try {
  process.setuid(70);
  console.log('New uid: ' + process.getuid());
}
catch (err) {
  console.log('Failed to set uid: ' + err);
}

Alternative using string name for group:

console.log('Current uid: ' + process.getuid());
try {
  process.setuid("_www");
  console.log('New uid: ' + process.getuid());
}
catch (err) {
  console.log('Failed to set uid: ' + err);
}
$ sudo node doc_test.js 
Current uid: 0
New uid: 70

Example failure due to insufficient permissions:

$ node doc_test.js 
Current uid: 501
Failed to set uid: Error: EPERM, Operation not permitted

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s