I try to determine which group(s) a running child process has inherited. I want to find all groups the process is in for his uid. Is there a way to determine this via /proc filesystem?

  • Are you looking for the groups associated to the process's UID (which one?), or for the groups that the process is in? The two are usually the same, but they might not be (e.g. if the process was run with sudo -g, or if the group database changed since the user logged in). – Gilles May 17 at 15:58

The list of groups is given under Groups in /proc/<pid>/status; for example,

$ grep '^Groups' /proc/$$/status
Groups: 4 24 27 30 46 110 115 116 1000

The primary group is given under Gid:

$ grep '^Gid' /proc/$$/status
Gid:    1000    1000    1000    1000

ps is also capable of showing the groups of a process, as the other answers indicate.

Using ps:

$ ps -o group,supgrp $$
GROUP    SUPGRP
muru     adm,cdrom,sudo,dip,www-data,plugdev,lpadmin,mlocate,sambashare,lxd,libvirtd,docker,muru

From man ps, the output columns used for -o:

   egid        EGID      effective group ID number of the process as a
                         decimal integer.  (alias gid).

   egroup      EGROUP    effective group ID of the process.  This will be
                         the textual group ID, if it can be obtained and
                         the field width permits, or a decimal
                         representation otherwise.  (alias group).

   gid         GID       see egid.  (alias egid).

   group       GROUP     see egroup.  (alias egroup).

   supgid      SUPGID    group ids of supplementary groups, if any.  See
                         getgroups(2).

   supgrp      SUPGRP    group names of supplementary groups, if any.  See
                         getgroups(2).

For the effective group id, real group id and supplementary group ids (as used for access control):

ps -o gid,rgid,supgid -p "$pid"

gid and rgid are fairly portable, supgid less so (all 3 would be available with the ps from procps as typically found on Linux-based systems).

group, rgroup and supgrp can be used to translate group ids to group names, but note that for group ids that have several corresponding group names, only one of them will be shown (same as for ls -l vs ls -n or anything that deals with user or group names based on ids).

For the process group id (as used for terminal job control):

ps -o pgid -p "$pid"

On a UNIX system derived from SVr4, you may call:

pcred <prcess-id>

Note that the official procfs is not ASCII but binary.

  • The question is about Linux, which is not (much) derived from SVr4, so this doesn't answer the question. Also there is no such thing as “the official procfs”: each Unix variant has its own implementation of it, or doesn't have one. – Gilles May 17 at 16:00
  • You seem to miss that there was a paper on procfs and another one on procfs-2. Linux has not much more in common with that paper than the name procfs. Since this portal is about UNIX, it is obvious that there are people who like to know how things work on UNIX even thought the question might have been Linux specific. – schily May 17 at 20:09

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.