getgroups(2) System Calls Manual getgroups(2) getgroups, setgroups - / C (libc, -lc) #include int getgroups(int size, gid_t list[_Nullable size]); #include int setgroups(size_t size, const gid_t list[size]); glibc (. feature_test_macros(7)): setgroups(): glibc 2.19: _DEFAULT_SOURCE glibc 2.19 : _BSD_SOURCE getgroups() list. size , , list. size , . , ( getegid(2) ). size , list , . list, getgroups(). setgroups() . ( EPERM ). size , list. : setgroups(0, (gid_t [0]){}); On success, getgroups() returns the number of supplementary group IDs. On error, -1 is returned, and errno is set to indicate the error. setgroups() 0; -- -1, errno . EFAULT list . getgroups() : EINVAL size , . setgroups() : EINVAL size NGROUPS_MAX (32, Linux 2.6.4; 65536, Linux 2.6.4). ENOMEM . EPERM ( CAP_SETGID , ). EPERM ( Linux 3.19) setgroups() user. /proc/pid/setgroups user_namespaces(7). C ID . POSIX , . NPTL POSIX , UID GID . ( setgroups()) , , , . nptl(7). getgroups() POSIX.1-2024. setgroups() . getgroups() 4.3BSD, SVr4, POSIX.1-1988. setgroups() SVr4, 4.3BSD. Since setgroups() requires privilege, it is not covered by POSIX.1. getgroups() Linux 16- . Linux 2.4 getgroups32(), 32- . glibc getgroups() . , , NGROUPS_MAX . NGROUPS_MAX . execve(2). sysconf(3): long ngroups_max; ngroups_max = sysconf(_SC_NGROUPS_MAX); , getgroups(), , . Linux 2.6.4, Linux, -- /proc/sys/kernel/ngroups_max. #include #include #include #include #include #include #include #define MALLOC(n, T) ((T *) reallocarray(NULL, n, sizeof(T))) static gid_t *agetgroups(size_t *ngids); int main(void) { gid_t *gids; size_t n; gids = agetgroups(&n); if (gids == NULL) err(EXIT_FAILURE, "agetgroups"); if (n != 0) { printf("%jd", (intmax_t) gids[0]); for (size_t i = 1; i < n; i++) printf(" %jd", (intmax_t) gids[i]); } puts(""); free(gids); exit(EXIT_SUCCESS); } static gid_t * agetgroups(size_t *ngids) { int n; gid_t *gids; n = getgroups(0, NULL); if (n == -1) return NULL; gids = MALLOC(n, gid_t); if (gids == NULL) return NULL; n = getgroups(n, gids); if (n == -1) { free(gids); return NULL; } *ngids = n; return gids; } getgid(2), setgid(2), getgrouplist(3), group_member(3), initgroups(3), capabilities(7), credentials(7) () Azamat Hackimov , Dmitry Bolkhovskikh , Vladislav , Yuri Kozlov ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () <>. Linux 6.17 8 2026 . getgroups(2)