User identifiers (uid) to textual usernames mapping:
/etc/passwd

Group identifiers (gid) to textual group names mapping:
/etc/group

Overview

Each Linux process has its own credentials/identity, which include various user and group identifiers.

Identifier Description
uid / ruid

gid / rgid
(real) user identifier and (real) group identifier.

On Linux, the uid and gid of a process are its real identifiers (also referred to as ruid and rgid), which determine who owns the process.

The uid and gid are uniquely linked, respectively, to a specific user and group. The association between the uid/gid and textual usernames/group names is defined in the /etc/passwd and /etc/group files.

In addition to being used as identifiers in process credentials, the uid and gid are also stored in the inodes of the file system.

The real identifiers are inherited from the parent process. Only privileged processes (with the CAP_SETUID / CAP_SETGID capabilities, such as root processes) can set the real identifiers of a process to arbitrary values.

If executing a SUID or SGID binary, the real identifiers of the new process will remain inherited from the invoking process.

If calling a command through sudo (a SUID binary owned by root):
- The sudo process itself will run with the euid of root and the real uid of the invoking process.
- The command executed through sudo will run with uid, euid, gid, and egid set to root, as sudo sets the real identifiers to 0.
euid

egid
effective user identifier and effective group identifier.

The euid and egid of the process are used for most access checks and as the user/group owner for files created by that process.

The euid and egid should generally be equal, respectively, to the uid and gid of the user associated with the process.

Executing a SUID or SGID binary (i.e. a binary with its SUID/SGID bit set) will result in the new process’s euid/egid being set to the user/group owner of the SUID/SGID file.

euid != (real) uid can thus be an indication of privilege escalation through a SUID binary.
suid

sgid
saved user identifier and saved group identifier.

The suid and sgid are used as a way to allow a privileged process to temporarily downgrade privileges and revert to full privileges. When a privileged process (typically with an euid of 0/root) lowers its euid to an unprivileged value (for example with seteuid()), its suid keeps the privileged value (typically 0/root). As an unprivileged process may set its euid to its real uid or its suid value, the process can later revert to full privileges.

Executing a SUID or SGID binary will result in the new process’s suid/sgid being set to the user/group owner of the SUID/SGID file (copy of the new euid/egid).
fsuid

fsgid
filesystem user identifier and filesystem group identifier.

The fsuid and fsgid are used specifically for filesystem access checks, and should, unless set otherwise, be equal to euid and egid, respectively.

fsuid and fsgid mostly remain for backward compatibility, as they have been unnecessary since Linux Kernel 2.0.

Administrative operations and resulting identifiers

# Note that, for ps, the "uid" format specifier is an alias of "euid" (the real user identifier being displayed with the "ruid" format specifier).

# Standard user (uid/gid 1000) logged in.
ps -o uid,euid,ruid,suid,fsuid,egid,rgid,sgid
    UID  EUID  RUID  SUID FSUID  EGID  RGID  SGID
   1000  1000  1000  1000  1000  1000  1000  1000

# Root user logged in.
ps -o uid,euid,ruid,suid,fsuid,egid,rgid,sgid
    UID  EUID  RUID  SUID FSUID  EGID  RGID  SGID
      0     0     0     0     0     0     0     0

# SUID (user owner "root", uid 0) ps, executed as user with uid 1000.
# sudo cp /usr/bin/ps /tmp/ps && sudo chown root /tmp/ps && sudo chmod u+s /tmp/ps
# /tmp/ps -rwsr-xr-x root user
/tmp/ps -o uid,euid,ruid,suid,fsuid,egid,rgid,sgid
    UID  EUID  RUID  SUID FSUID  EGID  RGID  SGID
      0     0  1000     0     0  1000  1000  1000

# SUID (user owner "test", uid 1001) ps, executed as user with uid 1000.
# sudo cp /usr/bin/ps /tmp/ps && sudo chown test /tmp/ps && sudo chmod u+s /tmp/ps
# /tmp/ps -rwsr-xr-x test user
/tmp/ps -o uid,euid,ruid,suid,fsuid,egid,rgid,sgid
    UID  EUID  RUID  SUID FSUID  EGID  RGID  SGID
   1001  1001  1000  1001  1001  1000  1000  1000

# SGID (group owner "root", gid 0) ps, executed as user with uid 1000.
# sudo cp /usr/bin/ps /tmp/ps && sudo chown user:root /tmp/ps && sudo chmod g+s /tmp/ps
# /tmp/ps -rwxr-sr-x user root
/tmp/ps -o uid,euid,ruid,suid,fsuid,egid,rgid,sgid
    UID  EUID  RUID  SUID FSUID  EGID  RGID  SGID
   1000  1000  1000  1000  1000     0  1000     0

# SGID (group owner "test", gid 1001) ps, executed as user with uid 1000.
# sudo cp /usr/bin/ps /tmp/ps && sudo chown user:test /tmp/ps && sudo chmod g+s /tmp/ps
# /tmp/ps -rwxr-sr-x user test
/tmp/ps -o uid,euid,ruid,suid,fsuid,egid,rgid,sgid
    UID  EUID  RUID  SUID FSUID  EGID  RGID  SGID
   1000  1000  1000  1000  1000  1001  1000  1001

# sudo, executed as user with uid 1000.
sudo ps -o cmd,uid,euid,ruid,suid,fsuid,egid,rgid,sgid
    CMD                           UID  EUID  RUID  SUID FSUID  EGID  RGID  SGID
    sudo ps -o cmd,uid,euid,rui     0     0  1000     0     0     0     0     0
    ps -o cmd,uid,euid,ruid,sui     0     0     0     0     0     0     0     0

# su (root).
su root
ps -o uid,euid,ruid,suid,fsuid,egid,rgid,sgid
    UID  EUID  RUID  SUID FSUID  EGID  RGID  SGID
      0     0     0     0     0     0     0     0

References



View on GitHub