Technical skill

Linux process: users and groups

Subject: .../LINUX

How Linux decides who can open a file

Imagine a shared folder where some people may read a document and others may not. Linux makes that decision for programs using numbers for users and groups. A name such as alice is easier for people to remember; the system looks up the name and uses its numeric user ID (UID). A group has a numeric group ID (GID). A process is a running program, and it carries user and group IDs that Linux checks when it opens files or uses other protected resources.

For a first check, run id to see the user and groups of your current shell. Run id alice to look up the IDs assigned to that account. If the account is allowed to read a file only through a group, compare those group IDs with the file's owner, group, and permissions.

Where names come from

Many computers store local account names in /etc/passwd and group names in /etc/group. An account record includes the login name, a password marker, UID, primary GID, comment, home directory, and login shell. Password hashes are normally kept in the more protected shadow database. A group record connects a name to a GID and can list members.

Some systems get names from a network directory or another service. getent passwd alice asks the system's configured name lookup, so it can find accounts beyond the local file. Change accounts through the source configured on that computer. If accounts come from a network directory, editing /etc/passwd will not change them; edit that file only when local files are the active source.

Each account has one primary group (its main group) and may also belong to supplementary, or additional, groups. When a program starts, it receives these credentials. A shell that was already open usually keeps its original supplementary groups after an account's membership changes. A new login session picks up the updated list, and commands started from it inherit that session's credentials.

Compare a process with a file

Use stat -c '%A %U:%G %n' /srv/reports on systems with GNU stat to print the file mode (its permission bits), owner, group, and path. The owner and group names are labels; Linux checks the underlying IDs. On a regular file, read and write permissions control access to its contents. On a directory, execute permission allows a program to pass through it to reach names inside. Access-control lists (ACLs) can add rules, and restrictive permissions can still prevent access even when a user belongs to the file's group.

A shared directory can belong to a dedicated group. On Linux, setting the set-group-ID bit, a special directory permission, can cause new files and subdirectories to inherit that directory's group, depending on the filesystem and program. Check the owner, group, mode, and ACL before changing permissions; making a directory writable by everyone can expose its contents.

A program that can switch special rights may carry several kinds of user and group IDs. Real IDs usually identify the user and group that started it; effective IDs are used for many permission checks; saved IDs let some programs return to an earlier identity after changing it; filesystem IDs are used for file-access checks. These IDs have separate roles and are not interchangeable. A service can also run under a dedicated account with fewer rights than an interactive user. Compare the service's configured account with the live process IDs when investigating access. A service manager, such as systemd, starts and monitors background services. It may start or restart several related processes, so check its configuration as well as the individual process.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful