What is uid, euid in Linux

UID

  1. The UID, or User ID, is the real user identifier associated with a process. It represents the user who launched the process. The UID can be got using getuid() function or using id in bash.
1
2
3
4
5
6
7
#include <stdio.h>
#include <unistd.h>
int main() {
uid_t uid = getuid();
printf("UID: %u\n", uid);
return 0;
}
  1. The UID is generally set at the beginning of a process and remains constant throughout the process’s lifetime.
  2. The UID is used for various permission checks and to determine ownership of files and processes.

EUID

  1. The EUID, or Effective User ID, is a separate user identifier that can be changed during the execution of a process, using using system calls like seteuid() or setreuid() to temporarily gain or relinquish certain privileges. Typically, only processes running as the superuser (UID 0) or with the setuid permission can change their effective user ID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
// Attempt to set the effective user ID to 0 (root)
if (seteuid(0) == 0) {
printf("Successfully set the effective UID to 0 (root).\n");
// After performing the privileged operations, you should reset the effective UID
// to the original value using seteuid(getuid()) for security reasons.
seteuid(getuid());
} else {
perror("seteuid");
exit(EXIT_FAILURE);
}
return 0;
}
  1. The EUID is used for most permission checks in the kernel and is what determines the process’s effective privileges.
  2. When a process is executed, the EUID is often initially set to the UID of the user who launched the process, that can be derived from geteuid() (notice the e before uid) system call or echo $EUID in bash.

In most Unix-like systems:

  • UID 0 is reserved for the root user (superuser).
  • UID 1-99 are often reserved for system users and groups.
  • UID 1000 and above are typically assigned to regular user accounts.