id – print real and effective user and group IDs

ABOUT id

Print  user  and  group  information  for  the  specified USERNAME, or (when USERNAME omitted) for the
current user

A TYPICAL COMMANDLINE SESSION WITH id COMMAND

$id
uid=1000(jeffrin) gid=1000(jeffrin) groups=1000(jeffrin),29(audio),1001(wireshark)
$id -n
id: cannot print only names or real IDs in default format
$id -a
uid=1000(jeffrin) gid=1000(jeffrin) groups=1000(jeffrin),29(audio),1001(wireshark)
$id -g
1000
$id -G
1000 29 1001
$id -Gn
jeffrin audio wireshark
$id -r
id: cannot print only names or real IDs in default format
$id -rG
1000 29 1001
$id -u
1000
$id -un
jeffrin
$

RELATED SOURCE CODE EXPOSURE

/* Print all of the info about the user's user and group IDs. */

static void
print_full_info (const char *username)
{
  struct passwd *pwd;
  struct group *grp;

  printf (_("uid=%s"), uidtostr (ruid));
  pwd = getpwuid (ruid);
  if (pwd)
    printf ("(%s)", pwd->pw_name);

  printf (_(" gid=%s"), gidtostr (rgid));
  grp = getgrgid (rgid);
  if (grp)
    printf ("(%s)", grp->gr_name);

  if (euid != ruid)
    {
      printf (_(" euid=%s"), uidtostr (euid));
      pwd = getpwuid (euid);
      if (pwd)
        printf ("(%s)", pwd->pw_name);
    }

  if (egid != rgid)
    {
      printf (_(" egid=%s"), gidtostr (egid));
      grp = getgrgid (egid);
      if (grp)
        printf ("(%s)", grp->gr_name);
    }

  {
    gid_t *groups;

    gid_t primary_group;
    if (username)
      primary_group = pwd ? pwd->pw_gid : -1;
    else
      primary_group = egid;

    int n_groups = xgetgroups (username, primary_group, &groups);
    if (n_groups < 0)
      {
        if (username)
          error (0, errno, _("failed to get groups for user %s"),
                 quote (username));
        else
          error (0, errno, _("failed to get groups for the current process"));
        ok = false;
        return;
      }
    else if (sysconf(_SC_NGROUPS_MAX) > 0 && n_groups > sysconf(_SC_NGROUPS_MAX))
      {
/* tried to format code( break a line into two) below to make a difference in appearance */ 
        fprintf (stderr, _("Warning: user %s is in more groups than system's \ 
        configured maximum.\n"), (username != NULL)?username:"");
      }

    if (n_groups > 0)
      fputs (_(" groups="), stdout);
    for (int i = 0; i < n_groups; i++)
      {
        if (i > 0)
          putchar (',');
        fputs (gidtostr (groups[i]), stdout);
        grp = getgrgid (groups[i]);
        if (grp)
          printf ("(%s)", grp->gr_name);
      }
    free (groups);
  }

SOURCE CODE TAKEN FROM DEBIAN coreutils SOURCE PACKAGE

LINKS
https://wiki.debian.org/coreutils
https://github.com/coreutils/coreutils/blob/master/src/id.c
https://serverfault.com/questions/9977/what-is-the-windows-equivalent-of-unix-whoami-command
https://stackoverflow.com/questions/15667189/what-is-the-id-function-used-for

 

Leave a Reply

Your email address will not be published. Required fields are marked *