process.setgroups(groups)


  • groups 整数数组[]

process.setgroups() 方法用于设置 Node.js 进程的补充组 ID。这是一个需要特权的操作,要求 Node.js 进程具有 root 权限或 CAP_SETGID 能力。

【The process.setgroups() method sets the supplementary group IDs for the Node.js process. This is a privileged operation that requires the Node.js process to have root or the CAP_SETGID capability.】

groups 数组可以包含数字组 ID、组名或两者。

【The groups array can contain numeric group IDs, group names, or both.】

import process from 'node:process';

if (process.getgroups && process.setgroups) {
  try {
    process.setgroups([501]);
    console.log(process.getgroups()); // new groups
  } catch (err) {
    console.error(`Failed to set groups: ${err}`);
  }
}const process = require('node:process');

if (process.getgroups && process.setgroups) {
  try {
    process.setgroups([501]);
    console.log(process.getgroups()); // new groups
  } catch (err) {
    console.error(`Failed to set groups: ${err}`);
  }
}

此功能仅在 POSIX 平台上可用(即不支持 Windows 或 Android)。 此功能在 Worker 线程中不可用。

【This function is only available on POSIX platforms (i.e. not Windows or Android). This feature is not available in Worker threads.】