How to set two gid use SystemUserSpawner

I want to use jupyterhub with the SystemUserSpawner to spawn a container with a specific user(with two system groups),I tried with the code below ,but failed, any other solution? Thanks !!

def get_gids_from_id(username):
    id_out = subprocess.check_output(['id', username]).decode('ascii')
    uid, gid, groups = id_out.strip().split(' ')
    _, groups = groups.split('=')
    gids = [int(g.split('(')[0]) for g in groups.split(',')]
    return gids
class DemoFormSpawner(SystemUserSpawner):
     def get_args(self):
            argv = super().get_args()
            if self.user_options.get('argv'):
            argv.extend(self.user_options['argv'])
            return argv
    def get_env(self):
        env = super().get_env()
        if self.user_options.get('env'):
            env.update(self.user_options['env'])
        env.pop('USER',  None)
        env['NB_UID'] = self.user_id
        env.update(NB_GID=get_gids_from_id(self.user.name))
        return env
c.JupyterHub.spawner_class = DemoFormSpawner
c.SystemUserSpawner.extra_create_kwargs = {'user': 'root'}
c.SystemUserSpawner.environment = {
  'CHOWN_HOME':'yes',
  'CHOWN_HOME_OPTS':'-R',
}

and get the error like:

Set username to: test
Set user test UID:GID to: 2009:[800, 804]
groupadd: invalid group ID '[800, 804]'```

Are you using one of the docker-stacks images? If so this is one of the startup scripts that uses NB_GID:

You’ll need to build your own image with custom handling for multiple groups.

Thanks,it is solved!

1 Like